创建了一个SpringBoot
项目,引入pom
依赖
<!--zookeeper--> <dependency> <groupId>org.apache.curator</groupId> <artifactId>curator-framework</artifactId> <version>4.2.0</version> </dependency> <dependency> <groupId>org.apache.curator</groupId> <artifactId>curator-client</artifactId> <version>4.2.0</version> </dependency> <dependency> <groupId>org.apache.curator</groupId> <artifactId>curator-recipes</artifactId> <version>4.2.0</version> </dependency> <dependency> <groupId>org.apache.zookeeper</groupId> <artifactId>zookeeper</artifactId> <version>3.4.6</version> <exclusions> <exclusion> <groupId>org.slf4j</groupId> <artifactId>slf4j-log4j12</artifactId> </exclusion> <exclusion> <groupId>log4j</groupId> <artifactId>log4j</artifactId> </exclusion> </exclusions> </dependency>
配置application.yml
属性
curator: #重试次数 maxRetries: 3 #重试间隔时间 baseSleepTimeMs: 1000 # zookeeper 地址 多个可用逗号分隔127.0.0.1:2181,127.0.0.1:2182 connectString: 192.168.106.128:2181 # session超时时间 sessionTimeoutMs: 60000 # 连接超时时间 connectionTimeoutMs: 5000 path: /distributed-lock
配置注入Bean
package com.example.zkConfig; import lombok.Data; import org.apache.curator.framework.CuratorFramework; import org.apache.curator.framework.CuratorFrameworkFactory; import org.apache.curator.retry.RetryNTimes; import org.springframework.beans.factory.annotation.Value; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; @Data @Configuration public class ZookeeperConfig { @Value("${curator.maxRetries}") private int maxRetries; @Value("${curator.baseSleepTimeMs}") private int baseSleepTimeMs; @Value("${curator.connectString}") private String connectString; @Value("${curator.sessionTimeoutMs}") private int sessionTimeoutMs; @Value("${curator.connectionTimeoutMs}") private int connectionTimeoutMs; @Value("${curator.path}") private String path; //调用start初始化方法 @Bean(initMethod = "start") public CuratorFramework curatorFramework(){ return CuratorFrameworkFactory.newClient( this.connectString, this.sessionTimeoutMs, this.connectionTimeoutMs, new RetryNTimes(this.maxRetries,this.connectionTimeoutMs) ); } }
测试文件ZkTest
一些基本操作
package com.example; import org.apache.curator.framework.CuratorFramework; import org.apache.zookeeper.CreateMode; import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; @SpringBootTest public class ZkTest { @Autowired private CuratorFramework curatorFramework; @Test public void test1() throws Exception { //创建持久节点 // String s = curatorFramework.create().forPath("/test-node1"); // 创建临时序号节点 String s = curatorFramework.create().withMode(CreateMode.EPHEMERAL_SEQUENTIAL).forPath("/test-node2", "abc".getBytes()); System.out.println(s); } @Test public void test2() throws Exception { //获取节点的数据 byte[] bytes = curatorFramework.getData().forPath("/test-node1"); System.out.println(new String(bytes)); } @Test public void test3() throws Exception { //修改节点的数据 curatorFramework.setData().forPath("/test-node1","你好".getBytes()); //获取节点的数据 byte[] bytes = curatorFramework.getData().forPath("/test-node1"); System.out.println(new String(bytes)); } @Test public void test4() throws Exception { //创建若父节点不存在则先创建父节点 String s = curatorFramework.create().creatingParentsIfNeeded().forPath("/node-parent/node-1"); System.out.println(s); } @Test public void test5() throws Exception { //删除父节点 子节点存在也一并删除 curatorFramework.delete().guaranteed().deletingChildrenIfNeeded().forPath("/node-parent"); } }