Zookeeper客户端使用
二、使用zkclient
在pom.xml中加入依赖
<dependency> <groupId>com.101tec</groupId> <artifactId>zkclient</artifactId> <version>0.2</version> </dependency>
直接上代码:
1 /** 2 * Project Name:mk-project <br> 3 * Package Name:com.suns.zookeeper.zkclient <br> 4 * 5 * @author mk <br> 6 * Date:2018-10-31 11:05 <br> 7 */ 8 9 package com.suns.zookeeper.zkclient; 10 11 import org.I0Itec.zkclient.IZkChildListener; 12 import org.I0Itec.zkclient.IZkDataListener; 13 import org.I0Itec.zkclient.ZkClient; 14 import org.apache.zookeeper.CreateMode; 15 import org.apache.zookeeper.data.Stat; 16 17 import java.util.List; 18 19 20 /** 21 * zkclient客户端使用 22 * 和原生zookeeper优点: 23 * 1.使用api更方便 24 * 2.订阅节点数据改变或者子节点变化,只需要订阅一次,便可以一直使用。而原生zookeeper的监听是一次性的,需要重复注册。 25 * ClassName: ZkClientTest <br> 26 * Description: <br> 27 * @author mk 28 * @Date 2018-10-31 11:05 <br> 29 * @version 30 */ 31 public class ZkClientTest { 32 33 public static final String connect = "127.0.0.1:2181"; 34 private static ZkClient zkClient = null; 35 private static String nodePath = "/zkclient1"; 36 private static String nodeChildPath = "/zkclient1/n1/n11/n111/n1111"; 37 38 public static void main(String[] args) throws Exception { 39 40 //初始化 41 init(connect,5000); 42 43 //订阅节点数据改变或者子节点变化,只需要订阅一次,便可以一直使用。而原生zookeeper的监听是一次性的,需要重复注册。 44 subscribe(); 45 46 //新增 47 create(nodePath,"n1"); 48 //递归新增 49 createRecursion(nodeChildPath,"n1"); 50 51 //查询 52 query(nodePath); 53 54 //修改 55 update(nodePath,"n11"); 56 57 //单个节点删除 58 // delete(nodePath); 59 //递归删除 60 deleteRecursion(nodePath); 61 62 } 63 64 private static void deleteRecursion(String path) { 65 boolean result = zkClient.deleteRecursive(path); 66 System.out.println("delete:"+"["+path+"],result:"+result); 67 } 68 69 private static void delete(String path) { 70 boolean result = zkClient.delete(path); 71 System.out.println("delete:"+"["+path+"],result:"+result); 72 } 73 74 private static void update(String path, String data) { 75 Stat stat = zkClient.writeData(path, data); 76 System.out.println("setData:"+"["+path+"],stat:"+stat); 77 } 78 79 private static void query(String path) { 80 Object o = zkClient.readData(path); 81 System.out.println("query:"+"["+path+"],result:"+o); 82 } 83 84 private static void createRecursion(String path,String data) { 85 zkClient.createPersistent(path,true); 86 System.out.println("create:"+"["+path+"-->"+data); 87 } 88 89 private static void create(String path, String data) { 90 boolean exists = zkClient.exists(path); 91 if(exists){ 92 System.out.println("节点["+path+"]已存在,不能新增"); 93 return; 94 } 95 String result = zkClient.create(path, data, CreateMode.PERSISTENT); 96 System.out.println("create:"+"["+path+"-->"+data+"],result:"+result); 97 } 98 99 private static void subscribe() { 100 //订阅节点内容改变 101 zkClient.subscribeDataChanges(nodePath, new IZkDataListener() { 102 @Override 103 public void handleDataChange(String path, Object data) throws Exception { 104 System.out.println("handleDataChange----->"+path+"|"+data); 105 } 106 107 @Override 108 public void handleDataDeleted(String path) throws Exception { 109 System.out.println("handleDataDeleted----->"+path); 110 } 111 }); 112 113 //订阅子节点改变 114 zkClient.subscribeChildChanges(nodePath, new IZkChildListener() { 115 @Override 116 public void handleChildChange(String parentPath, List<String> list) throws Exception { 117 System.out.println("handleChildChange----->"+parentPath+"|"+list); 118 } 119 }); 120 121 } 122 123 private static void init(String connect, int sessionTimeout) { 124 zkClient = new ZkClient(connect, sessionTimeout); 125 } 126 }
运行结果: