In order to better implement Java operation zooKeeper server, later appeared the Curator framework, very powerful, is currently apache’s top project, which provides more rich operations. For example, session timeout reconnection, primary/secondary election, distributed counters, and distributed locks are suitable for various complex ZooKeeper scenarios.

Source: github.com/limingios/n…

The official source

Curator.apache.org/ ZkClient is similar to Mybatis, and is also a curator of data on zkClient.

  • Maven rely on
<dependency>
  <groupId>org.apache.curator</groupId>Basic framework<artifactId>curator-framework</artifactId>
  <version></version>
</dependency>
<dependency>
  <groupId>org.apache.curator</groupId>
  <artifactId>curator-recipes</artifactId>Jar distributed lock, queue, etc<version></version>
</dependency>
<dependency>Client retry policy<groupId>org.apache.curator</groupId>
  <artifactId>curator-client</artifactId>
  <version></version>
</dependency>
Copy the code
  • use

The Curator framework provides a streaming interface to string parameters through the Builder as callback methods.

The CuratorFrameworkFactory is used by the Curator framework to create CuratorFramework instances in factory mode and Builder mode. CuratorFramework instances are thread-safe and should be shared across your applications. The factory method newClient() provides a simple way to create instances. Builder provides more parameter control. Once you have created a CuratorFramework instance, you must call its start() to start and call the close() method to close it when the application exits.

  • Create a Curator connection instance

Note: a Zookeeper cluster only needs to construct a CuratorFramework instance object. The CuratorFramework must call client.start() before it can be used;

String address = "localhost:2181";
CuratorFramework  client  =  CuratorFrameworkFactory.newClient(address,  new
ExponentialBackoffRetry(1000.3));// Retry mechanism
client.start()
Copy the code
  • CuratorFramework

Methods provided

Interface class Register listening methods
The method name describe
create To start the creation operation, you can call additional methods (such as mode or background) and finally call forPath() to specify which ZNode to operate on
Delete You can call additional methods (version or background processing version or background) and finally call forPath() to specify which ZNode to operate on
checkExists The system starts to check whether the ZNode exists. You can call additional methods (monitoring or background processing) and finally call forPath() to specify which ZNode to operate on
getData The operation to obtain ZNode data begins. You can call additional methods (monitor, background, or get the status watch, background, or get stat) and finally call forPath() to specify which ZNode to operate on
setData The system starts to set ZNode data. You can call additional methods (version or background management) and finally call forPath() to specify which ZNode to operate on
getChildren Start getting a list of ZNode children. Specify the ZNode to operate on by calling additional methods (monitor, background, or get status watch, background, or get stat) and finally calling forPath()
inTransaction It starts with an atomic ZooKeeper transaction. You can compound the create, setData, check, and/ orDELETE operations and call commit() as an atomic operation

Event type and method of the event

Event Type Event Methods
CREATE getResultCode() and getPath()
DELETE getResultCode() and getPath()
EXISTS getResultCode(), getPath() and getStat()
GETDATA getResultCode(), getPath(), getStat() and getData()
SETDATA getResultCode(), getPath() and getStat()
CHILDREN getResultCode(), getPath(), getStat(),getChildren()
WATCHED getWatchedEvent

The listener

Toth provides three types of Watcher(Cache) to listen for node changes:

  • Path Cache

Monitor the creation, deletion, and update of a path’s subnodes. The event will be passed to the registered PathChildrenCacheListener.

  • Node Cache

Monitor the creation, update, and deletion of a node, and cache its data locally.

  • Tree Cache

A combination of the Path Cache and Node Cache monitors creation, update, and deletion events in a Path and caches data from all children of the Path.

Retry mechanism

Several retry strategies implemented internally by Curator:

  1. ExponentialBackoffRetry: Specifies the number of retry times, and the pause time between retries increases gradually.
  2. RetryNTimes: Retry policy that specifies the maximum number of retries
  3. RetryOneTime: Retry only once
  4. RetryUntilElapsed: Retry until the specified time is reached

A distributed lock

Introduced the maven. Functions such as distributed locks or atomic operations and queues should be introduced

<dependency>
<groupId>org.apache.curator</groupId>
<artifactId>curator-recipes</artifactId>
<version>${version}</version>
</dependency>
Copy the code

PS: just a tool, Apache’s top project, can be learned through the source code. The source code is provided above the article.