Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

How to use the Zookeeper open source client framework Curator simply

Shulou Source: shulou.com Published: 2022-06-01 08:33:23 10月04日 Update

How to use Zookeeper open source client framework Curator, for this problem, this article details the corresponding analysis and solution, hoping to help more small partners who want to solve this problem find a simpler and easier way.

Curator was originally developed by Netflix's Jordan Zimmerman, Curator provides a set of Java libraries that make it easier to use ZooKeeper.

The so-called ZooKeeper Recipes, also known as solutions, or implementation solutions, refer to the use of ZooKeeper, such as distributed configuration management, Leader election, etc.

Curator is a natural component of Apache ZooKeeper. Java developers of ZooKeeper naturally choose to use it in their projects.

Official website link: curator.apache.org/

a functional assembly provided in

1. The Framework provides a set of advanced APIs that simplify ZooKeeper operations. It adds a lot of features developed with ZooKeeper to handle the complex connection management and retry mechanisms of ZooKeeper clusters.

2. Client is an alternative to ZooKeeper client, providing some low-level processing and related tool methods

3. Recipes implements a generic ZooKeeper recipe, which builds on the Framework

4. Utilities Various tool classes

5. Errors Exception handling, concatenation, recovery, etc.

6. The Extensions curator-recipes package implements common techniques that are described in the ZooKeeper documentation. To avoid making the package huge, recipes/applications will be placed under a separate extension package. And use the naming convention curator-x-name.

Curator compiled libraries are published in Maven Center. Curator contains several artifacts. You can add dependencies to your project according to your needs. For most developers, introducing curator-recipes is enough.

Dependency:

org.apache.curator curator-recipes 2.6.0 org.apache.curator curator-client 2.6.0 org.apache.curator curator-framework 2.6.0

Next, I used some code in actual combat.(It should be noted that zookeeper shared locks are used here. Locks executed by day are not perfect. They are controlled by node paths.)

Example code:

@Component("userTask")public class UserTask extends BaseTask { private static Logger logger = LoggerFactory.getLogger(UserTask .class); @Autowired private UserService userService; @Resource protected ZooKeeperClient zkClient; public static String LOCK_NODE = "/data/lockPs">

Also need to explain is the use of a packaged zookeeper client, here in the introduction,

Get zookeeper client:

//1. Method 1 class ZooKeeper Client { private CuratorFramework client; public ZooKeeperClient(String zkAddress) { client = CuratorFrameworkFactory.newClient(zkAddress, new ExponentialBackoffRetry(1000, 3)); client.getCuratorListenable().addListener(new NodeEventListener()); client.start(); } ...}// 2. Class 2 ZooKeeper Client { private CuratorFramework client; public ZooKeeperClient(String connectString, int sessionTimeout, String parent) throws Exception { zkClient = new CuratorZookeeperClient(connectString, sessionTimeout, sessionTimeout, zNodeWatcher, new ExponentialBackoffRetry(1000, Integer.MAX_VALUE)); zkClient.start();//must,but anytime before zookeeper operation zkClient.blockUntilConnectedOrTimedOut(); //first connection should be successful } ...} About how to carry out Zookeeper open source client framework Curator simple use of the answer to the question shared here, I hope the above content can be of some help to everyone, if you still have a lot of doubts not solved, you can pay attention to the industry information channel to learn more related knowledge.

Tags: Customers clients developers techniques methods components problems processes frameworks code tools developers approaches solutions more projects help management solutions complexity Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno MariaDB Shulou Information NVidia vpn Shulou Tech Info