【ZooKeeper学习】2. 第三方Client

一. ZkClient
 
 ZkClient是一个在ZooKeeper原生API接口之上进行了包装的开源的ZooKeeper客户端。
 
1、Maven  引用
<!-- https://mvnrepository.com/artifact/com.github.sgroschupf/zkclient -->
<dependency>
  <groupId>com.github.sgroschupf</groupId>
  <artifactId>zkclient</artifactId>
  <version>0.1</version>
</dependency>

 

 
2、创建session
public class CreateSessionDemo {
    public static void main(String[] args) {
        ZkClient zkClient = new ZkClient("127.0.0.1:2181",5000);
        System.out.println("ZooKeeper session established");
    }
}

 

3、创建节点
public class CreateNodeDemo {
    public static void main(String[] args) {
        ZkClient zkClient = new ZkClient("127.0.0.1:2181",5000);
        String path = "/zk-test/c1";
        zkClient.createPersistent(path,true);
    }
}

 

 
4、获取子节点
*/
public class GetChildrenDemo {
    public static void main(String[] args) throws InterruptedException {
        String path = "/zk-test";
        ZkClient zkClient = new ZkClient("127.0.0.1:2181", 5000);
        zkClient.subscribeChildChanges(path, new IZkChildListener() {
            @Override
            public void handleChildChange(String parentPath, List<String> currentChilds) throws Exception {
                System.out.println(parentPath + "'s child changed, currentChilds:" + currentChilds);
            }
        });

        zkClient.createPersistent(path);
        Thread.sleep(1000);
        zkClient.createPersistent(path+"/c1");
        Thread.sleep(1000);
        zkClient.delete(path+"/c1");
        Thread.sleep(1000);
        zkClient.deleteRecursive(path);
        Thread.sleep(10000);

    }
}

 

 
5、删除节点
public class DeleteNodeDemo {
    public static void main(String[] args) {
        ZkClient zkClient = new ZkClient("127.0.0.1:2181",5000);
        String path = "/zk-test";
        zkClient.deleteRecursive(path);
    }
}

 

 
 
二、Curator
    Curator是一个在ZooKeeper原生API接口之上进行了包装的开源的提供Fluent风格的ZooKeeper客户端。提供ZooKeeper各种应用场景的抽象封装
 
1、Maven 引用
<!-- https://mvnrepository.com/artifact/org.apache.curator/curator-framework -->
<dependency>
  <groupId>org.apache.curator</groupId>
  <artifactId>curator-framework</artifactId>
  <version>4.0.1</version>
</dependency>

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值