2021-06-24

Zookeeper的原生api操作

pom文件引入

<dependencies> <dependency>  <groupId>org.apache.zookeeper</groupId>  <artifactId>zookeeper</artifactId>  <version>3.4.14</version> </dependency></dependencies>

几个状态和概念
节点创建的权限信息ACL 参数的类型

  • ANYONE_ID_UNSAFE : 表示任何⼈
  • AUTH_IDS :此ID仅可⽤于设置ACL。它将被客户机验证的ID替换。
  • OPEN_ACL_UNSAFE :这是⼀个完全开放的ACL(常⽤)-->world:anyone
  • CREATOR_ALL_ACL :此ACL授予创建者身份验证ID的所有权限
    节点的几种类型的枚举
  • PERSISTENT:持久节点
  • PERSISTENT_SEQUENTIAL:持久顺序节点
  • EPHEMERAL:临时节点
  • EPHEMERAL_SEQUENTIAL:临时顺序节点

zookeeper原生api创建会话操作

public class CreateSession implements Watcher { private static CountDownLatch countDownLatch = new CountDownLatch(1); public static void main(String[] args) throws IOException, InterruptedException {  ZooKeeper zooKeeper = new ZooKeeper("119.45.52.68:2181",6000,new CreateSession());  System.out.println(zooKeeper.getState());  countDownLatch.await();  System.out.println("zk session create success"); } @Override public void process(WatchedEvent watchedEvent) {  if (watchedEvent.getState()==Event.KeeperState.SyncConnected){   countDownLatch.countDown();  } }}

正常创建回话file

zookeeper原生api获取信息操作

public class getNoetNode implements Watcher { private static ZooKeeper zooKeeper; public static void main(String[] args) throws InterruptedException, IOException {  zooKeeper = new ZooKeeper("119.45.52.68:2181",10000,new getNoetNode());  //countDownLatch.await();  Thread.sleep(Integer.MAX_VALUE); } @Override public void process(WatchedEvent event) {  if(event.getType() ==Event.EventType.NodeChildrenChanged){//再次获取子节点数据,监听子节点变化   try {    List<String> children =      zooKeeper.getChildren(event.getPath(), true);    System.out.println(children);   } catch (KeeperException e) {    e.printStackTrace();   } catch (InterruptedException e) {    e.printStackTrace();   }  }  //当连接创建了,服务端发送给客户端SyncConnected事件  if(event.getState() == Event.KeeperState.SyncConnected){   try {//调⽤获取单个节点数据⽅法    getNodeData();    getChildren();   } catch (Exception e) {    e.printStackTrace();   }  }  System.out.println(); } private static void getNodeData() throws Exception {  byte[] data = zooKeeper.getData("/persistent/children", true,    null);  System.out.println(new String(data,"utf-8")); } public static void getChildren() throws Exception{  List<String> childrens = zooKeeper.getChildren("persistent",true);  System.out.println(childrens); }}

zookeeper原生api更新节点信息

public class UpdateNode implements Watcher { private static ZooKeeper zooKeeper; public static void main(String[] args) throws Exception{  zooKeeper = new ZooKeeper("119.45.52.68:2181",6000,new UpdateNode());  Thread.sleep(Integer.MAX_VALUE); } @Override public void process(WatchedEvent event) {  try {   byte[] before = zooKeeper.getData("/persistent/children",false,null);   System.out.println("修改前的值"+new String(before));   Stat stat = zooKeeper.setData("/persistent","客户端修改的内容".getBytes(),-1);   System.out.println(stat);   byte[] after = zooKeeper.getData("/persistent/children",false,null);   System.out.println("修改后的值"+new String(after));  }catch (Exception e){   e.printStackTrace();  } }}

zookeeper原生api删......

原文转载:http://www.shaoqun.com/a/825704.html

跨境电商:https://www.ikjzd.com/

易佰:https://www.ikjzd.com/w/1482

myshow:https://www.ikjzd.com/w/2235

跨境通网站:https://www.ikjzd.com/w/1329


pom文件引入<dependencies><dependency><groupId>org.apache.zookeeper</groupId><artifactId>zookeeper</artifactId><version>3.4.14</version></dependency>&l
为了上学我选择当他的情人 他却食言了:http://lady.shaoqun.com/a/48944.html
女生遭7人轮奸 22人赶场17人轮奸7人自首:http://lady.shaoqun.com/a/131851.html
少妇口述:老公不在 我和陌生男子激情做爱3次:http://lady.shaoqun.com/m/a/78156.html
了解Review的前世今生,实现10天1000个 VP Review堆积:https://www.ikjzd.com/tl/14034
A++所有模板及对应图片要求:https://www.ikjzd.com/tl/14045
亚马逊后台模板上传-常见模板填写方法:https://www.ikjzd.com/tl/14046
科学,因女性更美丽:http://lady.shaoqun.com/m/a/43464.html
男友扒了我的奶罩吻我的胸 男朋友摸进我内裤揉搓:http://lady.shaoqun.com/a/247552.html
扛老师那雪白的两条玉腿 巨大在她腿间进进出出:http://www.30bags.com/m/a/249915.html
深圳回响之家书展览地址+交通指南:http://www.30bags.com/a/453909.html
深圳回响之家书展览需要门票吗:http://www.30bags.com/a/453910.html
深圳水头沙海滩要门票吗:http://www.30bags.com/a/453911.html

No comments:

Post a Comment