zookeeper客户端curator使用手记

一、简介 
curator是Netflix公司开源的一个Zookeeper client library,用于简化zookeeper客户端编程,包含一下几个模块:

curator-client - zookeeper client封装,用于取代原生的zookeeper客户端,提供一些非常有用的客户端特性 
curator-framework - zookeeper api的高层封装,大大简化zookeeper客户端编程,添加了例如zookeeper连接管理、重试机制等 
curator-recipes - zookeeper recipes 基于curator-framework的实现(除2PC以外)

从github和maven上的消息来看,1.0.1的版本已经十分稳定,相对应的zk版本是3.3.x,还在开发中的版本是1.1.x,对应的版本是zk3.4.x。 
二、依赖

  1. < dependency >   
  2.              < groupId > com.netflix.curator </ groupId >   
  3.              < artifactId > curator-framework </ artifactId >   
  4.              < version > 1.0.1 </ version >   
  5.          </ dependency >   


三、代码讲解

以下代码以CuratorFramework为例:

  1. public   static   void  main(String[] args)  throws  Exception {  
  2.      String path =  "/test_path" ;  
  3.      CuratorFramework client = CuratorFrameworkFactory.builder().connectString( "zookeeper.n.miliao.com:2181" ).namespace( "/brokers" ).retryPolicy( new  RetryNTimes(Integer.MAX_VALUE,  1000 )).connectionTimeoutMs( 5000 ).build();  
  4.       // 启动 上面的namespace会作为一个最根的节点在使用时自动创建   
  5.      client.start();  
  6.   
  7.       // 创建一个节点   
  8.      client.create().forPath( "/head" ,  new   byte [ 0 ]);  
  9.   
  10.       // 异步地删除一个节点   
  11.      client.delete().inBackground().forPath( "/head" );  
  12.   
  13.       // 创建一个临时节点   
  14.      client.create().withMode(CreateMode.EPHEMERAL_SEQUENTIAL).forPath( "/head/child",  new   byte [ 0 ]);  
  15.   
  16.       // 取数据   
  17.      client.getData().watched().inBackground().forPath( "/test" );  
  18.   
  19.       // 检查路径是否存在   
  20.      client.checkExists().forPath(path);  
  21.   
  22.       // 异步删除   
  23.      client.delete().inBackground().forPath( "/head" );  
  24.   
  25.       // 注册观察者,当节点变动时触发   
  26.      client.getData().usingWatcher( new  Watcher() {  
  27.           @Override   
  28.           public   void  process(WatchedEvent event) {  
  29.              System.out.println( "node is changed" );  
  30.          }  
  31.      }).inBackground().forPath( "/test" );  
  32.   
  33.       // 结束使用   
  34.      client.close();  
  35.  }  

四、方法说明 

create(): 发起一个create操作. 可以组合其他方法 (比如mode 或background) 最后以forPath()方法结尾 
delete(): 发起一个删除操作. 可以组合其他方法(version 或background) 最后以forPath()方法结尾 
checkExists(): 发起一个检查ZNode 是否存在的操作. 可以组合其他方法(watch 或background) 最后以forPath()方法结尾 
getData(): 发起一个获取ZNode数据的操作. 可以组合其他方法(watch, background 或get stat) 最后以forPath()方法结尾 
setData(): 发起一个设置ZNode数据的操作. 可以组合其他方法(version 或background) 最后以forPath()方法结尾 
getChildren(): 发起一个获取ZNode子节点的操作. 可以组合其他方法(watch, background 或get stat) 最后以forPath()方法结尾 
inTransaction(): 发起一个ZooKeeper事务. 可以组合create, setData, check, 和/或delete 为一个操作, 然后commit() 提交

五、五四陈点评

相比zookeeper常用的zkClient的确是平易近人了,上手容易,理解轻松,语法优美,至于是不是各种情况都处理了,还得靠实践检验。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值