创建session
public class CuratorClientUtils {
private static CuratorFramework curatorFramework;
private final static String CONNECTSTRING="172.17.0.2:2181";
public static CuratorFramework getInstance(){
curatorFramework= CuratorFrameworkFactory.
newClient(CONNECTSTRING,5000,5000,
new ExponentialBackoffRetry(1000,3));
curatorFramework.start();
return curatorFramework;
}
}
或者:
public class CuratorCreateSessionDemo {
private final static String CONNECTSTRING="172.17.0.2:2181";
public static void main(String[] args) {
//创建会话的两种方式 normal
// CuratorFramework curatorFramework= CuratorFrameworkFactory.
// newClient(CONNECTSTRING,5000,5000,
// new ExponentialBackoffRetry(1000,3));
// curatorFramework.start(); //start方法启动连接
// //fluent风格
CuratorFramework curatorFramework1=CuratorFrameworkFactory.builder().connectString(CONNECTSTRING).sessionTimeoutMs(5000).
retryPolicy(new ExponentialBackoffRetry(1000,3)).
build();
curatorFramework1.start();
System.out.println("success");
}
}
事件监听
public class CuratorEventDemo {
/**
* 三种watcher来做节点的监听
* pathcache 监视一个路径下子节点的创建、删除、节点数据更新
* NodeCache 监视一个节点的创建、更新、删除
* TreeCache pathcaceh+nodecache 的合体(监视路径下的创建、更新、删除事件),
* 缓存路径下的所有子节点的数据
*/
public static void main(String[] args) throws Exception {
CuratorFramework curatorFramework=CuratorClientUtils.getInstance();
/**
* 节点变化NodeCache
*/
/* NodeCache cache=new NodeCache(curatorFramework,"/curator",false);
cache.start(true);
cache.getListenable().addListener(()-> System.out.println("节点数据发生变化,变化后的结果" +
":"+new String(cache.getCurrentData().getData())));
curatorFramework.setData().forPath("/curator","菲菲".getBytes());*/
/**
* PatchChildrenCache
*/
PathChildrenCache cache=new PathChildrenCache(curatorFramework,"/event",true);
cache.start(PathChildrenCache.StartMode.POST_INITIALIZED_EVENT);
// Normal / BUILD_INITIAL_CACHE /POST_INITIALIZED_EVENT
cache.getListenable().addListener((curatorFramework1,pathChildrenCacheEvent)->{
switch (pathChildrenCacheEvent.getType()){
case CHILD_ADDED:
System.out.println("增加子节点");
break;
case CHILD_REMOVED:
System.out.println("删除子节点");
break;
case CHILD_UPDATED:
System.out.println("更新子节点");
break;
default:break;
}
});
curatorFramework.create().withMode(CreateMode.PERSISTENT).forPath("/event","event".getBytes());
TimeUnit.SECONDS.sleep(1);
System.out.println("1");
curatorFramework.create().withMode(CreateMode.EPHEMERAL).forPath("/event/event1","1".getBytes());
TimeUnit.SECONDS.sleep(1);
System.out.println("2");
curatorFramework.setData().forPath("/event/event1","222".getBytes());
TimeUnit.SECONDS.sleep(1);
System.out.println("3");
curatorFramework.delete().forPath("/event/event1");
System.out.println("4");
System.in.read();
}
}
其它操作
public class CuratorOperatorDemo {
public static void main(String[] args) throws InterruptedException {
CuratorFramework curatorFramework=CuratorClientUtils.getInstance();
System.out.println("连接成功.........");
//fluent风格
/**
* 创建节点
*/
// try {
// String result=curatorFramework.create().creatingParentsIfNeeded().withMode(CreateMode.PERSISTENT).
// forPath("/curator/curator1/curator11","123".getBytes());
//
// System.out.println(result);
// } catch (Exception e) {
// e.printStackTrace();
// }
/**
* 删除节点
*/
// try {
// //默认情况下,version为-1
// curatorFramework.delete().deletingChildrenIfNeeded().forPath("/curator");
//
// } catch (Exception e) {
// e.printStackTrace();
// }
/**
* 查询
*/
// Stat stat=new Stat();
// try {
// byte[] bytes=curatorFramework.getData().storingStatIn(stat).forPath("/curator");
// System.out.println(new String(bytes)+"-->stat:"+stat);
// } catch (Exception e) {
// e.printStackTrace();
// }
/**
* 更新
*/
// try {
// Stat stat=curatorFramework.setData().forPath("/curator","123".getBytes());
// System.out.println(stat);
// } catch (Exception e) {
// e.printStackTrace();
// }
/**
* 异步操作
*/
// ExecutorService service= Executors.newFixedThreadPool(1);
// CountDownLatch countDownLatch=new CountDownLatch(1);
// try {
// curatorFramework.create().creatingParentsIfNeeded().withMode(CreateMode.EPHEMERAL).
// inBackground(new BackgroundCallback() {
// @Override
// public void processResult(CuratorFramework curatorFramework, CuratorEvent curatorEvent) throws Exception {
// System.out.println(Thread.currentThread().getName()+"->resultCode:"+curatorEvent.getResultCode()+"->"
// +curatorEvent.getType());
// countDownLatch.countDown();
// }
// },service).forPath("/enjoy","deer".getBytes());
// } catch (Exception e) {
// e.printStackTrace();
// }
// countDownLatch.await();
// service.shutdown();
/**
* 事务操作(curator独有的)
*/
try {
Collection<CuratorTransactionResult> resultCollections=curatorFramework.inTransaction().create().forPath("/demo2","111".getBytes()).and().
setData().forPath("/demo2","111".getBytes()).and().commit();
for (CuratorTransactionResult result:resultCollections){
System.out.println(result.getForPath()+"->"+result.getType());
}
} catch (Exception e) {
e.printStackTrace();
}
}
}