preface
- Following on from the previous article (Java implementing distributed locking – based on Zookeeper(3)), today we’ll talk about curator curator.
Curator profile
- Apache Curator is a relatively complete ZooKeeper client framework, which simplifies ZooKeeper operations by encapsulating a set of advanced apis. A review of official documents shows that Curator addresses three main problems:
- Encapsulates the connection between the ZooKeeper Client and ZooKeeper Server
- A Fluent style operation API is provided
- Provide abstract encapsulation of Various ZooKeeper application scenarios (recipe, distributed lock service, cluster leader election, shared counters, caching mechanism, distributed queue, etc.)
- Curator reduces the complexity of USING ZK mainly from the following aspects:
- Retry mechanism: Provides a pluggable retry mechanism that configates a retry strategy for catching all recoverable exceptions, and internally provides several standard retry strategies (such as exponential compensation)
- Connection state monitoring: When a zK connection is initialized, a Curator always listens for a change in connection state
- Zk Client instance Management: Manage connections between ZK clients and the server cluster and rebuild ZK instances when needed to ensure reliable connections with the ZK cluster
- Supported for various usage scenarios :Curator implements most of the usage scenarios supported by ZK (even those not supported by ZK itself), all of which follow ZK best practices and allow for extreme cases
Code implementation
- pom.xml
< the dependency > < groupId > org. Apache. Curator < / groupId > < artifactId > curator - recipes < / artifactId > < version > 4.2.0 < / version > </dependency>Copy the code
- DistributeZkLockApplication. Java / / lazy here at start write a bean class
import org.apache.curator.RetryPolicy; import org.apache.curator.framework.CuratorFramework; import org.apache.curator.framework.CuratorFrameworkFactory; import org.apache.curator.retry.ExponentialBackoffRetry; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.context.annotation.Bean; @SpringBootApplication public class DistributeZkLockApplication { public static void main(String[] args) { SpringApplication.run(DistributeZkLockApplication.class, args); } @Bean(initMethod="start",destroyMethod = "close") public CuratorFramework getCuratorFramework() { RetryPolicy retryPolicy = new ExponentialBackoffRetry(1000, 3); CuratorFramework client = CuratorFrameworkFactory.newClient("localhost:2181", retryPolicy); return client; }}Copy the code
- ZookeeperController.java
import com.example.distributezklock.lock.ZkLock; import org.apache.curator.framework.CuratorFramework; import org.apache.curator.framework.recipes.locks.InterProcessMutex; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import java.io.IOException; import java.util.concurrent.TimeUnit; @RestController public class ZookeeperController { @Autowired private CuratorFramework client; @RequestMapping("curatorLock") public String curatorLock(){ InterProcessMutex lock = new InterProcessMutex(client, "/order"); try{ if (lock.acquire(30, TimeUnit.SECONDS)){ Thread.sleep(10000); } } catch (IOException e) { e.printStackTrace(); } catch (Exception e) { e.printStackTrace(); }finally { try { lock.release(); } catch (Exception e) { e.printStackTrace(); }} return "Method done!" ; }}Copy the code