• 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("数据为"+new String(data));
        }
    }
    
    
    public class UpdateNodeData {
        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("数据为"+new String(data));
    
            int version = client.setData().withVersion(stat.getVersion()).forPath(path).getVersion();
            System.out.println("update node "+path+" version "+version);
            client.setData().withVersion(stat.getVersion()).forPath(path).getAversion();
        }
    }
    
    

    最后一行代码会报错,原因是不能设置同样的数据版本。

    除了上述的两种zkclient和curatir的常用操作。还有一些其他操作都可以去尝试
    代码的地址为https://github.com/zhendiao/deme-code/tree/main/zk

    欢迎搜索关注本人与朋友共同开发的微信面经小程序【大厂面试助手】和公众号【微瞰技术】,以及总结的分类面试题https://github.com/zhendiao/JavaInterview

    file
    file

  • 相关阅读:
    11.14 mii-tool:管理网络接口的状态
    11.15 dmidecode:查询系统硬件信息
    11.11 ntsysv:管理开机服务
    HDU 2476 String painter 刷字符串(区间DP)
    HDU 1085 Holding Bin-Laden Captive! 活捉本拉登(普通型母函数)
    母函数的应用
    HDU 1028 Ignatius and the Princess III伊格和公主III(AC代码)母函数
    HDU 1059 Dividing 分配(多重背包,母函数)
    HDU 2955 Robberies抢劫案(01背包,变形)
    HDU 1011 Starship Troopers星河战队(树形dp)
  • 原文地址:https://www.cnblogs.com/zhendiao/p/14932889.html
Copyright © 2020-2023  润新知