Curator is a zookeeper client of Netflix's open source, and curator has a higher level of abstraction than the native client provided by zookeeper, simplifying zookeeper client programming.
It contains the following components:
Component |
Description |
Recipes |
Implementations of some of the common zookeeper "recipes". The implementations are built on the top of the curator Framework. |
Framework |
The curator Framework is a high-level API that greatly simplifies using zookeeper. It adds many features that builds on zookeeper and handles the complexity of managing connections to the Zookeeper 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 Ities. |
Errors |
How to curator deals with errors, connection issues, recoverable exceptions, etc. |
Extensions |
The Curator-recipes package implements the common recipes are described in the zookeeper. To avoid bloating the package, recipes/applications that have a vertical appeal would be put in separate "extension" packa GES using the naming convention Curator-x-name. |
Environment ConfigurationJDK 1.7 Zookeeper 3.4.8 curator 2.11.1 Maven 3.3
Quick Start
maven Dependencies
<properties> <java.version>1.7</java.version> <curator.version>2.11.1</ curator.version> </properties> <dependencies> <dependency> <groupid>org.apache.zoo
Keeper</groupid> <artifactId>zookeeper</artifactId> <version>3.4.8</version> <exclusions> <exclusion> <ARTIFACTID>SLF4J-LOG4J12</ARTIFACTID&G
T <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& gt;guava</artifactid> </exclusion> </exclusions> </dependency> <de
Pendency> <groupId>com.google.guava</groupId> <artifactId>guava</artifactId> <version>18.0</version> </dependency> </dependencies>
1. Create a curator connection instance
String address = "localhost:2181";
Curatorframework client = curatorframeworkfactory.newclient (address, new Exponentialbackoffretry (1000, 3));
Note: A zookeeper cluster only needs to construct a Curatorframework instance object.
Curatorframework must first be invoked before using the
Client.start ();
After you complete a series of actions, call Client.close (), and you can use the try-finally statement:
Curatorframework client = curatorframeworkfactory.newclient (address, new Exponentialbackoffretry (1000, 3));
try{
Client.start ();
...
} Finally {
if (client!=null)
client.close ();
}
2. Create a node
A. Creating a permanent node
Client.create ()
. creatingparentcontainersifneeded ().
Withmode (createmode.persistent).
Withacl ( ZooDefs.Ids.OPEN_ACL_UNSAFE)
. Forpath (Path, "Hello, ZK". GetBytes ());
B. Creating a temporary node
Client.create (). Withmode (createmode.ephemeral). Forpath (Path, "Hello". GetBytes ());
3. Get the node value
byte[] buf = Client.getdata (). Forpath (path);
SYSTEM.OUT.PRINTLN ("Get Data path:" +path+ ", Data:" +new String (BUF));
4. Set node value
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. Delete node
Client.delete (). deletingchildrenifneeded (). Forpath ("/pandora");
Click here to download the complete code: Https://github.com/TiFG/zookeeper-samples Reference
Getting started:http://curator.apache.org/getting-started.html