ZK commonly used in three ways:
1, using native API Zookeeper
2, ZkClient
3, using Curator
Curator is open source Netflix, a Zookeeper client, compared with Zookeeper provide native client, a higher level of abstraction Curator, and simplifies the development of the amount Zookeeper client.
-
ZooKeeper client connection between the package and ZooKeeper server process;
-
Providing a Fluent style operation the API;
-
ZooKeeper provides a variety of application scenarios (recipe, such as shared locks service, the cluster leadership election mechanism) abstract package.
Curator several components
-
Client: is a substitute ZooKeeper client provides some underlying processing tools and related methods.
-
Framework: to simplify the use of advanced features ZooKeeper, and adds some new features, such as managing the connection to ZooKeeper cluster, retry processing.
-
Recipes: to achieve a common recipe ZooKeeper, the assembly is based on the Framework
-
Utilities: ZooKeeper variety of tools.
-
Errors: exception handling, connection recovery.
-
Extensions: recipe expansion
RetryPolicy connection policy
-
RetryOneTime: reconnection only once.
-
RetryNTime: Specifies the number of times reconnection N.
-
RetryUtilElapsed: Specifies the maximum reconnection timeout and reconnection time interval, intermittent reconnection until a timeout or link success.
-
ExponentialBackoffRetry: based on "backoff (retracted)" mode reconnection, and RetryUtilElapsed difference is reconnection interval is dynamic.
-
BoundedExponentialBackoffRetry: with ExponentialBackoffRetry, increased control the maximum number of retries.
Create Session
CuratorFrameworkFactory.newClient(String connectString, int sessionTimeoutMs, int connectionTimeoutMs, RetryPolicy retryPolicy) CuratorFrameworkFactory.builder().connectString("192.168.11.56:2180") .sessionTimeoutMs(30000).connectionTimeoutMs(30000) .canBeReadOnly(false) .retryPolicy(new ExponentialBackoffRetry(1000, Integer.MAX_VALUE)) .build();
Creating nodes
client.create().creatingParentIfNeeded()
.withMode(CreateMode.PERSISTENT)
.withACL(aclList)
.forPath(path, "hello, zk".getBytes());
Delete Node
client.delete().guaranteed().deletingChildrenIfNeeded().withVersion(version).forPath(path)
Gets node
client.getData().storingStatIn(stat).forPath(path);
client.getChildren().forPath(path);
Updates node
client.setData().withVersion(version).forPath(path, data)
Determine whether there is a node
client.checkExists().forPath(path);
Setting permissions
Build.authorization(String scheme, byte[] auth) client.setACL().withVersion(version) .withACL(ZooDefs.Ids.CREATOR_ALL_ACL) .forPath(path);
Monitor
Cache is the curator of the event listener packaging, listen for the event can be seen as an approximate comparison process local cache view and remote view of ZK
-
NodeCache for caching node processing node itself changes, the callback interface NodeCacheListener
-
Child nodes changes PathChildrenCache child node caching nodes for processing, the callback interface PathChildrenCacheListener
-
TreeCache / NodeCache and PathChildrenCache combination, the callback interface TreeCacheListener
NodeCache nc = new NodeCache(client, path, false); nc.start(); nc.getListenable().addListener(new NodeCacheListener() { @Override public void nodeChanged() throws Exception { ... } });