ZKClientAPI讲解、CuratorAPI讲解、分布式锁讲解、实现多Watcher

x33g5p2x  于2021-03-14 发布在 Zookeeper  
字(3.4k)|赞(0)|评价(0)|浏览(472)

一、ZKClient的基本使用

        public class ZkClientBase {
        /** zookeeper地址 */
        static final String CONNECT_ADDR = "192.168.1.171:2181,192.168.1.172:2181,192.168.1.173:2181";
        /** session超时时间 */
        static final int SESSION_OUTTIME = 5000;//ms 
        public static void main(String[] args) throws Exception {
            ZkClient zkc = new ZkClient(new ZkConnection(CONNECT_ADDR), 5000);
            //1. create and delete方法 
            zkc.createEphemeral("/temp");
            zkc.createPersistent("/super/c1", true);
            Thread.sleep(10000);
            zkc.delete("/temp");
            zkc.deleteRecursive("/super");		
            //2. 设置path和data 并且读取子节点和每个节点的内容
            zkc.createPersistent("/super", "1234");
            zkc.createPersistent("/super/c1", "c1内容");
            zkc.createPersistent("/super/c2", "c2内容");
            List<String> list = zkc.getChildren("/super");
            for(String p : list){
                System.out.println(p);
                String rp = "/super/" + p;
                String data = zkc.readData(rp);
                System.out.println("节点为:" + rp + ",内容为: " + data);
            }
            3. 更新和判断节点是否存在
            zkc.writeData("/super/c1", "新内容");
            System.out.println(zkc.readData("/super/c1"));
            System.out.println(zkc.exists("/super/c1"));

            4.递归删除/super内容
            zkc.deleteRecursive("/super");
        }
    }

二、zkClient使用 subscribeChildChanges

 subscribeChildChanges 方法
 参数一:path 路径
 参数二、时间了IZKChildListener接口的类(如:实例化IZkChildListener类)
 只需要重写其handleChildChange(String parentPath,List<String> currentChilds)
 其中parentPath为最新的子节点列表(相对路径)
 IZkChildListener事件说明针对于下面三个时间触发
 新增子节点、减少子节点、删除节点

代码实现

     public class ZkClientWatcher1 {

    /** zookeeper地址 */
    static final String CONNECT_ADDR = "192.168.1.171:2181,192.168.1.172:2181,192.168.1.173:2181";
    /** session超时时间 */
    static final int SESSION_OUTTIME = 5000;//ms 


    public static void main(String[] args) throws Exception {
        ZkClient zkc = new ZkClient(new ZkConnection(CONNECT_ADDR), 5000);

        //对父节点添加监听子节点变化。
        zkc.subscribeChildChanges("/super", new IZkChildListener() {
            @Override
            public void handleChildChange(String parentPath, List<String> currentChilds) throws Exception {
                System.out.println("parentPath: " + parentPath);
                System.out.println("currentChilds: " + currentChilds);
            }
        });

        Thread.sleep(3000);

        zkc.createPersistent("/super");
        Thread.sleep(1000);

        zkc.createPersistent("/super" + "/" + "c1", "c1内容");
        Thread.sleep(1000);

        zkc.createPersistent("/super" + "/" + "c2", "c2内容");
        Thread.sleep(1000);		

        zkc.delete("/super/c2");
        Thread.sleep(1000);	

        zkc.deleteRecursive("/super");
        Thread.sleep(Integer.MAX_VALUE);


    }
}

注意

      WatchedEvent : 包含三部分:通知状态、时间类型、节点路径,有了这个路径就可获取到
      不监听数据的更新

三、subscribeDataChanges

    public class ZkClientWatcher2 {

    /** zookeeper地址 */
    static final String CONNECT_ADDR = "192.168.1.171:2181,192.168.1.172:2181,192.168.1.173:2181";
    /** session超时时间 */
    static final int SESSION_OUTTIME = 5000;//ms 


    public static void main(String[] args) throws Exception {
        ZkClient zkc = new ZkClient(new ZkConnection(CONNECT_ADDR), 5000);

        zkc.createPersistent("/super", "1234");

        //对父节点添加监听子节点变化。
        zkc.subscribeDataChanges("/super", new IZkDataListener() {
            @Override
            public void handleDataDeleted(String path) throws Exception {
                System.out.println("删除的节点为:" + path);
            }

            @Override
            public void handleDataChange(String path, Object data) throws Exception {
                System.out.println("变更的节点为:" + path + ", 变更内容为:" + data);
            }
        });

        Thread.sleep(3000);
        zkc.writeData("/super", "456", -1);
        Thread.sleep(1000);

        zkc.delete("/super");
        Thread.sleep(Integer.MAX_VALUE);
    }
}

相关文章