springboot+zookeeper實(shí)現(xiàn)分布式鎖的示例代碼
InterProcessMutex內(nèi)部實(shí)現(xiàn)了zookeeper分布式鎖的機(jī)制,所以接下來我們嘗試使用這個(gè)工具來為我們的業(yè)務(wù)加上分布式鎖處理的功能
zookeeper分布式鎖的特點(diǎn):1、分布式 2、公平鎖 3、可重入
依賴
<dependency> <groupId>org.apache.zookeeper</groupId> <artifactId>zookeeper</artifactId> <version>3.4.10</version> </dependency> <!-- zookeeper 客戶端 --> <dependency> <groupId>org.apache.curator</groupId> <artifactId>curator-framework</artifactId> <version>2.12.0</version> </dependency> <dependency> <groupId>org.apache.curator</groupId> <artifactId>curator-recipes</artifactId> <version>2.12.0</version> </dependency> <!-- lombok --> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <version>1.18.16</version> <scope>provided</scope> </dependency>
本地封裝
這個(gè)工具類主要封裝CuratorFramework這個(gè)client(連接Zookeeper)
@Slf4j public class CuratorClientUtil { private String zookeeperServer; @Getter private CuratorFramework client; public CuratorClientUtil(String zookeeperServer) { this.zookeeperServer = zookeeperServer; } // 創(chuàng)建CuratorFrameworkFactory并且啟動(dòng) public void init() { // 重試策略,等待1s,最大重試3次 RetryPolicy retryPolicy = new ExponentialBackoffRetry(1000,3); this.client = CuratorFrameworkFactory.builder() .connectString(zookeeperServer) .sessionTimeoutMs(5000) .connectionTimeoutMs(5000) .retryPolicy(retryPolicy) .build(); this.client.start(); } // 容器關(guān)閉,CuratorFrameworkFactory關(guān)閉 public void destroy() { try { if (Objects.nonNull(getClient())) { getClient().close(); } } catch (Exception e) { log.info("CuratorFramework close error=>{}", e.getMessage()); } } }
配置
@Configuration public class CuratorConfigration { @Value("${zookeeper.server}") private String zookeeperServer; // 注入時(shí),指定initMethod和destroyMethod @Bean(initMethod = "init", destroyMethod = "destroy") public CuratorClientUtil curatorClientUtil() { CuratorClientUtil clientUtil = new CuratorClientUtil(zookeeperServer); return clientUtil; } }
測(cè)試代碼
模擬不同客戶端的請(qǐng)求
@Slf4j @RestController @RequestMapping("/test") public class TestController { // 注入client工具類 @Autowired private CuratorClientUtil curatorClientUtil; // 在zookeeper的/rootLock節(jié)點(diǎn)下創(chuàng)建鎖對(duì)應(yīng)的臨時(shí)有序節(jié)點(diǎn) private String rootLock = "/rootLock"; @GetMapping("/testLock") public Object testLock() throws Exception { // 獲取當(dāng)前線程的名字,方便觀察那些線程在獲取鎖 String threadName = Thread.currentThread().getName(); InterProcessMutex mutex = new InterProcessMutex(curatorClientUtil.getClient(), rootLock); try { log.info("{}---獲取鎖start", threadName); // 嘗試獲取鎖,最長(zhǎng)等待3s,超時(shí)放棄獲取 boolean lockFlag = mutex.acquire(3000, TimeUnit.SECONDS); // 獲取鎖成功,進(jìn)行業(yè)務(wù)處理 if (lockFlag) { log.info("{}---獲取鎖success", threadName); // 模擬業(yè)務(wù)處理,時(shí)間為3s Thread.sleep(3000); } else { log.info("{}---獲取鎖fail", threadName); } } catch (Exception e) { log.info("{}---獲取鎖異常", threadName); } finally { // 業(yè)務(wù)處理完成,釋放鎖,喚醒比當(dāng)前線程創(chuàng)建的節(jié)點(diǎn)序號(hào)大(最靠近)的線程獲取鎖 mutex.release(); log.info("{}---鎖release", threadName); } return "線程:" + threadName + "執(zhí)行完成"; } }
JMeter測(cè)試
我們使用JMeter模擬100個(gè)客戶端同時(shí)并發(fā)的訪問 localhost:8081/test/testLock,相當(dāng)于100個(gè)客戶端爭(zhēng)搶分布式鎖,結(jié)果如圖右上角所示,100個(gè)請(qǐng)求花了5分6s,每個(gè)線程獲取到鎖后業(yè)務(wù)處理3s,100個(gè)線程理想時(shí)間為300s(Thread.sleep(3000)),所以運(yùn)行時(shí)間符合。
zookeeper每個(gè)線程在/rooLock節(jié)點(diǎn)下創(chuàng)建的臨時(shí)有序節(jié)點(diǎn)如下圖,由于是臨時(shí)的,所以線程釋放鎖后這些節(jié)點(diǎn)也會(huì)刪除
100個(gè)線程程序日志打印
關(guān)于InterProcessMutex內(nèi)部如何實(shí)現(xiàn)zookeeper分布式鎖,請(qǐng)看我寫的這篇文章:在這里
到此這篇關(guān)于springboot+zookeeper實(shí)現(xiàn)分布式鎖的示例代碼的文章就介紹到這了,更多相關(guān)springboot zookeeper分布式鎖內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Spring Boot整合JPA使用多個(gè)數(shù)據(jù)源的方法步驟
這篇文章主要給大家介紹了關(guān)于Spring Boot整合JPA使用多個(gè)數(shù)據(jù)源的方法步驟,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用Spring Boot具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧2019-08-08Java使用FastExcel實(shí)現(xiàn)合并單元格
FastExcel 是一個(gè)采用純 java 開發(fā)的 excel 文件讀寫組件,支持 Excel'97(-2003)(BIFF8)文件格式,本文主要介紹了如何使用FastExcel實(shí)現(xiàn)合并單元格,需要的可以參考下2024-12-12Java結(jié)合Kotlin實(shí)現(xiàn)寶寶年齡計(jì)算
這篇文章主要為大家介紹了Java結(jié)合Kotlin實(shí)現(xiàn)寶寶年齡計(jì)算示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-06-06Java?超詳細(xì)講解對(duì)象的構(gòu)造及初始化
面向?qū)ο竽耸荍ava語言的核心,是程序設(shè)計(jì)的思想。Java語言的面向?qū)ο蠹夹g(shù)包括了面向?qū)ο蠛兔嫦蜻^程的基本概念,面向?qū)ο蟮奶卣鳎琂ava語言的類,對(duì)象,修飾符,抽象類等一系列的知識(shí)點(diǎn)2022-03-03