3 Curator框架实现分布式锁
生活随笔
收集整理的這篇文章主要介紹了
3 Curator框架实现分布式锁
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
1 pom.xml
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><groupId>org.ccb</groupId><artifactId>zookeeper</artifactId><version>1.0-SNAPSHOT</version><properties><maven.compiler.source>8</maven.compiler.source><maven.compiler.target>8</maven.compiler.target></properties><dependencies><dependency><groupId>junit</groupId><artifactId>junit</artifactId><version>RELEASE</version></dependency><dependency><groupId>org.apache.logging.log4j</groupId><artifactId>log4j-core</artifactId><version>2.8.2</version></dependency><dependency><groupId>org.apache.zookeeper</groupId><artifactId>zookeeper</artifactId><version>3.6.3</version></dependency><dependency><groupId>org.apache.curator</groupId><artifactId>curator-framework</artifactId><version>4.0.1</version></dependency><dependency><groupId>org.apache.curator</groupId><artifactId>curator-recipes</artifactId><version>4.0.1</version></dependency><dependency><groupId>org.apache.curator</groupId><artifactId>curator-client</artifactId><version>4.0.1</version></dependency></dependencies> </project>2 實現
package com.ccb.zookeeper.curator;import org.apache.curator.framework.CuratorFramework; import org.apache.curator.framework.CuratorFrameworkFactory; import org.apache.curator.framework.recipes.locks.InterProcessMutex; import org.apache.curator.retry.ExponentialBackoffRetry;public class CuratorLockTest {public static void main(String[] args) {//創建分布式鎖1InterProcessMutex lock1 = new InterProcessMutex(getCuratorFramework(), "/locks");//創建分布式鎖2InterProcessMutex lock2 = new InterProcessMutex(getCuratorFramework(), "/locks");new Thread(new Runnable() {@Overridepublic void run() {try {lock1.acquire();System.out.println("線程1 獲取到鎖");lock1.acquire();System.out.println("線程1 再次獲取到鎖");Thread.sleep(5* 1000);lock1.release();System.out.println("線程1 釋放鎖");lock1.release();System.out.println("線程1 再次釋放鎖");} catch (Exception e) {e.printStackTrace();}}}).start();new Thread(new Runnable() {@Overridepublic void run() {try {lock2.acquire();System.out.println("線程2 獲取到鎖");lock2.acquire();System.out.println("線程2 再次獲取到鎖");Thread.sleep(5* 1000);lock2.release();System.out.println("線程2 釋放鎖");lock2.release();System.out.println("線程2 再次釋放鎖");} catch (Exception e) {e.printStackTrace();}}}).start();}private static CuratorFramework getCuratorFramework() {ExponentialBackoffRetry exponentialBackoffRetry = new ExponentialBackoffRetry(3000,3);CuratorFramework client = CuratorFrameworkFactory.builder().connectString("172.16.171.2:2181,172.16.171.3:2181,172.16.171.4:2181").connectionTimeoutMs(2000).sessionTimeoutMs(2000).retryPolicy(exponentialBackoffRetry).build();//啟動客戶端client.start();System.out.println("zookeeper 啟動成功");return client;}}3 test結果
線程1 獲取到鎖 線程1 再次獲取到鎖 線程1 釋放鎖 線程1 再次釋放鎖 線程2 獲取到鎖 線程2 再次獲取到鎖 線程2 釋放鎖 線程2 再次釋放鎖Process finished with exit code 0總結
以上是生活随笔為你收集整理的3 Curator框架实现分布式锁的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 2 原生Zookeeper 实现分布式锁
- 下一篇: ShardingSphere 系列