最新消息:请随时分享你的乐趣!

Zookeeper 3.4.x 客户端连接帮助包

技术博客 磊子 532浏览 0评论

这个zk帮助类包不错,操作方便简单

主页链接地址:

包地址: https://github.com/adyliu/zkclient
包文档:https://github.com/adyliu/zkclient/wiki/tutorial

maven中央仓库

    <dependency>
        <groupId>com.github.adyliu</groupId>
        <artifactId>zkclient</artifactId>
        <version>2.1.1</version>
    </dependency>

Demo如下

package com.test.zk;

import com.github.zkclient.IZkChildListener;
import com.github.zkclient.IZkClient;
import com.github.zkclient.ZkClient;

import java.util.List;

/**
 * Created by cocky on 2016/11/18.
 */
public class ZkTest {
    String root= "/test/tt1/nodes";
    public void doSth() throws InterruptedException {

        IZkClient zkClient = new ZkClient("bigdata1:2181");

        if(!zkClient.exists(root)){
            zkClient.createPersistent(root,true);
        }

        zkClient.createEphemeralSequential(root+"/A","this is a Node}".getBytes());

        zkClient.subscribeChildChanges(root, new IZkChildListener() {
            public void handleChildChange(String parentPath, List<String> currentChildren) throws Exception {
                System.out.println("child changed> "+parentPath);
                System.out.println("child changed> "+currentChildren);
            }
        });

        System.out.println("sleep....");
        Thread.sleep(10000);
        System.out.println("over.....");
        zkClient.close();

    }

    public static void main(String[] args) throws InterruptedException {
            new ZkTest().doSth();

    }
}


转载请注明:印迹. » Zookeeper 3.4.x 客户端连接帮助包

发表我的评论
取消评论

表情