Curator教程(一)快速入门
2018-12-06 12:25
239 查看
Curator是Netflix公司开源的一个Zookeeper客户端,与Zookeeper提供的原生客户端相比,Curator的抽象层次更高,简化了Zookeeper客户端编程。
Maven依赖
<dependency> <groupId>org.apache.zookeeper</groupId> <artifactId>zookeeper</artifactId> <version>3.4.9</version> </dependency> <dependency> <groupId>org.apache.curator</groupId> <artifactId>curator-recipes</artifactId> <version>2.7.0</version> </dependency>
1.创建Curator 连接实例(2种方式)
CuratorFramework client = CuratorFrameworkFactory.newClient(address, new ExponentialBackoffRetry(1000, 3));
CuratorFramework client = CuratorFrameworkFactory.builder() .connectString(address) .sessionTimeoutMs(1000) .retryPolicy(retryPolicy) .build();
CuratorFramework 使用之前必须先调用
client.start();
完成一系列操作后,调用client.close();方法,可以使用try-finally语句:
CuratorFramework client = CuratorFrameworkFactory.newClient(address, new ExponentialBackoffRetry(1000, 3)); try{ client.start(); ... }finally { if(client!=null) client.close(); }
2.创建节点
a. 创建永久性节点
client.create() .creatingParentContainersIfNeeded() .withMode(CreateMode.PERSISTENT) .withACL(ZooDefs.Ids.OPEN_ACL_UNSAFE) .forPath(path, "hello, zk".getBytes());
b. 创建临时节点
client.create().withMode(CreateMode.EPHEMERAL).forPath(path, "hello".getBytes());
3.获取节点值
byte[] buf = client.getData().forPath(path);System.out.println("get data path:"+path+", data:"+new String(buf));
4.设置节点值
client.setData().inBackground().forPath(path, "ricky".getBytes());
5.checkExists
Stat stat = client.checkExists().forPath(path); if(stat==null){ System.out.println("exec create path:"+path); }else { System.out.println("exec getData"); }
6.删除节点
client.delete().deletingChildrenIfNeeded().forPath("/pandora");
相关文章推荐
- Curator教程(一)快速入门
- HTML编程快速入门教程
- yii快速入门教程2
- yii快速入门教程17
- Grunt快速入门教程
- MiniUI快速入门教程(五)主框架布局
- Spring MVC 教程,快速入门,深入分析
- C++快速入门系列教程
- Spring MVC 教程,快速入门,深入分析
- Expression Blend实例中文教程(5) - 布局控件快速入门StackPanel,ScrollViewer和Border
- 基于PHP的cURL快速入门教程 (小偷采集程序)
- WEB前端开发快速入门教程
- Spring MVC 教程,快速入门,深入分析
- ant快速入门教程
- ASP.NET2.0快速入门视频教程
- MongoDB教程快速学会入门 第6篇 索引操作
- 小程序开发快速入门教程(附源码)
- VB脚本:快速入门教程
- Expression Blend实例中文教程(11) - 视觉管理器快速入门Visual State Manager(VSM)
- Struts2快速入门实例代码教程