Zookeeper客户端curator常用法
2016-01-25 20:45
399 查看
Maven配置
<dependency> <groupId>org.apache.curator</groupId> <artifactId>curator-framework</artifactId> </dependency> <dependency> <groupId>org.apache.curator</groupId> <artifactId>curator-recipes</artifactId> </dependency>
基本用法
增删查改public static void main( String[] args ) throws Exception { //创建重连策略 RetryPolicy retryPolicy = new ExponentialBackoffRetry(1000, 3); //创建会话 CuratorFramework client = CuratorFrameworkFactory.builder() .connectString("localhost:2181") .sessionTimeoutMs(5000) .retryPolicy(retryPolicy) .build(); client.start(); String path = "/example"; //创建节点 client.create().creatingParentsIfNeeded().withMode(CreateMode.EPHEMERAL).forPath(path, "init".getBytes()); //读取数据节点 byte[] data = client.getData().forPath(path); System.out.println("init data is: " + new String(data)); //更新数据节点 client.setData().forPath(path, "update data".getBytes()); //删除数据节点 client.delete().deletingChildrenIfNeeded().forPath(path); }
异步接口
//请参考前面代码 ... //关键代码 final CountDownLatch cdl = new CountDownLatch(1); String path = "/example"; //创建节点 client.create().creatingParentsIfNeeded().withMode(CreateMode.EPHEMERAL) //创建成功后回调方法 .inBackground(new BackgroundCallback() { @Override public void processResult(CuratorFramework curatorFramework, CuratorEvent curatorEvent) throws Exception { System.out.println("node name is: " + curatorEvent.getName()); System.out.println("node path is: " + curatorEvent.getPath()); System.out.println("event type: " + curatorEvent.getType()); cdl.countDown(); } }) .forPath(path, "init".getBytes()); System.out.println("already commit!"); cdl.await();
输出:
already commit! node name is: /example node path is: /example event type: CREATE
事件监听
数据节点监听
public static void watchNode() throws Exception { client.start(); //创建节点 client.create().creatingParentsIfNeeded().withMode(CreateMode.EPHEMERAL).forPath(path, "init".getBytes()); final NodeCache nodeCache = new NodeCache(client, path); nodeCache.start(); final CountDownLatch countDownLatch = new CountDownLatch(1); nodeCache.getListenable().addListener(new NodeCacheListener() { @Override public void nodeChanged() throws Exception { byte[] data = nodeCache.getCurrentData().getData(); System.out.println(new String(data)); countDownLatch.countDown(); } }); client.setData().forPath(path, "update node".getBytes()); countDownLatch.await(); }
输出:
update node
数据子节点监听
public static void watchChildrenNode() throws Exception { client.start(); //创建节点 final NodeCache nodeCache = new NodeCache(client, path); nodeCache.start(); final CountDownLatch countDownLatch = new CountDownLatch(2); PathChildrenCache pathChildrenCache = new PathChildrenCache(client, path, true); pathChildrenCache.start(PathChildrenCache.StartMode.POST_INITIALIZED_EVENT); pathChildrenCache.getListenable().addListener(new PathChildrenCacheListener() { @Override public void childEvent(CuratorFramework curatorFramework, PathChildrenCacheEvent pathChildrenCacheEvent) throws Exception { System.out.println("event type: " + pathChildrenCacheEvent.getType()); System.out.println("node data: " + pathChildrenCacheEvent.getData()); countDownLatch.countDown(); } }); client.create().withMode(CreateMode.EPHEMERAL).forPath(path + "/child", "child data".getBytes()); System.out.println("init child data: " + new String(client.getData().forPath(path + "/child"))); client.setData().forPath(path + "/child", "set child data".getBytes()); countDownLatch.await(); }
输出:
event type: INITIALIZED node data: null init child data: child data event type: CHILD_ADDED node data: ChildData{path='/example/child', stat=151,151,1453725266116,1453725266116,0,0,0,95263405746815019,10,0,151 , data=[99, 104, 105, 108, 100, 32, 100, 97, 116, 97]}
相关文章推荐
- Shell脚本实现自动安装zookeeper
- 基于Zookeeper的使用详解
- mesos + marathon + docker部署
- 基于zk的配置管理
- SolrCloud4.9+zookeeper在CentOS上的搭建与安装
- 基于外部ZooKeeper的GlusterFS作为分布式文件系统的完全分布式HBase集群安装指南
- redis集群搭建
- 使用 RMI + ZooKeeper 实现远程调用框架
- 轻量级分布式 RPC 框架
- 整合Kafka到Spark Streaming——代码示例和挑战
- HBase 系统架构
- 使用zookeeper实现分布式共享锁
- 将Zookeeper集成到你的应用中
- 基于zookeeper+leveldb搭建activemq集群
- Fourinone四合一分布式计算框架整体介绍
- windows 64位 安装zookeeper
- 工作中的zookeeper
- storm
- zookeeper技术浅析
- ZOOKEEPER的作用