org.springframework.boot spring-boot-starter-parent 2.1.8.RELEASE org.springframework.boot spring-boot-starter org.springframework.boot spring-boot-starter-test test org.springframework.boot spring-boot-starter-web org.projectlombok lombok org.apache.curator curator-framework 4.0.0 org.apache.curator curator-recipes 4.0.0
Curator 是 Apache ZooKeeper 的Java客户端库
application.yml配置文件server: port: 8000 zookeeper: address: 192.168.23.129:2181 #zookeeper端口以及地址 sessionTimeoutMs: 2000 #会话超时时间,单位毫秒,默认60000ms connectionTimeoutMs: 2000 #连接创建超时时间,单位毫秒,默认60000msConfig配置文件 生成zookeeper连接Bean
在启动时,自动加载bean创建连接
@Configuration @Data public class ZKclientConfig { @Value("${zookeeper.address}") private String connect; @Value("${zookeeper.sessionTimeoutMs}") private Integer sessionTimeoutMs; @Value("${zookeeper.connectionTimeoutMs}") private Integer connectionTimeoutMs; @Bean public Curatorframework Client(){ //重试策略 RetryPolicy retryPolicy = new ExponentialBackoffRetry(3000, 10); Curatorframework ZkClient = CuratorframeworkFactory.builder() .connectString(connect) .sessionTimeoutMs(sessionTimeoutMs) .connectionTimeoutMs(connectionTimeoutMs) .retryPolicy(retryPolicy) .namespace("qyx") //根节点 .build(); ZkClient.start(); return ZkClient; } }测试连接
@Autowired private Curatorframework Client; public void connectTest(){ System.out.println(Client); }创建节点
创建带有数据的节点
public void testCreate() throws Exception { //2. 创建节点 带有数据 //如果创建节点,没有指定数据,则默认将当前客户端的ip作为数据存储 String path = Client.create().forPath("/app2", "hehe".getBytes()); System.out.println(path); Client.close(); }
控制台输出
查询zookeeper
public void testCreate2() throws Exception { //1. 基本创建 //如果创建节点,没有指定数据,则默认将当前客户端的ip作为数据存储 String path = Client.create().forPath("/app1"); System.out.println(path); }
控制台输出
查询zookeeper
public void testCreate3() throws Exception { //3. 设置节点的类型 //默认类型:持久化 //PERSISTENT 持久节点:节点创建后,会一直存在,不会因客户端会话失效而删除 //PERSISTENT_SEQUENTIAL持久顺序节点:基本特性与持久节点一致,创建节点的过程中, zookeeper会在其名字后自动追加一个单调增长的数字后缀,作为新的节点名; //EPHEMERAL 临时节点:客户端会话失效或连接关闭后,该节点会被自动删除, //EPHEMERAL_SEQUENTIAL 临时顺序节点:基本特性与临时节点一致,创建节点的过程中,zookeeper会在其名字后自动追加一个单调增长的数字后缀,作为新的节点名; String path = Client.create().withMode(CreateMode.EPHEMERAL).forPath("/app3", "33".getBytes()); byte[] data = Client.getData().forPath("/app3"); System.out.println(path); System.out.println(new String(data)); } }创建多级节点
public void testCreate4() throws Exception { //4. 创建多级节点 /app1/p1 //creatingParentsIfNeeded():如果父节点不存在,则创建父节点 String path = Client.create().creatingParentsIfNeeded().forPath("/app4/p1"); System.out.println(path); }查询节点
public void testGet1() throws Exception { //1. 查询数据:get byte[] data = Client.getData().forPath("/app2"); System.out.println(new String(data)); Client.close(); }查询子节点
public void testGet2() throws Exception { // 2. 查询子节点: ls List查询节点状态信息path = Client.getChildren().forPath("/"); System.out.println(path); }
public void testGet3() throws Exception { Stat status = new Stat(); System.out.println(status); //3. 查询节点状态信息:ls -s Client.getData().storingStatIn(status).forPath("/app2"); System.out.println(status); }修改数据
public void testSet() throws Exception { Client.setData().forPath("/app2", "abc".getBytes()); this.testGet1(); }删除节点
public void testDelete() throws Exception { // 1. 删除单个节点 Client.delete().forPath("/app2"); } public void testDelete2() throws Exception { //2. 删除带有子节点的节点 Client.delete().deletingChildrenIfNeeded().forPath("/app4"); }
查询app2和app4节点
两个节点都被删除了