Zookeeper JAVA 原生 API 超详细(三)

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接: https://blog.csdn.net/qq_34125999/article/details/100006612

1、pom文件

<dependencies>
    <!--zookeeper-->
    <!-- https://mvnrepository.com/artifact/org.apache.zookeeper/zookeeper-->
    <dependency>
        <groupId>org.apache.zookeeper</groupId>
        <artifactId>zookeeper</artifactId>
        <version>3.4.6</version>
    </dependency>
    <!--其它-->
    <dependency>
        <groupId>org.apache.commons</groupId>
        <artifactId>commons-lang3</artifactId>
        <version>3.8.1</version>
    </dependency>
    <dependency>
        <groupId>org.projectlombok</groupId>
        <artifactId>lombok</artifactId>
        <version>1.18.6</version>
    </dependency>
</dependencies>

2、创建会话

public class zookeeperTest {

    public static final String ZOOKEEPER_SERVER = "192.168.1.38:2181";

    public static void main(String[] args) throws IOException {
        //创建一个会话,url,超时时间,监听机制
        ZooKeeper zooKeeper = new ZooKeeper(ZOOKEEPER_SERVER, 5000, null);
        System.out.println(zooKeeper.getState());
    }

}

在这里插入图片描述
参数表:
在这里插入图片描述

3、创建节点

在这里插入图片描述
在这里插入图片描述
需要注意几点:
1)、zookeeper不支持递归创建,没有父节点,不支持建立子节点。
2)、关于权限控制,如果你的应用场景没有太高的权限要求,那么就不用关注acl这个参数。

public class zookeeperTest {

    public static final String ZOOKEEPER_SERVER = "192.168.1.38:2181";
    public static void main(String[] args) throws Exception {
        //创建一个会话,url,超时时间,监听机制
        ZooKeeper zooKeeper = new ZooKeeper(ZOOKEEPER_SERVER, 5000, null);
        //创建一个节点,url,data,无权限,持久性
        zooKeeper.create("/test1", "test1".getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
        System.out.println("############创建成功############");
    }
}

在这里插入图片描述
在这里插入图片描述

4、删除节点

在这里插入图片描述
在这里插入图片描述

public class zookeeperTest {
    public static final String ZOOKEEPER_SERVER = "192.168.1.38:2181";
    public static void main(String[] args) throws Exception {
        //创建一个会话,url,超时时间,监听机制
        ZooKeeper zooKeeper = new ZooKeeper(ZOOKEEPER_SERVER, 5000, null);
        zooKeeper.delete("/test1", 0);
    }
}

在这里插入图片描述

5、读取数据

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

public class zookeeperTest {

    public static final String ZOOKEEPER_SERVER = "192.168.1.38:2181";
    public static void main(String[] args) throws Exception {
        //创建一个会话,url,超时时间,监听机制
        ZooKeeper zooKeeper = new ZooKeeper(ZOOKEEPER_SERVER, 5000, null);
        zooKeeper.create("/list", "data".getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE,CreateMode.PERSISTENT );
        zooKeeper.create("/list/list1", "data".getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE,CreateMode.PERSISTENT );
        zooKeeper.create("/list/list2", "data".getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE,CreateMode.PERSISTENT );
        //获取/list下所有子节点,不注册监听
        List<String> children = zooKeeper.getChildren("/list", false);
        System.out.println(children);
    }
}

在这里插入图片描述

6、监听

在这里插入图片描述
常用监听事件:
EventType.NodeCreated : 节点创建事件类型
EventType.NodeDeleted : 节点被删除
EventType.NodeDataChanged : 节点被修改
EventType.None : 客户端与服务器成功建立会话
EventType.NodeChildrenChanged : 子节点列表发生变更

public class zookeeperTest {

    public static final String ZOOKEEPER_SERVER = "192.168.1.38:2181";
    private static CountDownLatch countDownLatch = new CountDownLatch(1);

    public static void main(String[] args) throws Exception {
        //创建一个会话,url,超时时间,监听机制
        ZooKeeper zooKeeper = new ZooKeeper(ZOOKEEPER_SERVER, 5000, new Watcher() {
            //绑定监听事件
            public void process(WatchedEvent watchedEvent) {
                if (watchedEvent.getState() == Event.KeeperState.SyncConnected) {
                    System.out.println("连接成功");
                    countDownLatch.countDown();
                }
                System.out.println(watchedEvent.getType());
            }
        });

        countDownLatch.await();
        zooKeeper.create("/list5", "test".getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
        zooKeeper.getChildren("/list5", true);
        zooKeeper.create("/list5/list1", "test".getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);

    }
}

在这里插入图片描述

7、获取节点内容

在这里插入图片描述

public class zookeeperTest {

    public static final String ZOOKEEPER_SERVER = "192.168.1.38:2181";
    private static CountDownLatch countDownLatch = new CountDownLatch(1);

    public static void main(String[] args) throws Exception {
        //创建一个会话,url,超时时间,监听机制
        ZooKeeper zooKeeper = new ZooKeeper(ZOOKEEPER_SERVER, 5000, new Watcher() {
            //绑定监听事件
            public void process(WatchedEvent watchedEvent) {
                if (watchedEvent.getState() == Event.KeeperState.SyncConnected) {
                    countDownLatch.countDown();
                }
                System.out.println(watchedEvent.getType());
            }
        });
        countDownLatch.await();
        Stat stat = new Stat();
        //设置监听事件
        zooKeeper.getData("/list", true,  stat);
        //修改值
        zooKeeper.setData("/list", "lalal".getBytes(), -1);
    }
}

在这里插入图片描述

8、检查节点是否存在

在这里插入图片描述

public class zookeeperTest {

    public static final String ZOOKEEPER_SERVER = "192.168.1.38:2181";
    private static CountDownLatch countDownLatch = new CountDownLatch(1);

    public static void main(String[] args) throws Exception {
        //创建一个会话,url,超时时间,监听机制
        ZooKeeper zooKeeper = new ZooKeeper(ZOOKEEPER_SERVER, 5000, new Watcher() {
            //绑定监听事件
            public void process(WatchedEvent watchedEvent) {
                if (watchedEvent.getState() == Event.KeeperState.SyncConnected) {
                    countDownLatch.countDown();
                }
                System.out.println(watchedEvent.getType());
            }
        });
        countDownLatch.await();
        zooKeeper.exists("/test", true);
        zooKeeper.create("/test", "data".getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
        zooKeeper.delete("/test", -1);
    }
}

在这里插入图片描述

9、ZkClient

9.1、pom

 <!--zookeeper-->
        <!-- https://mvnrepository.com/artifact/org.apache.zookeeper/zookeeper-->
        <dependency>
            <groupId>org.apache.zookeeper</groupId>
            <artifactId>zookeeper</artifactId>
            <version>3.4.6</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/com.101tec/zkclient-->
        <dependency>
            <groupId>com.101tec</groupId>
            <artifactId>zkclient</artifactId>
            <version>0.10</version>
        </dependency>

9.2、demo

public class SessionDemo {

    private final static String CONNECTIONSTR = "192.168.1.38:2181";

    public static void main(String[] args) throws InterruptedException {

        ZkClient zkClient = new ZkClient(CONNECTIONSTR, 4000);
        //递归创建父节点
        zkClient.createPersistent("/test/testa",true);
        //递归删除
        //zkClient.deleteRecursive("/test");
        //获取子节点
        List<String> children = zkClient.getChildren("/test");
        System.out.println(children);
        //监听test节点变化时
        zkClient.subscribeDataChanges("/test", new IZkDataListener() {
            public void handleDataChange(String s, Object o) throws Exception {
                System.out.println("节点名称: " + s + "-> 节点修改后的值为" + o);
            }
            public void handleDataDeleted(String s) throws Exception {
            }
        });
        zkClient.writeData("/test", "node1");
        zkClient.writeData("/test", "value");
        TimeUnit.SECONDS.sleep(2);
        //当节点变化时
        zkClient.subscribeChildChanges("/test", new IZkChildListener() {
            public void handleChildChange(String s, List<String> list) throws Exception {
                System.out.println("节点名称:" + s + "->" + "当前节点列表: " + list);
            }
        });
        zkClient.delete("/test/testa");
        TimeUnit.SECONDS.sleep(2);
    }

}

在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/qq_34125999/article/details/100006612