概述:
Curator是Netflix公司开源的一套zookeeper客户端框架
好处:
解决了很多Zookeeper客户端非常底层的细节开发工作,包括连接重连、反复注册和异常等...
基本API
1、创建会话
使用静态工程方法创建
RetryPolicy retryPolicy = new ExponentialBackoffRetry(1000, 3);
CuratorFramework client = CuratorFrameworkFactory.newClient("localhost:2181", 5000, 5000, retryPolicy);
使用Fluent风格api创建
RetryPolicy retryPolicy = new ExponentialBackoffRetry(1000, 3);
CuratorFramework client = CuratorFrameworkFactory.builder()
.connectString("192.168.128.129:2181")
.sessionTimeoutMs(5000)
.connectionTimeoutMs(5000)
.retryPolicy(retryPolicy)
.namespace("base")
.build();
client.start();
2、创建数据节点
client.create().creatingParentContainersIfNeeded()
.withMode(CreateMode.PERSISTENT).forPath("/nodeA", "init".getBytes());
3、删除数据节点
client.delete().guaranteed().deletingChildrenIfNeeded().withVersion(10086).forPath("/nodeA");
4、读取数据节点
读数据,返回值为byte[]
byte[] bytes = client.getData().forPath("/nodeA");
System.out.println(new String(bytes));
读stat
Stat stat = new Stat();
client.getData().storingStatIn(stat).forPath("/nodeA");
5、修改数据节点
client.setData().withVersion(10086).forPath("/nodeA", "data".getBytes());
6、事务
client.inTransaction().check().forPath("/nodeA")
.and()
.create().withMode(CreateMode.EPHEMERAL).forPath("/nodeB", "init".getBytes())
.and()
.create().withMode(CreateMode.EPHEMERAL).forPath("/nodeC", "init".getBytes())
.and()
.commit();
7、其他
client.checkExists().forPath("/nodeA");
client.getChildren().forPath("/nodeA");
|
|