Three java clients commonly used by zookeeper:
- zookeeper native Java API
- ZkClient
- Apache curator
1. zookeeper native Java API
Zookeeper client provides basic operations, such as creating a session, creating a node, reading a node, updating data, deleting a node and checking whether a node exists. But for developers, the basic manipulation provided by zookeeper still has some shortcomings.
Zookeeper API deficiencies
(1) The reconnection mechanism is not implemented after the Session timeout, and manual operation is required;
(2) Watcher registration is one-time and needs to be re registered after each trigger;
(3) Recursive node creation is not supported;
(4) Exception handling is cumbersome. Zookeeper provides many exceptions. Developers may not know how to handle these exception information at all;
(5) Only a simple byte [] array interface is provided, and no object level serialization is provided;
(6) If an exception is thrown when creating a node, you need to check whether the node exists;
(7) Deleting a node cannot achieve cascade deletion;
For the above reasons, few people directly use Zookeeper's native API.
2,ZkClient
<!-- https://mvnrepository.com/artifact/com.101tec/zkclient --> <dependency> <groupId>com.101tec</groupId> <artifactId>zkclient</artifactId> <version>0.11</version> </dependency>
ZkClient is an open source client, packaged on the basis of Zookeeper's native API interface, which is more convenient for developers to use. Solve the following problems:
1) Session session timeout reconnection
2) Solve Watcher's repeated registration
3) Simplify API development
Although ZkClient encapsulates the native API, it also has its own shortcomings:
- There are few reference documents;
- Simplify exception handling (throw RuntimeException);
- The retry mechanism is difficult to use;
- The implementation of various usage scenarios is not provided;
3,Apache Curator
Related dependency reference articles: https://blog.csdn.net/xiaojin21cen/article/details/88538102
Cursor is a Zookeeper client framework open source by Netflix. Like ZkClient, it solves the very low-level detail development work, including connection reconnection, repeated registration of Watcher and NodeExistsException exceptions. At present, it has become the top project of Apache.
Its features:
- Apache open source project
- Solve the problem that the Watch registration will fail once
- Provide a set of Fluent style API, which is easier to use
- Provide more solutions and simple implementation, such as distributed lock
- Provides common ZooKeeper tool classes
- More comfortable programming style
In addition, cursor also provides Abstract encapsulation of various application scenarios of Zookeeper (Recipe, such as shared lock service, Master election mechanism, distributed calculator, etc.).
Supplementary notes
Apache cursor is the Java / JVM client library of Apache ZooKeeper, which is a distributed coordination service.
It includes an advanced API framework and utilities that make Apache ZooKeeper easier and more reliable. It also includes recipes for common use cases and extensions such as service discovery and Java 8 asynchronous DSL s.
Official website: http://curator.apache.org/index.html
Cursor project components (you can see the following components by downloading the official source code)
Recipes: Zookeeper Implementation of typical application scenarios, which are based on Curator Framework.
Framework: Zookeeper API High level packaging, greatly simplified Zookeeper Client programming, added for example Zookeeper Connection management, retry mechanism, etc.
Utilities: by Zookeeper Various utilities provided.
Client: Zookeeper client Encapsulation for replacing native Zookeeper Client( ZooKeeper Class), which provides some very useful client-side features.
Errors: Curator How to handle errors, connection problems, recoverable exceptions, etc.
Maven dependency (address: https://search.maven.org/search?q=org.apache.curator )
GroupID/Org ArtifactID/Name describe org.apache.curator curator-recipes All typical application scenarios. Need to rely on client and framework,Automatic acquisition dependency needs to be set. org.apache.curator curator-framework Same as component framework Introduction. org.apache.curator curator-client Same as component client Introduction. org.apache.curator curator-test contain TestingServer,TestingCluster And some test tools. org.apache.curator curator-examples Various uses Curator Cases of features. org.apache.curator curator-x-discovery stay framework Service discovery implementation built on. org.apache.curator curator-x-discoveryserver Can and Curator Discovery Used together RESTful The server. org.apache.curator curator-x-rpc Curator framework and recipes wrong java Bridging the environment.
Distributed lock implementation
<dependency> <groupId>org.apache.curator</groupId> <artifactId>curator-recipes</artifactId> <version>4.1.0</version> </dependency>
public static void main(String[] args) { String zookeeperConnectionString = "localhost:2181"; RetryPolicy retryPolicy = new ExponentialBackoffRetry(1000, 3); CuratorFramework client = CuratorFrameworkFactory.newClient(zookeeperConnectionString, retryPolicy); client.start(); try { //Create distributed locks, The root node path of the lock space is/curator/lock InterProcessMutex lock = new InterProcessMutex(client, "/curator/lock"); if ( lock.acquire(1000, TimeUnit.SECONDS) ) { try { // do some work inside of the critical section here System.out.println("do some work inside of the critical section here"); } finally { //Complete business process, Release lock lock.release(); } } } catch (Exception e) { e.printStackTrace(); } }