2021年6月25日星期五

Zookeeper客户端zkClient和curator的操作

zkClient操作

基本增删改查代码如下

public class createSession { public static void main(String[] args) {  ZkClient zkClient = new ZkClient("119.45.52.68:2181");  System.out.println("connect success"); }}public class CreateNode { public static void main(String[] args) {  ZkClient zkClient = new ZkClient("119.45.52.68:2181");  System.out.println("connect success");  //true代表可以递归创建目录  zkClient.createPersistent("/zkclient/persistent/children",true);  System.out.println("create node success"); }}public class DeleteNode { public static void main(String[] args) {  ZkClient zkClient = new ZkClient("119.45.52.68:2181");  zkClient.deleteRecursive("/zkclient");  System.out.println("success delete node"); }}public class GetChildrenChanged { public static void main(String[] args) throws Exception{  ZkClient zkClient = new ZkClient("119.45.52.68:2181");  zkClient.createPersistent("/zkClient");  Thread.sleep(1000);  List<String> children = zkClient.getChildren("/zkClient");  System.out.println(children);  zkClient.subscribeChildChanges("/zkClient", new IZkChildListener() {   @Override   public void handleChildChange(String parentPath, List<String> currentChilds) throws Exception {    System.out.println("父路径"+parentPath+"当前子路径的"+currentChilds);   }  });  zkClient.createPersistent("/zkClient/child1");  Thread.sleep(1000);  zkClient.delete("/zkClient/child1");  Thread.sleep(Integer.MAX_VALUE); }}public class GetNodeData { public static void main(String[] args) throws Exception{  String path = "/zkclient-node";  ZkClient zkClient = new ZkClient("119.45.52.68:2181");  boolean exist = zkClient.exists(path);  if (!exist){   zkClient.createEphemeral(path,"123");  }  zkClient.subscribeDataChanges(path, new IZkDataListener() {   @Override   public void handleDataChange(String dataPath, Object data) throws Exception {    System.out.println(dataPath+"节点内容被更新"+data);   }   @Override   public void handleDataDeleted(String dataPath) throws Exception {    System.out.println(dataPath+"节点内容被删除");   }  });  Object o = zkClient.readData(path);  System.out.println(o);  zkClient.writeData(path,"4567");  Thread.sleep(2000);  zkClient.delete(path);  Thread.sleep(2000); }}

curator操作api使用

public class CreateSession { public static void main(String[] args) {  //curator第一种创建回话方式  RetryPolicy retryPolicy = new ExponentialBackoffRetry(1000,3);  CuratorFramework curatorFramework = CuratorFrameworkFactory.newClient("119.45.52.68:2181",    5000,3000,retryPolicy);  curatorFramework.start();  System.out.println("create session success");  //第二种方式  CuratorFramework client =CuratorFrameworkFactory.builder()    .connectString("119.45.52.68:2181")    .sessionTimeoutMs(5000)    .sessionTimeoutMs(3000)    .retryPolicy(retryPolicy)    .namespace("base")    .build();  client.start();  System.out.println("createe session2 success "); }}public class CreateNode { public static void main(String[] args) throws Exception{  RetryPolicy retryPolicy = new ExponentialBackoffRetry(1000,3);  CuratorFramework client = CuratorFrameworkFactory.builder()    .connectString("119.45.52.68:2181")    .sessionTimeoutMs(5000)    .sessionTimeoutMs(3000)    .retryPolicy(retryPolicy)    //.namespace("base")    .build();  client.start();  String path = "/curator/child1";  client.create().creatingParentContainersIfNeeded()    .withMode(CreateMode.PERSISTENT)    .forPath(path,"init".getBytes());  System.out.println("success create node"); }}public class DeleteNode { public static void main(String[] args) throws Exception{  RetryPolicy retryPolicy = new ExponentialBackoffRetry(1000,3);  CuratorFramework client = CuratorFrameworkFactory.builder()    .connectString("119.45.52.68:2181")    .sessionTimeoutMs(5000)    .sessionTimeoutMs(3000)    .retryPolicy(retryPolicy)    //.namespace("base")    .build();  client.start();  String path = "/curator";  client.delete().deletingChildrenIfNeeded().withVersion(-1).forPath(path);  System.out.println("delete node success "); }}public class GetNodeData { public static void main(String[] args) throws Exception{  RetryPolicy retryPolicy = new ExponentialBackoffRetry(1000,3);  CuratorFramework client = CuratorFrameworkFactory.builder()    .connectString("119.45.52.68:2181")    .sessionTimeoutMs(5000)    .sessionTimeoutMs(3000)    .retryPolicy(retryPolicy)    //.namespace("base")    .build();  client.start();  String path = "/curator/child1";  client.create().creatingParentContainersIfNeeded()    .withMode(CreateMode.PERSISTENT)    .forPath(path,"init".getBytes());  System.out.println("success create node");  Stat stat = new Stat();  byte [] data = client.getData().storingStatIn(stat).forPath(path);  System.out.println("数据为"+n......

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

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

lastpass:https://www.ikjzd.com/w/846

easybuy:https://www.ikjzd.com/w/2162

Sunrate:https://www.ikjzd.com/w/2685


zkClient操作基本增删改查代码如下publicclasscreateSession{publicstaticvoidmain(String[]args){ZkClientzkClient=newZkClient("119.45.52.68:2181");System.out.println("connectsuccess");}}publicclass
惠州铁泉温泉怎么样?:http://www.30bags.com/a/398196.html
让办公室恋情幸福的五大法则:http://lady.shaoqun.com/m/a/114541.html
老师把腿张大点我要进去 老师你夹得我好紧好爽:http://lady.shaoqun.com/m/a/247511.html
我和哥哥有过这样的关系,背叛了我们的婚姻却不知道怎么结束!:http://lady.shaoqun.com/a/387430.html
教你如何成为一个有钱的女人:http://lady.shaoqun.com/a/387431.html
贝贝母婴网:https://www.ikjzd.com/w/1321
【大学生办日本签证】日本简化75所中国大学学生签证名单:http://www.30bags.com/a/425689.html
【大栅栏好玩吗】北京前门大栅栏,大栅栏有什么好玩的:http://www.30bags.com/a/433851.html
【带娃必看】上海迪士尼最适合带孩子玩的项目TOP8,上海迪士尼亲子游项目攻略:http://www.30bags.com/a/427063.html
口述:相亲女友竟往我茶杯里吐口水(2/2):http://lady.shaoqun.com/a/42006.html
给两个女同学开嫩苞 乖把腿抬高点一进一出:http://lady.shaoqun.com/m/a/247949.html
发现亲戚出轨怎么办?:http://lady.shaoqun.com/a/387429.html

没有评论:

发表评论