💎一站式轻松地调用各大LLM模型接口,支持GPT4、智谱、星火、月之暗面及文生图 广告
[TOC] # 1. 节点的增删改查 以SprintBook Web项目为例。 (1)添加依赖 *`pom.xml`* ```xml <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.11</version> <scope>test</scope> </dependency> <dependency> <groupId>org.apache.zookeeper</groupId> <artifactId>zookeeper</artifactId> <version>3.4.5</version> </dependency> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-api</artifactId> <version>1.7.21</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> <version>RELEASE</version> <scope>compile</scope> </dependency> ``` (2)zookeeper服务器配置 *`application.properties`* ```properties # 如果是集群使用,隔开,如hadoop101:2181,hadoop102:2181 zookeeper.adrress=hadoop101:2181 zookeeper.timeout=5000 ``` (3)将Zookeeper组件注册到IOC容器中 *`config/ZKConfig.java`* ```java import org.apache.zookeeper.WatchedEvent; import org.apache.zookeeper.Watcher; import org.apache.zookeeper.ZooKeeper; import org.springframework.beans.factory.annotation.Value; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import java.util.concurrent.CountDownLatch; @Configuration public class ZKConfig { @Value("${zookeeper.address}") private String address; @Value("${zookeeper.timeout}") private int timeout; @Bean(name = "zkClient") public ZooKeeper zkClient() { ZooKeeper zooKeeper = null; try { final CountDownLatch countDownLatch = new CountDownLatch(1); zooKeeper = new ZooKeeper(address, timeout, new Watcher() { @Override public void process(WatchedEvent watchedEvent) { if (Event.KeeperState.SyncConnected == watchedEvent.getState()) { countDownLatch.countDown(); } } }); countDownLatch.await(); System.out.println("程序初始化的时候,zookeeper连接成功了"); } catch (Exception e) { System.out.println("程序初始化的时候,zookeeper连接失败" + e); } return zooKeeper; } } ``` (4)Zookeeper工具类 *`utils/ZKApi.java`* ```java import org.apache.zookeeper.CreateMode; import org.apache.zookeeper.ZooDefs; import org.apache.zookeeper.ZooKeeper; import org.apache.zookeeper.data.Stat; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; @Component public class ZKApi { @Autowired private ZooKeeper zooKeeper; /** * 判断某一个节点是否存在 */ public Stat exists(String path) { try { return zooKeeper.exists(path, false); } catch (Exception e) { e.printStackTrace(); return null; } } /** * 创建一个新的节点 */ public boolean createNode(String path, String data) { try { zooKeeper.create(path, data.getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); return true; } catch (Exception e) { e.printStackTrace(); return false; } } /** * 更新一个节点的数据 */ public boolean updateNode(String path, String data) { try { zooKeeper.setData(path, data.getBytes(), -1); return true; } catch (Exception e) { e.printStackTrace(); return false; } } /** * 删除一个节点 */ public boolean deleteNode(String path) { try { zooKeeper.delete(path, -1); return true; } catch (Exception e) { e.printStackTrace(); return false; } } } ``` (5)在controller层中测试 *`controller/ZKController.java`* ```java import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import zookeeper.znode.utils.ZKApi; @RestController public class ZKController { @Autowired private ZKApi zkApi; @RequestMapping("/createNode") public String createNode() { String path = "/bd202/node001"; String data = "this is znode!"; boolean b = zkApi.createNode(path, data); return b + " "; } @RequestMapping("/exists") public String exists(String path) { path = "/" + path; return zkApi.exists(path).toString(); } @RequestMapping("/updateNode") public String updateNode(String path, String data) { path = "/bd202/" + path; boolean b = zkApi.updateNode(path, data); return b + " "; } @RequestMapping("/deleteNode") public String deleteNode(String path) { boolean b = zkApi.deleteNode(path); return b + " "; } } ```