Home  >  Article  >  How does apache curator operate zookeeper?

How does apache curator operate zookeeper?

王林
王林forward
2021-01-12 10:23:491705browse

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>
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();
        }
    }

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!

Statement:
This article is reproduced at:csdn.net. If there is any infringement, please contact admin@php.cn delete