zookeeper-javaAPI
权限控制模式
schema:授权对象
ip : 192.168.1.1
Digest : username:password
world : 开放式的权限控制模式,数据节点的访问权限对所有用户开放。 world:anyone
super :超级用户,可以对zookeeper上的数据节点进行操作
连接状态
KeeperStat.Expired 在一定时间内客户端没有收到服务器的通知, 则认为当前的会话已经过期了。
KeeperStat.Disconnected 断开连接的状态
KeeperStat.SyncConnected 客户端和服务器端在某一个节点上建立连接,并且完成一次version、zxid同步
KeeperStat.authFailed 授权失败
事件类型
NodeCreated 当节点被创建的时候,触发
NodeChildrenChanged 表示子节点被创建、被删除、子节点数据发生变化
NodeDataChanged 节点数据发生变化
NodeDeleted 节点被删除
None 客户端和服务器端连接状态发生变化的时候,事件类型就是None
curator
Curator本身是Netflix公司开源的zookeeper客户端;
curator提供了各种应用场景的实现封装
curator-framework 提供了fluent风格api
curator-replice 提供了实现封装
master/leader选举
分布式锁(读锁、写锁)
分布式队列
curator连接的重试策略
ExponentialBackoffRetry() 衰减重试
RetryNTimes 指定最大重试次数
RetryOneTime 仅重试一次
RetryUnitilElapsed 一直重试知道规定的时间
<dependency> <groupId>org.apache.zookeeper</groupId> <artifactId>zookeeper</artifactId> <version>3.4.8</version> </dependency> <dependency> <groupId>com.101tec</groupId> <artifactId>zkclient</artifactId> <version>0.10</version> </dependency>
package com.example.springbootdemo.zookeeper; import org.apache.zookeeper.*; import org.apache.zookeeper.data.ACL; import org.apache.zookeeper.data.Id; import org.apache.zookeeper.data.Stat; import java.io.IOException; import java.util.ArrayList; import java.util.List; import java.util.concurrent.CountDownLatch; import java.util.concurrent.TimeUnit; public class ApiOperatorDemo implements Watcher{ // private final static String CONNECTSTRING="192.168.156.207:2181,192.168.11.134:2181," + // "192.168.11.135:2181,192.168.11.136:2181"; private final static String CONNECTSTRING="192.168.156.207:2181"; private static CountDownLatch countDownLatch=new CountDownLatch(1); private static ZooKeeper zookeeper; private static Stat stat=new Stat(); public static void main(String[] args) throws IOException, InterruptedException, KeeperException { zookeeper=new ZooKeeper(CONNECTSTRING, 5000, new ApiOperatorDemo()); countDownLatch.await(); // ACL acl=new ACL(ZooDefs.Perms.ALL,new Id("ip","192.168.156.207")); // List<ACL> acls=new ArrayList<>(); // acls.add(acl); // zookeeper.create("/authTest","111".getBytes(),acls,CreateMode.PERSISTENT); zookeeper.create("/authTest1","111".getBytes(),ZooDefs.Ids.OPEN_ACL_UNSAFE,CreateMode.EPHEMERAL); byte[] bytes = zookeeper.getData("/authTest1",true,new Stat()); System.out.println(zookeeper.getState()); // CONNECTED System.out.println(new String(bytes,"utf-8")); // 111 //创建节点 String result=zookeeper.create("/node1","123".getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); zookeeper.getData("/node1",new ApiOperatorDemo(),stat); //增加一个 System.out.println("创建成功:"+result); //修改数据 zookeeper.setData("/node1","yin123".getBytes(),-1); Thread.sleep(2000); //修改数据 zookeeper.setData("/node1","yin234".getBytes(),-1); Thread.sleep(2000); //* //删除节点 zookeeper.delete("/node1",-1); Thread.sleep(2000); //创建节点和子节点 String path="/node11"; zookeeper.create(path,"123".getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE,CreateMode.PERSISTENT); TimeUnit.SECONDS.sleep(1); Stat stat=zookeeper.exists(path+"/node1",true); if(stat==null){//表示节点不存在 zookeeper.create(path+"/node1","123".getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE,CreateMode.PERSISTENT); TimeUnit.SECONDS.sleep(1); } //修改子路径 zookeeper.setData(path+"/node1","mic123".getBytes(),-1); TimeUnit.SECONDS.sleep(1); //获取指定节点下的子节点 /* List<String> childrens=zookeeper.getChildren("/node",true); System.out.println(childrens);*/ } public void process(WatchedEvent watchedEvent) { //如果当前的连接状态是连接成功的,那么通过计数器去控制 if(watchedEvent.getState()==Event.KeeperState.SyncConnected){ if(Event.EventType.None==watchedEvent.getType()&&null==watchedEvent.getPath()){ countDownLatch.countDown(); System.out.println(watchedEvent.getState()+"-->"+watchedEvent.getType()); }else if(watchedEvent.getType()== Event.EventType.NodeDataChanged){ try { System.out.println("数据变更触发路径:"+watchedEvent.getPath()+"->改变后的值:"+ zookeeper.getData(watchedEvent.getPath(),true,stat)); } catch (KeeperException e) { e.printStackTrace(); } catch (InterruptedException e) { e.printStackTrace(); } }else if(watchedEvent.getType()== Event.EventType.NodeChildrenChanged){//子节点的数据变化会触发 try { System.out.println("子节点数据变更路径:"+watchedEvent.getPath()+"->节点的值:"+ zookeeper.getData(watchedEvent.getPath(),true,stat)); } catch (KeeperException e) { e.printStackTrace(); } catch (InterruptedException e) { e.printStackTrace(); } }else if(watchedEvent.getType()== Event.EventType.NodeCreated){//创建子节点的时候会触发 try { System.out.println("节点创建路径:"+watchedEvent.getPath()+"->节点的值:"+ zookeeper.getData(watchedEvent.getPath(),true,stat)); } catch (KeeperException e) { e.printStackTrace(); } catch (InterruptedException e) { e.printStackTrace(); } }else if(watchedEvent.getType()== Event.EventType.NodeDeleted){//子节点删除会触发 System.out.println("节点删除路径:"+watchedEvent.getPath()); } System.out.println(watchedEvent.getType()); } } }
package com.example.springbootdemo.zookeeper; import org.apache.zookeeper.*; import org.apache.zookeeper.data.ACL; import org.apache.zookeeper.data.Id; import org.apache.zookeeper.data.Stat; import java.io.IOException; import java.util.ArrayList; import java.util.List; import java.util.concurrent.CountDownLatch; import java.util.concurrent.TimeUnit; public class AuthControlDemo implements Watcher{ private final static String CONNECTSTRING="192.168.156.207:2181"; private static CountDownLatch countDownLatch=new CountDownLatch(1); private static CountDownLatch countDownLatch2=new CountDownLatch(1); private static ZooKeeper zookeeper; private static Stat stat=new Stat(); public static void main(String[] args) throws IOException, InterruptedException, KeeperException { // zookeeper=new ZooKeeper(CONNECTSTRING, 5000, new AuthControlDemo()); // countDownLatch.await(); // // ACL acl=new ACL(ZooDefs.Perms.CREATE, new Id("digest","root:root")); // ACL acl2=new ACL(ZooDefs.Perms.CREATE, new Id("ip","192.168.1.1")); // // List<ACL> acls=new ArrayList<>(); // acls.add(acl); // acls.add(acl2); // zookeeper.create("/auth11","123".getBytes(),acls,CreateMode.PERSISTENT); // zookeeper.addAuthInfo("digest","root:root".getBytes()); // zookeeper.create("/auth1","123".getBytes(), ZooDefs.Ids.CREATOR_ALL_ACL, CreateMode.PERSISTENT); // zookeeper.create("/auth1/auth1-1","123".getBytes(), ZooDefs.Ids.CREATOR_ALL_ACL,CreateMode.EPHEMERAL); ZooKeeper zooKeeper1=new ZooKeeper(CONNECTSTRING, 5000, new AuthControlDemo()); countDownLatch.await(); // System.out.println(zooKeeper1.getData("/auth11",new AuthControlDemo(),null)); zooKeeper1.delete("/auth11",-1); // acl (create /delete /admin /read/write) //权限模式: ip/Digest(username:password)/world/super } public void process(WatchedEvent watchedEvent) { //如果当前的连接状态是连接成功的,那么通过计数器去控制 if(watchedEvent.getState()==Event.KeeperState.SyncConnected){ if(Event.EventType.None==watchedEvent.getType()&&null==watchedEvent.getPath()){ countDownLatch.countDown(); System.out.println(watchedEvent.getState()+"-->"+watchedEvent.getType()); } } } }
ZkClient:
package com.example.springbootdemo.zookeeper.zkclient; import org.I0Itec.zkclient.ZkClient; public class SessionDemo { private final static String CONNECTSTRING="192.168.156.207:2181"; public static void main(String[] args) { ZkClient zkClient=new ZkClient(CONNECTSTRING,4000); System.out.println(zkClient+" - > success"); } }
package com.example.springbootdemo.zookeeper.zkclient; import org.I0Itec.zkclient.IZkChildListener; import org.I0Itec.zkclient.IZkDataListener; import org.I0Itec.zkclient.ZkClient; import java.util.List; import java.util.concurrent.TimeUnit; public class ZkClientApiOperatorDemo { private final static String CONNECTSTRING="192.168.156.207:2181"; private static ZkClient getInstance(){ return new ZkClient(CONNECTSTRING,10000); } public static void main(String[] args) throws InterruptedException { ZkClient zkClient=getInstance(); //zkclient 提供递归创建父节点的功能 // zkClient.createPersistent("/zkclient/zkclient2/zkclient2-2/zkclient1-1-1",true); // System.out.println("success"); //删除节点 // zkClient.deleteRecursive("/zkclient"); //获取子节点 // List<String> list=zkClient.getChildren("/zkclient"); // System.out.println(list); //watcher zkClient.subscribeDataChanges("/node", new IZkDataListener() { @Override public void handleDataChange(String s, Object o) throws Exception { System.out.println("节点名称:"+s+"->节点修改后的值"+o); } @Override public void handleDataDeleted(String s) throws Exception { } }); zkClient.writeData("/node","node"); TimeUnit.SECONDS.sleep(2); // // zkClient.subscribeChildChanges("/node", new IZkChildListener() { // @Override // public void handleChildChange(String s, List<String> list) throws Exception { // // } // }); } }
<dependency> <groupId>org.apache.curator</groupId> <artifactId>curator-framework</artifactId> <version>2.11.0</version> </dependency> <dependency> <groupId>org.apache.curator</groupId> <artifactId>curator-recipes</artifactId> <version>2.11.0</version> </dependency>