ZkClient

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/vbirdbest/article/details/82712453

一:简介

zkclient 是一种开源的zookeeper客户端,是对原生的客户端的一种封住, 和原生api相比最大的特点是将watch和操作分离了,可以看到zkclient对节点的操作都没有了watch参数。

public class ZkClient implements Watcher {

    public ZkClient(IZkConnection connection, int connectionTimeout);

    // 创建临时节点
    public void createEphemeral(String path);

    // 创建持久化节点(支持递归创建节点), 此时该节点没有值
    public void createPersistent(String path, boolean createParents);

    public void createPersistent(String path, Object data); 

    // 递归删除节点
    public boolean deleteRecursive(String path);

    // 获取子节点
    public List<String> getChildren(String path);

    // 获取节点值
    public <T> T readData(String path);

    // 修改节点数据
    public void writeData(String path, Object object);

    public boolean exists(String path);

    // 订阅子节点的变化(只需要订阅一次,以后每次发生变化都能接收到,而原生api需要每次去监控)
    // 监听当前节点和子节点的新增和删除,不监听修改操作
    public List<String> subscribeChildChanges(String path, IZkChildListener listener);
    public void unsubscribeChildChanges(String path, IZkChildListener childListener);

    public void subscribeDataChanges(String path, IZkDataListener listener);
    public void unsubscribeDataChanges(String path, IZkDataListener dataListener);

    public void subscribeStateChanges(IZkStateListener listener);
    public void unsubscribeStateChanges(IZkStateListener stateListener);

    public void close() throws ZkInterruptedException;
}

二:基本示例

<dependency>
    <groupId>com.101tec</groupId>
    <artifactId>zkclient</artifactId>
    <version>0.10</version>
</dependency>
public class ZKClientTest {
    /** zookeeper地址 */
    static final String connectString = "127.0.0.1:2181,127.0.0.1:2182,127.0.0.1:2183";
    /** session超时时间 */
    static final int sessionTimeout = 5000;

    public static void main(String[] args) throws InterruptedException {
        ZkClient zkClient = new ZkClient(new ZkConnection(connectString), 10000);

        // 创建一个临时节点,没有值或者值为null
        zkClient.createEphemeral("/temp");
        // 递归创建一个节点
        zkClient.createPersistent("/super/c1/c2", true);
        // 给节点赋值
        zkClient.writeData("/super", "super");
        zkClient.writeData("/super/c1", "super c1");
        zkClient.writeData("/super/c1/c2", "super c1 c2");

        // 创建一个节点并赋值(该操作不支持创建一个递归路径并赋值)
        zkClient.createPersistent("/test", "test");


        List<String> children = zkClient.getChildren("/super");
        children.forEach(path -> {
            System.out.println(path);
            String fullPath = "/super/" + path;
            String data = zkClient.readData(fullPath);
            System.out.println(fullPath + "=" + data);

        });

        boolean exists = zkClient.exists("/super");
        System.out.println(exists);

        zkClient.deleteRecursive("/super");

        Thread.sleep(10000);
        zkClient.close();
    }
}

三:Watch

通过订阅子节点改变(subscribeChildChanges)、订阅数据改变(subscribeDataChanges)等来监控对zookeeper的操作。只需要订阅一次,便可永久使用,而原生客户端需要反复注册,原生客户端的监控是一次性的。

public class ZKClientWatcherTest {
    /** zookeeper地址 */
    static final String connectString = "127.0.0.1:2181,127.0.0.1:2182,127.0.0.1:2183";
    /** session超时时间 */
    static final int sessionTimeout = 5000;

    public static void main(String[] args) throws InterruptedException {
        ZkClient zkClient = new ZkClient(new ZkConnection(connectString), 10000);
        // 监听当前节点和子节点的新增和删除,不监听修改操作
        zkClient.subscribeChildChanges("/super", new IZkChildListener() {
            @Override
            public void handleChildChange(String parentPath, List<String> currentChild) throws Exception {
                System.out.println("--------------handleChildChange---------------");
                System.out.println("parentPath=" + parentPath);
                System.out.println("currentChild=" + currentChild);

            }
        });

        zkClient.subscribeDataChanges("/super", new IZkDataListener() {
            @Override
            public void handleDataChange(String path, Object data) throws Exception {
                System.out.println("修改节点 path=" + path + ", value=" + data);
            }

            @Override
            public void handleDataDeleted(String path) throws Exception {
                System.out.println("删除的节点 path" + path);
            }
        });

        Thread.sleep(3000);
        zkClient.createPersistent("/super");
        Thread.sleep(1000);


        zkClient.writeData("/super", "super new value");
        Thread.sleep(1000);

        zkClient.delete("/super");
        Thread.sleep(1000);

        zkClient.close();
    }
}

这里写图片描述

猜你喜欢

转载自blog.csdn.net/vbirdbest/article/details/82712453