您的位置:首页 > 其它

zookeeper - curator的使用

2017-06-26 00:00 288 查看

简介

《zookeeper - zkclient的使用》 该文中,列举了zk原生客户端的种种不足,并简单介绍了curator。本文将继续介绍curator的API的使用。

curator的使用

创建会话

int sleepMs = 10000;
RetryPolicy retryPolicy = new RetryOneTime(sleepMs); //断线重连策略,这里使用仅重试一次的策略
client = CuratorFrameworkFactory.builder()
.connectString(ZookeeperHelper.zkAddress)
.sessionTimeoutMs(ZookeeperHelper.sessionTimeout)
.retryPolicy(retryPolicy)
.namespace(namespace)  //这个namespace路径必须先存在
.build();
client.start();

retryPolicy :断开重连策略。curator支持ExponentialBackoffRetry、BoundedExponentialBackoffRetry、RetryNTimes、RetryOneTime、RetryForever、RetryUntilElapsed。其中,ExponentialBackoffRetry和BoundedExponentialBackoffRetry都是类似TCP重连的退避重连策略,RetryUntilElapsed是重试次数直到总耗时达到了预定时间,其它的重试策略都是规定次数的。

创建节点

@Test
public void testCreateNode() throws Exception {
String persistent_path = "/create_persistent";  // 持久节点
String ephemeral_path = "/create_ephemeral";  // 临时节点

//创建持久化节点
String path1 = client.create()
.creatingParentContainersIfNeeded() //自动递归创建父节点
.withMode(CreateMode.PERSISTENT)
.forPath(persistent_path);
System.out.println("---------success to create node : " + path1);

//创建持久化序列节点
String path2 = client.create()
.creatingParentContainersIfNeeded() //自动递归创建父节点
.withMode(CreateMode.PERSISTENT_SEQUENTIAL)
.forPath(persistent_path);
System.out.println("---------success to create node : " + path2);

//创建临时节点
String path3 = client.create()
.creatingParentContainersIfNeeded() //自动递归创建父节点
.withMode(CreateMode.EPHEMERAL)
.forPath(ephemeral_path);
System.out.println("---------success to create node : " + path3);

//创建临时序列节点
String path4 = client.create()
.creatingParentContainersIfNeeded() //自动递归创建父节点
.withMode(CreateMode.EPHEMERAL_SEQUENTIAL)
.forPath(ephemeral_path);
System.out.println("---------success to create node : " + path4);

//恢复现场
client.delete()
.forPath(path1);
client.delete()
.forPath(path2);
client.delete()
.forPath(path3);
client.delete()
.forPath(path4);
}

删除节点

@Test
public void testDeleteNode() throws Exception {
String firstLevelPath = "/delete";
String path = firstLevelPath + "/node";  // 多级路径

String createdNode = client.create()
.creatingParentContainersIfNeeded() //自动递归创建父节点
.withMode(CreateMode.PERSISTENT)
.forPath(path);
// 创建好的路径是 /zookeeper-showcase/curator/delete/node
System.out.println("---------success to create node : " + createdNode);

client.delete()
.deletingChildrenIfNeeded()  //递归删除子节点
// .withVersion(0)  //使用版本号删除
.forPath(firstLevelPath); //指的是删除/zookeeper-showcase/curator/delete 节点及其它所有子节点
// 删除后只剩下 /zookeeper-showcase/curator
System.out.println("---------success to all child's nodes ");
}

更新、获取节点的数据

@Test
public void testSetAndGetData() throws Exception {
String dataPath = "/data_path";

//创建持久化节点
client.create()
.creatingParentContainersIfNeeded() //自动递归创建父节点
.withMode(CreateMode.PERSISTENT)
.forPath(dataPath, "data".getBytes());

//读取数据
Stat stat_old = new Stat();
byte[] bytes_old = client.getData()
.storingStatIn(stat_old) //同时读取节点stat
.forPath(dataPath);
System.out.println("节点数据:" + new String(bytes_old));
System.out.println("节点数据版本:" + stat_old.getVersion());

//更新数据
Stat stat_new = client.setData()
// .withVersion(0) //使用版本号
.forPath(dataPath, "new data".getBytes());//更新节点的数据
System.out.println("更新数据后,节点数据版本:" + stat_new.getVersion());

//读取数据
byte[] bytes_new = client.getData()
.forPath(dataPath);
System.out.println("更新数据后,节点数据:" + new String(bytes_new));

client.delete().deletingChildrenIfNeeded().forPath(dataPath);
}

获取子节点

@Test
public void testGetChildren() throws Exception {
String parentNode = "/parent_node";
String childNode1 = parentNode + "/child_Node1";
String childNode2 = parentNode + "/child_Node2";

String path1 = client.create()
.creatingParentContainersIfNeeded() //自动递归创建父节点
.withMode(CreateMode.PERSISTENT)
.forPath(childNode1);

String path2 = client.create()
.creatingParentContainersIfNeeded() //自动递归创建父节点
.withMode(CreateMode.PERSISTENT)
.forPath(childNode2);

List<String> children = client.getChildren().forPath(parentNode);
for (String child : children) {
/**
* 输出:
* 节点:child_Node1
* 节点:child_Node2
*/
System.out.println("节点:" + child);
}

client.delete().deletingChildrenIfNeeded().forPath(parentNode);
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  ZooKeeper curator