Curator教程(一)快速入门
2016-12-09 11:57
465 查看
Curator是Netflix公司开源的一个Zookeeper客户端,与Zookeeper提供的原生客户端相比,Curator的抽象层次更高,简化了Zookeeper客户端编程。
它包含以下几个组件:
Zookeeper 3.4.8
Curator 2.11.1
Maven 3.3
注意:一个Zookeeper集群只需要构造一个CuratorFramework 实例对象即可。
CuratorFramework 使用之前必须先调用
完成一系列操作后,调用client.close();方法,可以使用try-finally语句:
b. 创建临时节点
点此下载完整代码:https://github.com/TiFG/zookeeper-samples
它包含以下几个组件:
Component | description |
---|---|
Recipes | Implementations of some of the common ZooKeeper “recipes”. The implementations are built on top of the Curator Framework. |
Framework | The Curator Framework is a high-level API that greatly simplifies using ZooKeeper. It adds many features that build on ZooKeeper and handles the complexity of managing connections to the ZooKeeper cluster and retrying operations. |
Utilities | Various utilities that are useful when using ZooKeeper. |
Client | A replacement for the bundled ZooKeeper class that takes care of some low-level housekeeping and provides some useful utilities. |
Errors | How Curator deals with errors, connection issues, recoverable exceptions, etc. |
Extensions | The curator-recipes package implements the common recipes that are described in the ZooKeeper documentation. To avoid bloating that package, recipes/applications that have a vertical appeal will be put in separate “extension” packages using the naming convention curator-x-name. |
环境配置
JDK 1.7Zookeeper 3.4.8
Curator 2.11.1
Maven 3.3
快速入门
Maven依赖
<properties> <java.version>1.7</java.version> <curator.version>2.11.1</curator.version> </properties> <dependencies> <dependency> <groupId>org.apache.zookeeper</groupId> <artifactId>zookeeper</artifactId> <version>3.4.8</version> <exclusions> <exclusion> <artifactId>slf4j-log4j12</artifactId> <groupId>org.slf4j</groupId> </exclusion> <exclusion> <artifactId>log4j</artifactId> <groupId>log4j</groupId> </exclusion> </exclusions> </dependency> <dependency> <groupId>org.apache.curator</groupId> <artifactId>curator-framework</artifactId> <version>${curator.version}</version> </dependency> <dependency> <groupId>org.apache.curator</groupId> <artifactId>curator-client</artifactId> <version>${curator.version}</version> <exclusions> <exclusion> <groupId>com.google.guava</groupId> <artifactId>guava</artifactId> </exclusion> </exclusions> </dependency> <dependency> <groupId>com.google.guava</groupId> <artifactId>guava</artifactId> <version>18.0</version> </dependency> </dependencies>
1.创建Curator 连接实例
String address = "localhost:2181"; CuratorFramework client = CuratorFrameworkFactory.newClient(address, new ExponentialBackoffRetry(1000, 3));
注意:一个Zookeeper集群只需要构造一个CuratorFramework 实例对象即可。
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");
点此下载完整代码:https://github.com/TiFG/zookeeper-samples
参考
Getting Started:http://curator.apache.org/getting-started.html相关文章推荐
- Microsoft .Net 框架 SDK 快速入门教程
- Microsoft ASP.NET 快速入门教程
- Subversion快速入门教程
- PHP快速入门教程:hello world
- PHP快速入门教程:hello world
- 来自asp.net快速入门教程 自定义控件(1)
- PHP快速入门教程:服务器和浏览器变量获取示例
- asp.net2.0快速入门教程下载
- asp.net2.0快速入门教程下载
- PHP快速入门教程:for循环
- 原创关于asp.net快速入门教程的个性化门户配置设置篇一
- PRADO 2.0.1 快速入门教程 -- 第四章.高级主题
- PRADO 2.0.1 快速入门教程 -- 第三章.范例: the Hangman Game
- Subversion快速入门教程
- 必收:Microsoft .NET Framework SDK 快速入门教程
- 安装Microsoft .NET Framework SDK 快速入门、教程和示例
- PRADO 2.0.1 快速入门教程 -- 第二章.使用PRADO编程
- Subversion快速入门教程(转)
- Subversion快速入门教程
- PHP快速入门教程:for循环