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.监听事件
- PathChildCache 监听子节点 的创建、删除、更新
- NodeCache 监听当前节点的 创建、更新
- 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();
}
}