Home > Common Problem > How does apache curator operate zookeeper?

How does apache curator operate zookeeper?

王林
Release: 2021-01-12 10:23:49
forward
1784 people have browsed it

How does apache curator operate zookeeper?

How does apache curator operate zookeeper?

(Learning video sharing: Programming video)

First of all, let’s briefly introduce apache curator.

Apache Curator is a Java/JVM client library for Apache ZooKeeper, a distributed coordination service. It includes a high-level API framework and utilities to 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.

Official website: http://curator.apache.org/index.html

Curator project components (you can see the following components by downloading the official source code)

How does apache curator operate zookeeper?

Maven dependency (Address: https://search.maven.org/search?q=org.apache.curator)

How does apache curator operate zookeeper?

Distributed lock implementation

<dependency>
  <groupId>org.apache.curator</groupId>
  <artifactId>curator-recipes</artifactId>
  <version>4.1.0</version>
</dependency>
Copy after login
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 {
            //创建分布式锁, 锁空间的根节点路径为/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
                {
                    //完成业务流程, 释放锁
                    lock.release();
                }
            }
 
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
Copy after login

Related recommendations: apache tutorial

The above is the detailed content of How does apache curator operate zookeeper?. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:csdn.net
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template