zookeeper的zkclient的使用简介
来源:互联网 发布:大数据清洗技术 编辑:程序博客网 时间:2024/05/22 00:29
github: https://github.com/zhaikaishun/zookeeper_tutorial
前言
Zookeeper的原生API,就之前的那一些,用起来还是比较麻烦的,所以,有些工程师对原生的API接口进行了封装,简化了ZK的复杂性。
1. 创建客户端的方法: ZKClient(Arguments)
- 参数1:zkServer zookeeper服务器的地址,用”,”分割
- 参数2:sessionTimeout超时回话,为毫秒,默认是30000ms
- 参数3:connectionTimeOut 连接超时会话
- 参数4:IZKConnection接口的实现类
- 参数5:zkSerializer 兹定于序列化实现
需要引入zkclient的jar包,自行百度即可
ZKClient的基础操作
ZKClient建立连接
直接new即可
String CONNECT_ADDR = "192.168.1.171:2181,192.168.1.172:2181,192.168.1.173:2181"; ZkClient zkc = new ZkClient(new ZkConnection(CONNECT_ADDR), 10000);
ZKClient创建节点
方法1 和原生的差不多
zkc.create(final String path, Object data, final CreateMode mode)
方法2
zkc.createEphemeral("/temp");// 可以支持递归的创建,但是不能递归赋值zkc.createPersistent("/super/c1", true);
删除节点的操作
支持递归的删除
删除 /temp节点zkc.delete("/temp");//递归删除/superzkc.deleteRecursive("/super");
获取子节点和阅读节点内容操作
List<String> list = zkc.getChildren("/super");for(String p : list){ System.out.println(p); String rp = "/super/" + p; String data = zkc.readData(rp); System.out.println("节点为:" + rp + ",内容为: " + data);}
更新节点
zkc.writeData("/super/c1", "新内容");
一个简单的例子
代码在 bjsxt.zkclient.base
public class ZkClientBase { /** zookeeper地址 */ static final String CONNECT_ADDR = "192.168.1.31:2181,192.168.1.32:2181,192.168.1.33:2181"; /** session超时时间 */ static final int SESSION_OUTTIME = 10000;//ms public static void main(String[] args) throws Exception { ZkClient zkc = new ZkClient(new ZkConnection(CONNECT_ADDR), SESSION_OUTTIME); //1. create and delete方法 zkc.createEphemeral("/temp"); zkc.createPersistent("/super/c1", true); Thread.sleep(10000); zkc.delete("/temp"); zkc.deleteRecursive("/super"); //2. 设置path和data 并且读取子节点和每个节点的内容 zkc.createPersistent("/super", "1234"); zkc.createPersistent("/super/c1", "c1内容"); zkc.createPersistent("/super/c2", "c2内容"); List<String> list = zkc.getChildren("/super"); for(String p : list){ System.out.println(p); String rp = "/super/" + p; String data = zkc.readData(rp); System.out.println("节点为:" + rp + ",内容为: " + data); } //3. 更新和判断节点是否存在 zkc.writeData("/super/c1", "新内容"); System.out.println(zkc.readData("/super/c1").toString()); System.out.println(zkc.exists("/super/c1"));// 4.递归删除/super内容 zkc.deleteRecursive("/super"); }}
输出
c1节点为:/super/c1,内容为: c1内容c2节点为:/super/c2,内容为: c2内容新内容true
ZKClient Watch的使用
我们发现,上述ZKClient里面并没有类似的watcher、watch参数,这也就是说我们开发人员无需关心反复注册watcher的问题,zkclient给我们提供了一套监听方式,我们可以使用监听节点的方式进行操作,剔除了繁琐的反复watcher操作、简化了代码的复杂程度
subscribeChildChanges方法 订阅子节点变化
参数1:path路径
参数2:实现了IZKChildListener接口的类(如:实例化IZKClientListener类) 只需要重写其handleChildChanges(String parentPath, List currentChild)方法。其中
- parentPath 为监听节点全路径
- currentChilds为新的子节点列表
IZKChildListener事件说明针对于下面三个事件触发:
- 新增子节点
- 减少子节点
- 删除节点
注意: 不监听节点内容的变化
举例
public class ZkClientWatcher1 { /** zookeeper地址 */ static final String CONNECT_ADDR = "192.168.1.31:2181,192.168.1.32:2181,192.168.1.33:2181"; /** session超时时间 */ static final int SESSION_OUTTIME = 10000;//ms public static void main(String[] args) throws Exception { ZkClient zkc = new ZkClient(new ZkConnection(CONNECT_ADDR), SESSION_OUTTIME); //对父节点添加监听子节点变化。 zkc.subscribeChildChanges("/super", new IZkChildListener() { @Override public void handleChildChange(String parentPath, List<String> currentChilds) throws Exception { System.out.println("parentPath: " + parentPath); System.out.println("currentChilds: " + currentChilds); } }); Thread.sleep(3000); zkc.createPersistent("/super"); Thread.sleep(1000); zkc.createPersistent("/super" + "/" + "c1", "c1内容"); Thread.sleep(1000); zkc.createPersistent("/super" + "/" + "c2", "c2内容"); Thread.sleep(1000); zkc.delete("/super/c2"); Thread.sleep(1000); zkc.deleteRecursive("/super"); Thread.sleep(Integer.MAX_VALUE); }}
输出
parentPath: /supercurrentChilds: []parentPath: /supercurrentChilds: [c1]parentPath: /supercurrentChilds: [c1, c2]parentPath: /supercurrentChilds: [c1]parentPath: /supercurrentChilds: nullparentPath: /supercurrentChilds: null
subscribeDataChanges 订阅内容变化
和前面的subscribeChildChanges类似
- 参数1 路径
- 参数2 IZkDataListener对象,重写handleDataDeleted(String path) 方法,可以得到删除节点的path 重写handleDataChange(String path, Object data)可以得到变更的节点和变更的内容
举例
public class ZkClientWatcher2 { /** zookeeper地址 */ static final String CONNECT_ADDR = "192.168.1.31:2181,192.168.1.32:2181,192.168.1.33:2181"; /** session超时时间 */ static final int SESSION_OUTTIME = 10000;//ms public static void main(String[] args) throws Exception { ZkClient zkc = new ZkClient(new ZkConnection(CONNECT_ADDR), SESSION_OUTTIME); zkc.createPersistent("/super", "1234"); //对父节点添加监听子节点变化。 zkc.subscribeDataChanges("/super", new IZkDataListener() { @Override public void handleDataDeleted(String path) throws Exception { System.out.println("删除的节点为:" + path); } @Override public void handleDataChange(String path, Object data) throws Exception { System.out.println("变更的节点为:" + path + ", 变更内容为:" + data); } }); Thread.sleep(3000); zkc.writeData("/super", "456", -1); Thread.sleep(1000); zkc.delete("/super"); Thread.sleep(Integer.MAX_VALUE); }}
输出
变更的节点为:/super, 变更内容为:456删除的节点为:/super
前言
Zookeeper的原生API,就之前的那一些,用起来还是比较麻烦的,所以,有些工程师对原生的API接口进行了封装,简化了ZK的复杂性。
1. 创建客户端的方法: ZKClient(Arguments)
- 参数1:zkServer zookeeper服务器的地址,用”,”分割
- 参数2:sessionTimeout超时回话,为毫秒,默认是30000ms
- 参数3:connectionTimeOut 连接超时会话
- 参数4:IZKConnection接口的实现类
- 参数5:zkSerializer 兹定于序列化实现
需要引入zkclient的jar包,自行百度即可
ZKClient的基础操作
ZKClient建立连接
直接new即可
String CONNECT_ADDR = "192.168.1.171:2181,192.168.1.172:2181,192.168.1.173:2181"; ZkClient zkc = new ZkClient(new ZkConnection(CONNECT_ADDR), 10000);
ZKClient创建节点
方法1 和原生的差不多
zkc.create(final String path, Object data, final CreateMode mode)
方法2
zkc.createEphemeral("/temp");// 可以支持递归的创建,但是不能递归赋值zkc.createPersistent("/super/c1", true);
删除节点的操作
支持递归的删除
删除 /temp节点zkc.delete("/temp");//递归删除/superzkc.deleteRecursive("/super");
获取子节点和阅读节点内容操作
List<String> list = zkc.getChildren("/super");for(String p : list){ System.out.println(p); String rp = "/super/" + p; String data = zkc.readData(rp); System.out.println("节点为:" + rp + ",内容为: " + data);}
更新节点
zkc.writeData("/super/c1", "新内容");
一个简单的例子
代码在 bjsxt.zkclient.base
public class ZkClientBase { /** zookeeper地址 */ static final String CONNECT_ADDR = "192.168.1.31:2181,192.168.1.32:2181,192.168.1.33:2181"; /** session超时时间 */ static final int SESSION_OUTTIME = 10000;//ms public static void main(String[] args) throws Exception { ZkClient zkc = new ZkClient(new ZkConnection(CONNECT_ADDR), SESSION_OUTTIME); //1. create and delete方法 zkc.createEphemeral("/temp"); zkc.createPersistent("/super/c1", true); Thread.sleep(10000); zkc.delete("/temp"); zkc.deleteRecursive("/super"); //2. 设置path和data 并且读取子节点和每个节点的内容 zkc.createPersistent("/super", "1234"); zkc.createPersistent("/super/c1", "c1内容"); zkc.createPersistent("/super/c2", "c2内容"); List<String> list = zkc.getChildren("/super"); for(String p : list){ System.out.println(p); String rp = "/super/" + p; String data = zkc.readData(rp); System.out.println("节点为:" + rp + ",内容为: " + data); } //3. 更新和判断节点是否存在 zkc.writeData("/super/c1", "新内容"); System.out.println(zkc.readData("/super/c1").toString()); System.out.println(zkc.exists("/super/c1"));// 4.递归删除/super内容 zkc.deleteRecursive("/super"); }}
输出
c1节点为:/super/c1,内容为: c1内容c2节点为:/super/c2,内容为: c2内容新内容true
ZKClient Watch的使用
我们发现,上述ZKClient里面并没有类似的watcher、watch参数,这也就是说我们开发人员无需关心反复注册watcher的问题,zkclient给我们提供了一套监听方式,我们可以使用监听节点的方式进行操作,剔除了繁琐的反复watcher操作、简化了代码的复杂程度
subscribeChildChanges方法 订阅子节点变化
参数1:path路径
参数2:实现了IZKChildListener接口的类(如:实例化IZKClientListener类) 只需要重写其handleChildChanges(String parentPath, List currentChild)方法。其中
- parentPath 为监听节点全路径
- currentChilds为新的子节点列表
IZKChildListener事件说明针对于下面三个事件触发:
- 新增子节点
- 减少子节点
- 删除节点
注意: 不监听节点内容的变化
举例
public class ZkClientWatcher1 { /** zookeeper地址 */ static final String CONNECT_ADDR = "192.168.1.31:2181,192.168.1.32:2181,192.168.1.33:2181"; /** session超时时间 */ static final int SESSION_OUTTIME = 10000;//ms public static void main(String[] args) throws Exception { ZkClient zkc = new ZkClient(new ZkConnection(CONNECT_ADDR), SESSION_OUTTIME); //对父节点添加监听子节点变化。 zkc.subscribeChildChanges("/super", new IZkChildListener() { @Override public void handleChildChange(String parentPath, List<String> currentChilds) throws Exception { System.out.println("parentPath: " + parentPath); System.out.println("currentChilds: " + currentChilds); } }); Thread.sleep(3000); zkc.createPersistent("/super"); Thread.sleep(1000); zkc.createPersistent("/super" + "/" + "c1", "c1内容"); Thread.sleep(1000); zkc.createPersistent("/super" + "/" + "c2", "c2内容"); Thread.sleep(1000); zkc.delete("/super/c2"); Thread.sleep(1000); zkc.deleteRecursive("/super"); Thread.sleep(Integer.MAX_VALUE); }}
输出
parentPath: /supercurrentChilds: []parentPath: /supercurrentChilds: [c1]parentPath: /supercurrentChilds: [c1, c2]parentPath: /supercurrentChilds: [c1]parentPath: /supercurrentChilds: nullparentPath: /supercurrentChilds: null
subscribeDataChanges 订阅内容变化
和前面的subscribeChildChanges类似
- 参数1 路径
- 参数2 IZkDataListener对象,重写handleDataDeleted(String path) 方法,可以得到删除节点的path 重写handleDataChange(String path, Object data)可以得到变更的节点和变更的内容
举例
public class ZkClientWatcher2 { /** zookeeper地址 */ static final String CONNECT_ADDR = "192.168.1.31:2181,192.168.1.32:2181,192.168.1.33:2181"; /** session超时时间 */ static final int SESSION_OUTTIME = 10000;//ms public static void main(String[] args) throws Exception { ZkClient zkc = new ZkClient(new ZkConnection(CONNECT_ADDR), SESSION_OUTTIME); zkc.createPersistent("/super", "1234"); //对父节点添加监听子节点变化。 zkc.subscribeDataChanges("/super", new IZkDataListener() { @Override public void handleDataDeleted(String path) throws Exception { System.out.println("删除的节点为:" + path); } @Override public void handleDataChange(String path, Object data) throws Exception { System.out.println("变更的节点为:" + path + ", 变更内容为:" + data); } }); Thread.sleep(3000); zkc.writeData("/super", "456", -1); Thread.sleep(1000); zkc.delete("/super"); Thread.sleep(Integer.MAX_VALUE); }}
输出
变更的节点为:/super, 变更内容为:456删除的节点为:/super
特别感谢互联网架构师白鹤翔老师,本文大多出自他的视频讲解。
笔者主要是记录笔记,以便之后翻阅,正所谓好记性不如烂笔头,烂笔头不如云笔记
- zookeeper的zkclient的使用简介
- zookeeper三:zkClient的使用
- Zookeeper学习(五):ZKClient的使用
- Zookeeper学习(五):ZKClient的使用
- Zookeeper学习(五):ZKClient的使用
- Zookeeper ZkClient简介
- I0Itec-zkClient --- Kafka中使用的Zookeeper客户端
- 使用zkclient操作zookeeper的学习过程记录
- ZkClient的使用
- ZooKeeper之zkClient使用
- zookeeper zkClient使用
- zkClient的打包及使用
- 使用zkClient连接zookeeper服务
- zookeeper集群之开源客户端ZkClient的使用(四)
- 关于zookeeper第三方客户端zkclient的使用说明
- zookeeper学习笔记-zkclient,curator使用
- zookeeper学习笔记-zkclient,curator使用
- ZooKeeper学习笔记-6---ZkClient使用
- Laravel小项目之第6节 Laravel-通过模型处理性别
- Leetcode 53. Maximum Subarray 贪心或动态规划
- Laravel小项目之第7节 Laravel-通过表单实现修改
- Python生成式和生成器
- Laravel小项目之第8节 Laravel-表单中查看详情及删除 8.1 给HTML添加链接
- zookeeper的zkclient的使用简介
- Android控件之TableLayout布局拉伸宽度
- 一道趣味题 万一用得上呢》》(P1054)
- SSH框架的搭建和整合
- reids关于string型的指令
- 在c++11中讨论nullptr和NULL
- 从调试数据分析USB通信协议——USB键盘鼠标【HID类设备】(四)
- 2017年11月1日 第二十三次总结
- RxJava线程切换源码分析