ZooKeeper学习笔记-6---ZkClient使用
来源:互联网 发布:手机淘宝如何管理店铺 编辑:程序博客网 时间:2024/05/01 01:10
ZkClient是一个开源的ZooKeeper客户端,是在原生的ZooKeeper API接口之上进行包装,是一个更易使用的ZooKeeper客户端。ZkClient在内部实现了Session超时重连、Watcher反复注册等功能,使得ZooKeeper客户端的繁琐细节对开发人员透明。
接下来,我们将从创建会话、创建节点、读取数据、更新数据、删除节点、检测节点等方面介绍ZkClient的使用。
1.会话创建、节点创建、获取子节点、删除节点
public class ZkClientTest { public static void main(String[] args) throws IOException,InterruptedException { //创建会话 ZkClient zkClient = new ZkClient("127.0.0.1:2181", 5000); String path = "/zk-book"; //监测子节点变化 zkClient.subscribeChildChanges(path, new IZkChildListener() { @Override public void handleChildChange(String parentPath, List<String> currentChild) throws Exception { System.out.println(parentPath + " 's child changed ,currentChilds: " + currentChild); } }); //创建节点 zkClient.createPersistent(path); Thread.sleep(1000); System.out.println(zkClient.getChildren(path)); Thread.sleep(1000); zkClient.createPersistent(path + "/c1"); Thread.sleep(1000); System.out.println(zkClient.getChildren(path)); Thread.sleep(1000); zkClient.createPersistent(path + "/c2","123"); Thread.sleep(1000); System.out.println(zkClient.getChildren(path)); //删除节点 Thread.sleep(1000); zkClient.delete(path + "/c1"); Thread.sleep(1000); System.out.println(zkClient.getChildren(path)); zkClient.delete(path + "/c2"); System.out.println(zkClient.getChildren(path)); Thread.sleep(1000); }}
输出结果:
/zk-book 's child changed ,currentChilds: [][]/zk-book 's child changed ,currentChilds: [c1][c1]/zk-book 's child changed ,currentChilds: [c1, c2][c1, c2]/zk-book 's child changed ,currentChilds: [c2][c2][]/zk-book 's child changed ,currentChilds: []
2.改变节点数据、检测节点是否存在
public class ZkClientTest { public static void main(String[] args) throws InterruptedException { //创建会话 ZkClient zkClient = new ZkClient("127.0.0.1:2181", 5000); //创建节点 String path = "/zk-book"; zkClient.createPersistent(path,"123"); //监测节点数据变化 zkClient.subscribeDataChanges(path, new IZkDataListener() { @Override public void handleDataDeleted(String dataPath) throws Exception { System.out.println("Node " + dataPath + " deleted."); } @Override public void handleDataChange(String dataPath, Object data) throws Exception { System.out.println("Node " + dataPath + " changed, new data: " +data); } }); //读取节点数据 System.out.println(zkClient.readData(path)); zkClient.writeData(path, "456"); Thread.sleep(1000); System.out.println("Node exists :" + zkClient.exists(path)); zkClient.delete(path); System.out.println("Node exists :" + zkClient.exists(path)); }}
输出结果:
123Node /zk-book changed, new data: 456Node exists :trueNode exists :false
1 0
- ZooKeeper学习笔记-6---ZkClient使用
- zookeeper学习笔记-zkclient,curator使用
- zookeeper学习笔记-zkclient,curator使用
- zookeeper学习笔记-zkclient,curator使用
- Zookeeper学习(五):ZKClient的使用
- Zookeeper学习(五):ZKClient的使用
- Zookeeper学习(五):ZKClient的使用
- ZooKeeper之zkClient使用
- zookeeper zkClient使用
- 使用zkclient操作zookeeper的学习过程记录
- 使用zkClient连接zookeeper服务
- zookeeper三:zkClient的使用
- ZooKeeper学习总结(2)——ZooKeeper开源Java客户端ZkClient使用
- zookeeper的zkclient的使用简介
- ZooKeeper 学习 (五) 开源ZkClient操作ZooKeeper
- Zookeeper——3、使用zkClient操作zookeeper
- 项目使用dubbo 、zookeeper服务报 Could not initialize class org.I0Itec.zkclient.ZkClient
- I0Itec-zkClient --- Kafka中使用的Zookeeper客户端
- IWebsite
- Android展现层与业务层的数据解耦
- 玩游戏赚钱真实收入5000+
- pixhawk-console调试之ardupilot
- 方阵中的最大乘积
- ZooKeeper学习笔记-6---ZkClient使用
- Ubuntu 16.04无线网卡RTL8723BE频繁掉线及信号不足解决办法
- HDU 5727 - Necklace
- Spark1.5堆内存分配
- 【我的Android进阶之旅】解决SVN Cleanup错误: Failed to run the WC DB work queue associated with
- Linux kernel printk的用法
- ibatis中动态查询返回字段返回用resultClass="java.util.HashMap" Java.sql.SQLException: 列名无效的问题
- ES6笔记:正则的扩展
- ROC曲线-阈值评价标准