标签:Zookeeper3.8 ,Curator5.5;
一、简介
ZooKeeper是一个集中的服务,用于维护配置信息、命名、提供分布式同步、提供组服务。分布式应用程序以某种形式使用所有这些类型的服务。
二、环境搭建
1、修改配置文件
# 1、拷贝一份样本配置文件
cp zookeeper-3.8.3/conf/zoo_sample.cfg zookeeper-3.8.3/conf/zoo.cfg
# 2、修改数据文件地址,注意这里用本地路径
dataDir=/local-path/zookeeper-3.8.3/data
# 3、添加一个配置,处理启动日志的提示:ZooKeeper audit is disabled.
audit.enable=true
2、服务启动
# 1、启动服务端
zookeeper-3.8.3/bin/zkServer.sh start
# 2、停止服务端
zookeeper-3.8.3/bin/zkServer.sh stop
# 3、启动客户端
zookeeper-3.8.3/bin/zkCli.sh
3、客户端测几个增删查的命令
[zk: localhost:2181(CONNECTED) 0] create /cicada smile1
Created /cicada
[zk: localhost:2181(CONNECTED) 1] get /cicada
smile1
[zk: localhost:2181(CONNECTED) 2] ls /
[cicada, zookeeper]
[zk: localhost:2181(CONNECTED) 3] delete /cicada
三、工程搭建
1、工程结构
2、依赖管理
Curator是一组Java库,它让ZooKeeper的使用变得更加容易,这里的依赖实际是查询匹配版本的时候走了个捷径,也可以参考
integration-redis
包,熟悉下Spring的封装策略。
<!-- Zookeeper组件 --><dependency><groupId>org.apache.zookeeper</groupId><artifactId>zookeeper</artifactId><version>${zookeeper.version}</version></dependency><!-- 包含Curator组件 --><dependency><groupId>org.springframework.integration</groupId><artifactId>spring-integration-zookeeper</artifactId><version>${spring-integration.version}</version></dependency>
3、配置文件
配置脚本
zookeeper:#服务器地址connectString: 127.0.0.1:2181#会话超时时间sessionTimeoutMs:3000#连接超时时间connectionTimeoutMs:60000#最大重试次数maxRetries:3#初始休眠时间baseSleepTimeMs:1000
配置类
@ConfigurationpublicclassZookeeperConfig{@Value("${zookeeper.connectString}")privateString connectString;@Value("${zookeeper.baseSleepTimeMs}")privateint baseSleepTimeMs;@Value("${zookeeper.maxRetries}")privateint maxRetries ;@Value("${zookeeper.connectionTimeoutMs}")int connectionTimeoutMs ;@Value("${zookeeper.sessionTimeoutMs}")int sessionTimeoutMs ;privatestaticCuratorFramework client =null;/**
* 初始化
*/@PostConstructpublicvoid init (){// 重试策略RetryPolicy policy =newExponentialBackoffRetry(baseSleepTimeMs, maxRetries);// 创建Curator
client =CuratorFrameworkFactory.builder().connectString(connectString).connectionTimeoutMs(connectionTimeoutMs).sessionTimeoutMs(sessionTimeoutMs).retryPolicy(policy).build();//开启连接
client.start();}@BeanpublicCuratorFramework getClient (){return client ;}}
四、ZooKeeper用法
测试几个API方法,节点创建和添加数据,以及判断和查询数据,还有就是基于ZooKeeper提供的读写锁能力。
publicclassConfigTest{@AutowiredprivateCuratorFramework client ;@Testpublicvoid testCreate ()throwsException{// 创建一个持久化节点,断开连接时不会自动删除
client.create().creatingParentsIfNeeded().withMode(CreateMode.PERSISTENT).forPath("/path1");}@Testpublicvoid testExists ()throwsException{// 判断节点是否存在,path2不存在所以stat2是nullStat stat1 = client.checkExists().forPath("/path1");System.out.println(stat1);Stat stat2 = client.checkExists().forPath("/path2");System.out.println(stat2);}@Testpublicvoid testSetData ()throwsException{// 设置节点数据
client.setData().forPath("/path1","data1".getBytes(StandardCharsets.UTF_8));}@Testpublicvoid testCreateAndSet ()throwsException{// 创建一个持久化节点并设置节点数据
client.create().creatingParentsIfNeeded().withMode(CreateMode.PERSISTENT).forPath("/path3","data3".getBytes(StandardCharsets.UTF_8));}@Testpublicvoid testGetData ()throwsException{// 查询节点数据byte[] data = client.getData().forPath("/path3");System.out.println(newString(data,StandardCharsets.UTF_8));}@Testpublicvoid testDelete ()throwsException{// 删除节点
client.delete().guaranteed().deletingChildrenIfNeeded().forPath("/path3");}@Testpublicvoid testReadLock ()throwsException{// 读写锁-读InterProcessReadWriteLock lock =newInterProcessReadWriteLock(client,"/lock-read");
lock.readLock().acquire();System.out.println("获取-ReadLock");
lock.readLock().release();}@Testpublicvoid testWriteLock ()throwsException{// 读写锁-写InterProcessReadWriteLock lock =newInterProcessReadWriteLock(client,"/lock-write");
lock.writeLock().acquire();System.out.println("获取-WriteLock");
lock.writeLock().release();}}
五、参考源码
文档仓库:
https://gitee.com/cicadasmile/butte-java-note
源码仓库:
https://gitee.com/cicadasmile/butte-spring-parent
版权归原作者 知了一笑 所有, 如有侵权,请联系我们删除。