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使用
- 使用Apache Curator监控Zookeeper的Node和Path的状态
- Spring Boot 构建应用——使用 Curator 操作 ZooKeeper
- zookeeper入门之Curator的使用之几种监听器的使用
- Zookeeper入门之使用curator连接zookeeper并且进行节点的增删改查及ACL
- [curator] Netflix Curator 使用 Zookeeper 编程
- zookeeper入门之Curator框架--几种监听器的使用
- zookeeper学习笔记-zkclient,curator使用
- zookeeper客户端curator使用手记
- zookeeper入门之Curator的使用之几种监听器的使用
- 使用curator实现zookeeper锁服务的示例分享
- zookeeper入门之Curator的使用之几种监听器的使用
- Zookeeper入门之使用curator连接zookeeper并且进行节点的增删改查及ACL
- Zookeeper学习(Curator使用)
- Curator是Netflix开源的一套ZooKeeper客户端框架. Netflix在使用ZooKeeper的过程中发现ZooKeeper自带的客户端太底层, 应用方在使用的时候需要自己处理很多事情
- 使用Apache Curator监控Zookeeper的Node和Path的状态
- 使用curator操作zookeeper
- curator的使用(将Zookeeper封装的客户端)
- Zookeeper——4、使用Curator操作Zookeeper
- Zookeeper开源客户端框架Curator的简单使用