大家好,又见面了,我是你们的朋友全栈君。
1.引入依赖
<dependency> <groupId>org.apache.zookeeper</groupId> <artifactId>zookeeper</artifactId> <version>3.4.8</version> </dependency>
2.创建会话
1 package com.karat.cn.zookeeper.javaApi; 2 3 import java.io.IOException; 4 import java.util.concurrent.CountDownLatch; 5 6 import org.apache.zookeeper.WatchedEvent; 7 import org.apache.zookeeper.Watcher; 8 import org.apache.zookeeper.ZooKeeper; 9 10 /** 11 * 创建会话(连接zookeeper) 12 * @author 开发 13 * 14 */ 15 public class CreateSessionDemo { 16 17 private final static String CONNECTSTRING="47.107.121.215:2181"; 18 19 private static CountDownLatch countDownLatch=new CountDownLatch(1); 20 21 public static void main(String []args) throws IOException, InterruptedException{ 22 ZooKeeper zooKeeper=new ZooKeeper(CONNECTSTRING, 5000,new Watcher() { 23 @Override 24 public void process(WatchedEvent event) { 25 //如果当前的连接状态是连接成功的,那么通过计数器去控制 26 if(event.getState()==Event.KeeperState.SyncConnected){ 27 countDownLatch.countDown(); 28 System.out.println(event.getState()); 29 } 30 } 31 }); 32 countDownLatch.await(); 33 System.out.println(zooKeeper.getState()); 34 } 35 }
View Code
3.节点的操作
1 package com.karat.cn.zookeeper.javaApi; 2 3 import java.io.IOException; 4 import java.util.List; 5 import java.util.concurrent.CountDownLatch; 6 import java.util.concurrent.TimeUnit; 7 8 import org.apache.zookeeper.CreateMode; 9 import org.apache.zookeeper.KeeperException; 10 import org.apache.zookeeper.WatchedEvent; 11 import org.apache.zookeeper.Watcher; 12 import org.apache.zookeeper.ZooDefs; 13 import org.apache.zookeeper.ZooKeeper; 14 import org.apache.zookeeper.data.Stat; 15 16 /** 17 * java的api对节点的操作 18 * @author 开发 19 * 20 */ 21 public class CreateDodeDemo implements Watcher{ 22 23 private final static String CONNECTSTRING="47.107.121.215:2181"; 24 25 private static CountDownLatch countDownLatch=new CountDownLatch(1); 26 27 private static ZooKeeper zooKeeper; 28 29 private static Stat stat=new Stat(); 30 31 public static void main(String []args) throws IOException, InterruptedException, KeeperException{ 32 /*zooKeeper=new ZooKeeper(CONNECTSTRING, 5000,new Watcher() { 33 @Override 34 public void process(WatchedEvent event) { 35 //如果当前的连接状态是连接成功的,那么通过计数器去控制 36 if(event.getState()==Event.KeeperState.SyncConnected){ 37 if(Event.EventType.None==event.getType()&&null==event.getPath()){ 38 countDownLatch.countDown(); 39 System.out.println(event.getState()+"->"+event.getType()); 40 }else if(event.getType()== Event.EventType.NodeDataChanged){//数据变更触发路径 41 try { 42 System.out.println("数据变更触发路径:"+event.getPath()+"->改变后的值:"+ 43 zooKeeper.getData(event.getPath(),true,stat)); 44 } catch (KeeperException e) { 45 e.printStackTrace(); 46 } catch (InterruptedException e) { 47 e.printStackTrace(); 48 } 49 }else if(event.getType()== Event.EventType.NodeChildrenChanged){//子节点的数据变化会触发 50 try { 51 System.out.println("子节点数据变更路径:"+event.getPath()+"->节点的值:"+ 52 zooKeeper.getData(event.getPath(),true,stat)); 53 } catch (KeeperException e) { 54 e.printStackTrace(); 55 } catch (InterruptedException e) { 56 e.printStackTrace(); 57 } 58 }else if(event.getType()== Event.EventType.NodeCreated){//创建子节点的时候会触发 59 try { 60 System.out.println("节点创建路径:"+event.getPath()+"->节点的值:"+ 61 zooKeeper.getData(event.getPath(),true,stat)); 62 } catch (KeeperException e) { 63 e.printStackTrace(); 64 } catch (InterruptedException e) { 65 e.printStackTrace(); 66 } 67 }else if(event.getType()== Event.EventType.NodeDeleted){//子节点删除会触发 68 System.out.println("节点删除路径:"+event.getPath()); 69 } 70 System.out.println(event.getType()); 71 } 72 } 73 });*/ 74 zooKeeper=new ZooKeeper(CONNECTSTRING, 5000,new CreateDodeDemo());//上面代码改成这一行代码 75 76 77 countDownLatch.await(); 78 System.out.println(zooKeeper.getState()); 79 80 //连接成功创建节点 81 String result=zooKeeper.create("/app","app节点创建".getBytes(),ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.EPHEMERAL); 82 //注册 83 zooKeeper.getData("/app", true, stat); 84 System.out.println("创建成功->"+result); 85 //修改数据 86 zooKeeper.setData("/app", "修改第一次".getBytes(),-1); 87 //睡眠2秒 88 Thread.sleep(2000); 89 //修改数据 90 zooKeeper.setData("/app", "修改第二次".getBytes(),-1); 91 //睡眠2秒 92 Thread.sleep(2000); 93 //删除 94 //zooKeeper.delete("/app", -1); 95 //睡眠2秒 96 //Thread.sleep(2000); 97 //创建节点和子节点(临时节点下不能挂子节点) 98 String path="/node"; 99 100 zooKeeper.create(path, "节点node".getBytes(),ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); 101 TimeUnit.SECONDS.sleep(1); 102 103 Stat stat=zooKeeper.exists(path+"/app",true); 104 if(stat==null){ //表示节点不存在 105 zooKeeper.create(path+"/app","node子节点".getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE,CreateMode.PERSISTENT); 106 TimeUnit.SECONDS.sleep(1); 107 } 108 //修改子路径 109 zooKeeper.setData(path+"/app","修改后的".getBytes(),-1); 110 TimeUnit.SECONDS.sleep(1); 111 112 //获取指定节点下的子节点 113 List<String> list=zooKeeper.getChildren("/node", true);//第二个参数表示获取是否监控 114 System.out.println(list); 115 116 117 } 118 119 120 /** 121 * 实现接口Watcher中的process抽象方法 122 */ 123 public void process(WatchedEvent watchedEvent) { 124 //如果当前的连接状态是连接成功的,那么通过计数器去控制 125 if(watchedEvent.getState()==Event.KeeperState.SyncConnected){ 126 if(Event.EventType.None==watchedEvent.getType()&&null==watchedEvent.getPath()){ 127 countDownLatch.countDown(); 128 System.out.println(watchedEvent.getState()+"-->"+watchedEvent.getType()); 129 }else if(watchedEvent.getType()== Event.EventType.NodeDataChanged){ 130 try { 131 System.out.println("数据变更触发路径:"+watchedEvent.getPath()+"->改变后的值:"+ 132 zooKeeper.getData(watchedEvent.getPath(),true,stat)); 133 } catch (KeeperException e) { 134 e.printStackTrace(); 135 } catch (InterruptedException e) { 136 e.printStackTrace(); 137 } 138 }else if(watchedEvent.getType()== Event.EventType.NodeChildrenChanged){ //子节点的数据变化会触发 139 try { 140 System.out.println("子节点数据变更路径:"+watchedEvent.getPath()+"->节点的值:"+ 141 zooKeeper.getData(watchedEvent.getPath(),true,stat)); 142 } catch (KeeperException e) { 143 e.printStackTrace(); 144 } catch (InterruptedException e) { 145 e.printStackTrace(); 146 } 147 }else if(watchedEvent.getType()== Event.EventType.NodeCreated){ //创建子节点的时候会触发 148 try { 149 System.out.println("节点创建路径:"+watchedEvent.getPath()+"->节点的值:"+ 150 zooKeeper.getData(watchedEvent.getPath(),true,stat)); 151 } catch (KeeperException e) { 152 e.printStackTrace(); 153 } catch (InterruptedException e) { 154 e.printStackTrace(); 155 } 156 }else if(watchedEvent.getType()== Event.EventType.NodeDeleted){ //子节点删除会触发 157 System.out.println("节点删除路径:"+watchedEvent.getPath()); 158 } 159 System.out.println(watchedEvent.getType()); 160 } 161 162 } 163 }
View Code
4.权限控制模式
schema:授权对象
ip : 192.168.1.1
Digest : username:password
world : 开放式的权限控制模式,数据节点的访问权限对所有用户开放。 world:anyone
super :超级用户,可以对zookeeper上的数据节点进行操作
5.连接状态
KeeperStat.Expired 在一定时间内客户端没有收到服务器的通知, 则认为当前的会话已经过期了。
KeeperStat.Disconnected 断开连接的状态
KeeperStat.SyncConnected 客户端和服务器端在某一个节点上建立连接,并且完成一次version、zxid同步
KeeperStat.authFailed 授权失败
6.事件类型
NodeCreated 当节点被创建的时候,触发
NodeChildrenChanged 表示子节点被创建、被删除、子节点数据发生变化
NodeDataChanged 节点数据发生变化
NodeDeleted 节点被删除
None 客户端和服务器端连接状态发生变化的时候,事件类型就是None
转载于:https://www.cnblogs.com/LJing21/p/10536943.html
发布者:全栈程序员-用户IM,转载请注明出处:https://javaforall.cn/106928.html原文链接:https://javaforall.cn
【正版授权,激活自己账号】: Jetbrains全家桶Ide使用,1年售后保障,每天仅需1毛
【官方授权 正版激活】: 官方授权 正版激活 支持Jetbrains家族下所有IDE 使用个人JB账号...