Curator客户端的使用

Curator是Netflix公司开源的一套zookeeper客户端框架,解决了很多Zookeeper客户端非常底层的细节开发工作,包括连接重连、反复注册Watcher和NodeExistsException异常等等。Patrixck Hunt(Zookeeper)以一句“Guava is to Java that Curator to Zookeeper”给Curator予高度评价。

1.连接zookeeper

package com.stu.demo.zooker;

import org.apache.curator.framework.CuratorFramework;
import org.apache.curator.framework.CuratorFrameworkFactory;
import org.apache.curator.retry.ExponentialBackoffRetry;
import org.apache.zookeeper.CreateMode;
import org.apache.zookeeper.data.Stat;

public class CuratorDemo {
    public final static String CONNECTION_STR="192.168.0.128:2181,192.168.0.129:2181,192.168.0.130:2181";

    public static void main(String[] args) throws Exception {
        CuratorFramework curatorFramework = CuratorFrameworkFactory.builder()
                .connectString(CONNECTION_STR).sessionTimeoutMs(100000)
                .retryPolicy(new ExponentialBackoffRetry(1000,3))
                .namespace("curator").build();
        curatorFramework.start();
        curatorFramework.close();
    }
}

2.增删改查api使用

package com.stu.demo.zooker;

import org.apache.curator.framework.CuratorFramework;
import org.apache.curator.framework.CuratorFrameworkFactory;
import org.apache.curator.retry.ExponentialBackoffRetry;
import org.apache.zookeeper.CreateMode;
import org.apache.zookeeper.data.Stat;

public class CuratorDemo {
    public final static String CONNECTION_STR="192.168.0.128:2181,192.168.0.129:2181,192.168.0.130:2181";

    public static void main(String[] args) throws Exception {
        CuratorFramework curatorFramework = CuratorFrameworkFactory.builder()
                .connectString(CONNECTION_STR).sessionTimeoutMs(100000)
                .retryPolicy(new ExponentialBackoffRetry(1000,3))
                .namespace("curator").build();
        curatorFramework.start();

        //结果:/curator/lizhe/node1
        //原生api中,必须是逐层创建,父节点存在,子节点才能创建
        curatorFramework.create().creatingParentsIfNeeded().withMode(CreateMode.PERSISTENT)
                .forPath("/lizhe/node1","1".getBytes());
        //删除
        //curatorFramework.delete().deletingChildrenIfNeeded().forPath("/lizhe/node1");

        Stat stat = new Stat();
        curatorFramework.getData().storingStatIn(stat).forPath("/lizhe/node1");
        //修改
        curatorFramework.setData().withVersion(stat.getVersion()).forPath("/lizhe/node1","xx".getBytes());

        curatorFramework.close();
    }
}

3.监听事件

  1. PathChildCache 监听子节点 的创建、删除、更新
  2. NodeCache 监听当前节点的 创建、更新
  3. TreeCache 综合PathChildCache和NodeCache特性
package com.stu.demo.zooker;

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

import javax.sound.midi.Soundbank;

/**
 * 事件
 *
 *      * PathChildCache  监听子节点 的创建、删除、更新
 *      * NodeCache    监听当前节点的 创建、更新
 *      * TreeCache    综合PathChildCache和NodeCache特性
 *
 */
public class CuratorWatcherDemo {
    public final static String CONNECTION_STR="192.168.0.128:2181,192.168.0.129:2181,192.168.0.130:2181";

    public static void main(String[] args) throws Exception {
        CuratorFramework curatorFramework = CuratorFrameworkFactory.builder()
                .connectString(CONNECTION_STR).sessionTimeoutMs(100000)
                .retryPolicy(new ExponentialBackoffRetry(1000,3))
                .namespace("curator").build();
        curatorFramework.start();
        //监听当前节点的 创建、更新
        //addListenerWithNodeCache(curatorFramework,"/lizhe");
        //监听子节点 的创建、删除、更新
        //addChildrenListener(curatorFramework,"/lizhe");
        addTreeCacheListener(curatorFramework,"/lizhe");
        System.in.read();
    }

    /**
     * 监听当前节点的 创建、更新
     * @param curatorFramework
     * @param path
     * @throws Exception
     */
    public static void addListenerWithNodeCache(CuratorFramework curatorFramework,String path) throws Exception {
        NodeCache nodeCache = new NodeCache(curatorFramework,path,false);
        NodeCacheListener nodeCacheListener = new NodeCacheListener() {
            @Override
            public void nodeChanged() throws Exception {
                System.out.println("NodeCache节点触发事件:"+nodeCache.getCurrentData().getPath());
            }
        };
        nodeCache.getListenable().addListener(nodeCacheListener);
        nodeCache.start();
    }

    /**
     * 监听子节点 的创建、删除、更新
     * @param curatorFramework
     * @param path
     * @throws Exception
     */
    public static void addChildrenListener(CuratorFramework curatorFramework,String path) throws Exception {
        PathChildrenCache pathChildrenCache = new PathChildrenCache(curatorFramework,path,false);
        PathChildrenCacheListener pathChildrenCacheListener = new PathChildrenCacheListener() {
            @Override
            public void childEvent(CuratorFramework curatorFramework, PathChildrenCacheEvent pathChildrenCacheEvent) throws Exception {
                System.out.println("PathChildCache子节点触发事件:"+pathChildrenCacheEvent.getType());
            }
        };
        pathChildrenCache.getListenable().addListener(pathChildrenCacheListener);
        pathChildrenCache.start();
    }

    /**TreeCache
     * 监听当前节点的 创建、更新以及监听子节点 的创建、删除、更新
     * @param curatorFramework
     * @param path
     * @throws Exception
     */
    public static void addTreeCacheListener(CuratorFramework curatorFramework,String path) throws Exception {
        TreeCache treeCache = new TreeCache(curatorFramework,path);
        TreeCacheListener treeCacheListener = new TreeCacheListener() {
            @Override
            public void childEvent(CuratorFramework curatorFramework, TreeCacheEvent treeCacheEvent) throws Exception {
                System.out.println("TreeCache当前节点以及子节点触发事件"+treeCacheEvent.getType()+"——》"+treeCacheEvent.getData().getPath());
            }
        };
        treeCache.getListenable().addListener(treeCacheListener);
        treeCache.start();
    }
}

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值