Open Source, Open Future!
  menu
107 文章
ღゝ◡╹)ノ❤️

zookeeper---客户端---原生API

引入依赖

        <dependency>
            <groupId>org.apache.zookeeper</groupId>
            <artifactId>zookeeper</artifactId>
            <version>3.4.5</version>
        </dependency>

创建连接

    private static CountDownLatch connectedSemaphore = new CountDownLatch(1);
    private String connectString = "192.168.1.201:2181";
    private int sessionTimeout = 3000;
    private ZooKeeper client;

    @Before
    public void init() throws Exception {
        client = new ZooKeeper(connectString, sessionTimeout, new Watcher() {
            @Override
            public void process(WatchedEvent watchedEvent) {
                if (KeeperState.SyncConnected == watchedEvent.getState()) {
                    connectedSemaphore.countDown();
                }
            }
        });
        connectedSemaphore.await();
        System.out.println("连接创建成功");
    }

创建节点

    @Test
    public void createTest() throws Exception {
        // 创建持久节点
        String node1 = client.create("/c", "3".getBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
        System.out.println("创建节点: " + node1);
        // 创建持久顺序节点
        String node2 = client.create("/d", "4".getBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT_SEQUENTIAL);
        System.out.println("创建节点: " + node2);
        // 创建临时节点
        String node3 = client.create("/e", "5".getBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.EPHEMERAL);
        System.out.println("创建节点: " + node3);
        // 创建临时顺序节点
        String node4 = client.create("/f", "6".getBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.EPHEMERAL_SEQUENTIAL);
        System.out.println("创建节点: " + node4);
    }

输出结果:
image.png
查询节点情况:
image.png
因为/e和/f0000000015两个节点是临时节点,所以客户端连接断开后会自动被剔除。

获取子节点

    @Test

    public void getChildrenTest() throws Exception {

        // 获取子节点

        List<String> childrenList = client.getChildren("/c", false);
        System.out.println(childrenList);
    }

获取子节点(动态刷新)

    @Test
    public void getChildren2Test() throws Exception {
        // 获取子节点
        List<String> childrenList = client.getChildren("/c", true);
        System.out.println(childrenList);
        Thread.sleep(Integer.MAX_VALUE);
    }

修改init方法为以下内容

    @Before
    public void init() throws Exception {
        client = new ZooKeeper(connectString, sessionTimeout, new Watcher() {
            @Override
            public void process(WatchedEvent watchedEvent) {
                System.out.println(watchedEvent);
                if (KeeperState.SyncConnected == watchedEvent.getState()) {
                    if (EventType.None == watchedEvent.getType() && null == watchedEvent.getPath()) {
                        connectedSemaphore.countDown();
                    } else if (EventType.NodeChildrenChanged == watchedEvent.getType()) {
                        try {
                            List<String> childrenList = client.getChildren(watchedEvent.getPath(), true);
                            System.out.println("------子节点列表发生变化------");
                            System.out.println(childrenList);
                            System.out.println("------------");
                        } catch (Exception e) {
                        }
                    }
                }
            }
        });
        connectedSemaphore.await();
        System.out.println("连接创建成功");
    }

1、第一次启动时,输出以下内容:
image.png
2、当创建节点/c/c1时
image.png
自动输出以下内容:
image.png
3、当删除节点/c/c1时,自动输出以下内容:
image.png

查询节点数据

    @Test
    public void getDataTest() throws Exception {
        // 查询节点数据
        byte[] data = client.getData("/c",false, null);
        System.out.println(new String(data));
    }

删除节点

    @Test
    public void deleteTest() throws Exception {
        // 删除节点
        client.delete("/d0000000013", -1);
        System.out.println("删除节点完成");
    }