zkClient是一个开源的zookeeper客户端,内部针对zookeeper原生API进行了封装,让 *** 作zookeeper更加的简单;同时zkClient内部还实现了诸如:Session超时重连;Warcher反复注册机制;
导入依赖:常用 *** 作:com.101tec zkclient0.2
package com.example.liunx_demo.zookeeper.zkclient; import org.I0Itec.zkclient.IZkChildListener; import org.I0Itec.zkclient.IZkDataListener; import org.I0Itec.zkclient.ZkClient; import java.util.List; public class test { public static void main(String[] args) { ZkClient zkClient = new ZkClient("192.168.10.80:2181"); System.err.println("会话创建成功"); //==========================1:创建节点,同时递归创建子节点=========================================== zkClient.createPersistent("/zkclient-data01/01-c2", true); System.err.println("节点递归创建完成"); //===========================2:删除节点========================================== // zkClient.deleteRecursive("/zkclient-data01"); // System.err.println("递归删除完成"); //===========================3:获取某节点的子节点列表========================================== List封装的工具类children = zkClient.getChildren("/zkclient-data01"); System.err.println(children); zkClient.subscribeChildChanges("/zkclient-data01", new IZkChildListener() { @Override public void handleChildChange(String s, List list) throws Exception { System.err.println("s" + "的子节点列表发生了变化,变化后的子节点列表为:" + list); } }); //============================5:判断节点是否存在========================================= boolean exists = zkClient.exists("/zkclient-data01"); //============================6:创建节点,并给节点赋值========================================= zkClient.createPersistent("/zkclient-data02", "12345"); //============================6:读取节点内容========================================= zkClient.readData("/zkclient-data02"); zkClient.subscribeDataChanges("/zkclient-data01", new IZkDataListener() { @Override public void handleDataChange(String s, Object o) throws Exception { System.err.println(s + "该节点内容被更新,更新后的内容:" + o); } @Override public void handleDataDeleted(String s) throws Exception { System.err.println(s + "该节点内容被删除"); } }); } }
package com.zk.zkclient; import org.I0Itec.zkclient.IZkChildListener; import org.I0Itec.zkclient.IZkDataListener; import org.I0Itec.zkclient.ZkClient; import java.util.List; public class ZkClientUtils { private static ZkClient zkClient; private static final String address = "192.168.73.131:2191,192.168.73.131:2192,192.168.73.131:2193"; static { zkClient = new ZkClient(address, 30000, 50000); } public static void createPersistent(String path) { zkClient.createPersistent(path, true); } public static void createPersistent(String path, String data) { zkClient.createPersistent(path, data); } public static void createPersistentSequential(String path, String data) { zkClient.createPersistentSequential(path, data); } public static void createEphemeral(String path) { zkClient.createEphemeral(path); } public static void createEphemeral(String path, String data) { zkClient.createEphemeral(path, data); } public static void createEphemeralSequential(String path, String data) { zkClient.createEphemeralSequential(path, data); } public static void delete(String path) { zkClient.delete(path); } public static void deleteRecursive(String path) { zkClient.deleteRecursive(path); } public static void updataNode(String path,String data) throws Exception { zkClient.writeData(path,data); } public static List测试调用工具类:getNodes(String path) { List nodes = zkClient.getChildren(path); return nodes; } public static void addNodeListener(String path, IZkChildListener childListener) { zkClient.subscribeChildChanges(path,childListener); } public static void addContentListener(String path, IZkDataListener dataListener) { zkClient.subscribeDataChanges(path,dataListener); } public static boolean exists(String path) { return zkClient.exists(path); } public static String readNode(String path) { Object o = zkClient.readData(path); return (String) o; } }
package com.zk.zkclient; import org.I0Itec.zkclient.IZkChildListener; import org.I0Itec.zkclient.IZkDataListener; import org.I0Itec.zkclient.ZkClient; import java.util.List; public class Test { public static void main(String[] args) throws InterruptedException { Listnodes = ZkClientUtils.getNodes("/test-javaapi"); // 给此节点注册子节点列表变更事件,当子节点列表发生变更时触发 ZkClientUtils.addNodeListener("/test-javaapi", new IZkChildListener() { @Override public void handleChildChange(String s, List list) throws Exception { System.err.println(s + "节点的子节点列表变更为:" + list); } }); // 删除节点--非递归删除 // ZkClientUtils.delete("/test-javaapi/c1"); // 创建持久节点--递归创建 ZkClientUtils.createPersistent("/test-javaapi/c1"); Thread.sleep(5000); // ZkClientUtils.delete("/test-javaapi/c2"); ZkClientUtils.createPersistent("/test-javaapi/c2"); Thread.sleep(5000); // ZkClientUtils.delete("/test-javaapi/c3"); ZkClientUtils.createPersistent("/test-javaapi/c3"); System.err.println(nodes); Thread.sleep(Integer.MAX_VALUE); } }
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)