zookeeper使用(三)--Curator客户端操作zookeeper

一、简介

Curator是Netflix公司开源的zookeeper客户端,与之类似的还有zkClient客户端。

zkClient是在zookeeper原生api的基础上包装而来,因此实现的功能更多是基于zookeeper原生api,扩展性方便不多。

而Curtor目前是apache的顶级项目,提供了更易用的,适用于各种业务的api(如锁服务等)。

二、常用操作实例

这里就不多讲,直接上代码,代码里见注释,更详情的可以直接点开看源码。

1、添加maven依赖

<dependency>
    <groupId>org.apache.zookeeper</groupId>
    <artifactId>zookeeper</artifactId>
    <version>3.4.6</version>
</dependency>
<dependency>
    <groupId>org.apache.curator</groupId>
    <artifactId>curator-framework</artifactId>
    <version>4.0.0</version>
</dependency>
<dependency>
    <groupId>org.apache.curator</groupId>
    <artifactId>curator-client</artifactId>
    <version>4.0.0</version>
</dependency>
<dependency>
    <groupId>org.apache.curator</groupId>
    <artifactId>curator-recipes</artifactId>
    <version>4.0.0</version>
</dependency>

2、代码实例

import org.apache.curator.RetryPolicy;
import org.apache.curator.framework.CuratorFramework;
import org.apache.curator.framework.CuratorFrameworkFactory;
import org.apache.curator.framework.recipes.cache.PathChildrenCache;
import org.apache.curator.framework.recipes.cache.PathChildrenCacheListener;
import org.apache.curator.retry.ExponentialBackoffRetry;
import org.apache.zookeeper.CreateMode;
import org.apache.zookeeper.data.Stat;

import java.util.Objects;

public class ZookeeperCuratorMain {
    public static void main(String[] args) throws Exception {
        //配置连接
        RetryPolicy retryPolicy = new ExponentialBackoffRetry(1000, 3);
        CuratorFramework client = CuratorFrameworkFactory.builder()
                .connectString("192.168.0.107:2181")
                .sessionTimeoutMs(5000)
                .retryPolicy(retryPolicy)
                .build();

        String rootPath = "/zkStudy";
        String path = "/zkStudy/studyChildNode";
        String val = "this.is a zk node val";

        //启动
        client.start();

        //监听
        PathChildrenCache cache = new PathChildrenCache(client, rootPath, true);
        cache.start();
        PathChildrenCacheListener listener = (listenerClient, event) -> {
            System.out.println("event happen : " + event.getType());
            if (null != event.getData()) {
                System.out.println("node path : " + event.getData().getPath() + "   node val : " + new String(event.getData().getData()));
            }
        };
        cache.getListenable().addListener(listener);

        //创建
        client.create()
                .creatingParentContainersIfNeeded()
                .withMode(CreateMode.PERSISTENT)
                .forPath(path, val.getBytes());

        //读取
        Stat stat = new Stat();
        byte[] dataBytes = client.getData().storingStatIn(stat).forPath(path);
        String readVal = new String(dataBytes);
        System.out.println(readVal);

        //更新
        String newVal = "this ia a new zk node val";
        client.setData().forPath(path, newVal.getBytes());

        //判断是否存在
        Stat existFlagStat = client.checkExists().forPath("/notExistPath");
        System.out.println("node /notExistPath is exist : " + !Objects.isNull(existFlagStat));

        //删除
        client.delete()
                .deletingChildrenIfNeeded()
                .forPath(rootPath);

    }
}

输出:

event happen : CONNECTION_RECONNECTED
this.is a zk node val
event happen : CHILD_ADDED
node path : /zkStudy/studyChildNode   node val : this.is a zk node val
node /notExistPath is exist : false
event happen : CHILD_UPDATED
node path : /zkStudy/studyChildNode   node val : this ia a new zk node val
event happen : CHILD_REMOVED
node path : /zkStudy/studyChildNode   node val : this ia a new zk node val



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值