Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use the Zookeeper open source client framework Curator simply

2025-03-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

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.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report