Curator uses Fluent Style.
If you haven’t used this before, it might seem odd so it’s suggested that you familiarize yourself with the style.
CuratorFramework
are allocated from theCuratorFrameworkFactory
.
You only need one CuratorFramework
object for each ZooKeeper cluster you are connecting to:
RetryPolicy retryPolicy = new ExponentialBackoffRetry(1000, 3); CuratorFramework client = CuratorFrameworkFactory.newClient("127.0.0.1:2181",retryPolicy); client.start();
The Curator Client is a low-level API.
It is strongly recommended that you use the CuratorFramework instead of directly using Curator Zookeeper Client.
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.
Some of the features are:
Automatic connection management:
There are potential error cases that require ZooKeeper clients to recreate a connection and/or retry operations. Curator automatically and transparently (mostly) handles these cases.
Cleaner API:
simplifies the raw ZooKeeper methods, events, etc.
provides a modern, fluent interface
Recipe implementations:
Leader election
Shared lock
Path cache and watcher
Distributed Queue
Distributed Priority Queue
The CuratorFramework uses a Fluent-style interface.
Operations are constructed using builders returned by the CuratorFramework instance.
client.create().forPath("/head", new byte[0]); client.delete().inBackground().forPath("/head"); client.create().withMode(CreateMode.EPHEMERAL_SEQUENTIAL) .forPath("/head/child",new byte[0]); client.getData().watched().inBackground().forPath("/test");
Background operations and watches are published via the ClientListener interface.
Register listeners with the CuratorFramework instance using the addListener method.
Curator's namespace to avoid ZK paths conflicting in ZK Cluster.
The CuratorFramework has a concept of a "namespace".
CuratorFramework will then prepend the namespace to all paths when one of its APIs is called.
CuratorFramework client = CuratorFrameworkFactory.builder().namespace("MyApp") ... build(); ... client.create().forPath("/test", data); // node was actually written to: "/MyApp/test"
Temporary CuratorFramework instances are meant for single requests to ZooKeeper ensembles over a failure prone network such as a WAN.
The APIs available from CuratorTempFramework are limited.
The connection will be closed after a period of inactivity.
CuratorTempFramework instances are created via the CuratorFrameworkFactory just like normal CuratorFramework instances.
However, instead of calling the build() method, call buildTemp().
buildTemp() creates a CuratorTempFramework instance that closes itself after 3 minutes of inactivity.
There is an alternate version of buildTemp() that allows you to specify the inactivity period.