Curator是Netflix公司开源的一个Zookeeper客户端,与Zookeeper提供的原生客户端相比,Curator的抽象层次更高,简化了Zookeeper客户端编程。
它包含以下几个组件:
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. |
<properties>
<java.version>1.7java.version>
<curator.version>2.11.1curator.version>
properties>
<dependencies>
<dependency>
<groupId>org.apache.zookeepergroupId>
<artifactId>zookeeperartifactId>
<version>3.4.8version>
<exclusions>
<exclusion>
<artifactId>slf4j-log4j12artifactId>
<groupId>org.slf4jgroupId>
exclusion>
<exclusion>
<artifactId>log4jartifactId>
<groupId>log4jgroupId>
exclusion>
exclusions>
dependency>
<dependency>
<groupId>org.apache.curatorgroupId>
<artifactId>curator-frameworkartifactId>
<version>${curator.version}version>
dependency>
<dependency>
<groupId>org.apache.curatorgroupId>
<artifactId>curator-clientartifactId>
<version>${curator.version}version>
<exclusions>
<exclusion>
<groupId>com.google.guavagroupId>
<artifactId>guavaartifactId>
exclusion>
exclusions>
dependency>
<dependency>
<groupId>com.google.guavagroupId>
<artifactId>guavaartifactId>
<version>18.0version>
dependency>
dependencies>
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();
}
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());
byte[] buf = client.getData().forPath(path);
System.out.println("get data path:"+path+", data:"+new String(buf));
client.setData().inBackground().forPath(path, "ricky".getBytes());
Stat stat = client.checkExists().forPath(path);
if(stat==null){
System.out.println("exec create path:"+path);
}else {
System.out.println("exec getData");
}
client.delete().deletingChildrenIfNeeded().forPath("/pandora");
点此下载完整代码:https://github.com/TiFG/zookeeper-samples
Getting Started:http://curator.apache.org/getting-started.html