Zookeeper 系列(四)ZKClient API

Zookeeper 系列(四)ZKClient API

环境准备:

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

一、ZKClient 基本用法

public class ZkClientBase {

    private static final String CONNECT_ADDR = "127.0.0.1:2181";
    private static final int SEESION_OUTTIME = 5 * 1000;
    
    public static void main(String[] args) {
        ZkClient zkClient = new ZkClient(new ZkConnection(CONNECT_ADDR), 10 * 1000);

        //1. create and delete
        zkClient.createEphemeral("/temp");
        zkClient.createPersistent("/super/c1", true);
        zkClient.deleteRecursive("/super");

        //2. 设置path和data,并且读取于节点和每个节点的内容
        zkClient.createPersistent("/super", "1234");
        zkClient.createPersistent("/super/c1", "c1");
        zkClient.createPersistent("/super/c2", "c2");

        List<String> list = zkClient.getChildren("/super");
        for (String path : list) {
            String rp = "/super/" + path;
            System.out.println(rp + ":" + zkClient.readData(rp).toString());
        }

        //3. 更新和判断节点是否存在
        zkClient.writeData("/super/c1", "新内容");
        System.out.println("/super/c1:" + zkClient.readData("/super/c1").toString());
        System.out.println("/super/c1:" + zkClient.exists("/super/c1"));
    }

}

一、ZKClient Watcher

(一)节点变化

import org.I0Itec.zkclient.IZkDataListener;
import org.I0Itec.zkclient.ZkClient;
import org.I0Itec.zkclient.ZkConnection;

/**
 * 监听节点变化
 * @author: leigang
 * @version: 2018-04-06
 */
public class zkClientWatcher2 {

    private static final String CONNECT_ADDR = "127.0.0.1:2181";
    private static final int SEESION_OUTTIME = 5 * 1000;
    
    public static void main(String[] args) throws InterruptedException {
        ZkClient zkClient = new ZkClient(new ZkConnection(CONNECT_ADDR), SEESION_OUTTIME);
        zkClient.deleteRecursive("/super");
        zkClient.createPersistent("/super");

        // 对父节点添加子节点变化的事件
        zkClient.subscribeDataChanges("/super", new IZkDataListener() {
            @Override
            public void handleDataChange(String dataPath, Object data) throws Exception {
                System.out.println("变量的节点为:" + dataPath + ",变更的内容为:" + data);
            }

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

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

}

(一)子节点变化

import org.I0Itec.zkclient.IZkChildListener;
import org.I0Itec.zkclient.ZkClient;
import org.I0Itec.zkclient.ZkConnection;

import java.util.List;

/**
 * 监听子节点变化
 * @author: leigang
 * @version: 2018-04-06
 */
public class zkClientWatcher {

    private static final String CONNECT_ADDR = "127.0.0.1:2181";
    private static final int SEESION_OUTTIME = 5 * 1000;
    
    public static void main(String[] args) throws InterruptedException {
        ZkClient zkClient = new ZkClient(new ZkConnection(CONNECT_ADDR), SEESION_OUTTIME);
        zkClient.deleteRecursive("/super");

        zkClient.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);
            }
        });

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

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


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

        // 修改不会监听
        zkClient.writeData("/super/c2", "c2新内容");
        Thread.sleep(1000);

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

}

posted on 2018-04-07 07:32  binarylei  阅读(385)  评论(0编辑  收藏  举报

导航