ZkClient简单操作

    ZkClient简单的操作demo,通过修改节点或节点数据,监控其数据的变化。

1、对数据节点的修改操作

public class ZKClientChanger {

    private static ZkClient zkClient;

    /***
     * 测试修改path数据
     */
    private static class ChangerThread extends Thread {

        @Override
        public void run() {
            if(!zkClient.exists("/root")) {
                //1、创建root节点 2、初始化节点数据 3、节点为永久性创建
                zkClient.create("/root", "root".getBytes(), CreateMode.PERSISTENT);
            }

            for(int i = 0; i < 50; i++) {
                zkClient.writeData("/root", "root" + String.valueOf(i));
                try {
                    Thread.sleep(5000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }

    }

    public static void main(String[] args) {

        //对zookeeper封装的client,zookeeper发布订阅是一次的性的,监控一次后会断开连接;
        //client超时过期时货自动创建zookeeper连接
        zkClient = new ZkClient("192.168.52.128:2181", 1000);

        new ChangerThread().start();
    }

}

2、监控数据节点的变化情况

public class ZKclientWatcher {

    private static ZkClient zkClient;

    private static int count = 0;

    /**
     * 测试监控path下的数据变化情况
     */
    private static class WatcherThread extends Thread {
        @Override
        public void run() {
            //监控path下的数据的变化
            zkClient.subscribeDataChanges("/root", new IZkDataListener() {
                @Override
                public void handleDataChange(String path, Object data) throws Exception {
                    System.out.println("handleDataChange===========>path:" + path + " data: " + data);
                    count++;
                }

                @Override
                public void handleDataDeleted(String path) throws Exception {
                    System.out.println("handleDataDeleted=========>path:" + path);
                }
            });

            //监控path下的节点的变化
            zkClient.subscribeChildChanges("/root", new IZkChildListener() {
                @Override
                public void handleChildChange(String parentPath, List<String> currentChilds) throws Exception {
                    // TODO 节点变化处理逻辑
                }
            });

            //监控连接状态的变化
            zkClient.subscribeStateChanges(new IZkStateListener() {
                @Override
                public void handleStateChanged(Watcher.Event.KeeperState state) throws Exception {
                    // TODO 监控zookeeper连接状态变化逻辑
                }

                @Override
                public void handleNewSession() throws Exception {
                    // TODO  监控zookeeper Session过期并创建新Session时的逻辑
                }

                @Override
                public void handleSessionEstablishmentError(Throwable error) throws Exception {
                    // TODO 监控连接失败,session无法重新创建时的逻辑
                }
            });

            //取消所有的订阅[unsubscribeChildChanges/unsubscribeDataChanges/unsubscribeStateChanges]
//            zkClient.unsubscribeAll();
        }
    }

    public static void main(String[] args) throws InterruptedException {
        zkClient = new ZkClient("192.168.52.128:2181", 2000);
        new WatcherThread().start();

        while (count < 10) {
            System.out.println("count : " + count);
            Thread.sleep(1000);
        }
    }

}

    主要依赖zkclient的包:

<dependency>
    <groupId>com.101tec</groupId>
    <artifactId>zkclient</artifactId>
    <version>0.7</version>
</dependency>


你可能感兴趣的:(ZkClient简单操作)