SpringBoot中使用Zookeeper實(shí)現(xiàn)分布式鎖的案例
以下是一個(gè)在 Spring Boot 中使用 Zookeeper 和 Curator 實(shí)現(xiàn)分布式鎖的示例。分布式鎖可以確保在分布式環(huán)境中,同一時(shí)間只有一個(gè)客戶端能夠訪問共享資源。
1. 引入依賴
在 pom.xml
文件中添加必要的依賴:
<dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> </dependency> <dependency> <groupId>org.apache.curator</groupId> <artifactId>curator-framework</artifactId> <version>5.1.0</version> </dependency> <dependency> <groupId>org.apache.curator</groupId> <artifactId>curator-recipes</artifactId> <version>5.1.0</version> </dependency> <dependency> <groupId>org.apache.zookeeper</groupId> <artifactId>zookeeper</artifactId> <version>3.8.0</version> </dependency> </dependencies>
2. 配置 Zookeeper 連接
在 application.yml
文件中配置 Zookeeper 連接字符串:
zookeeper: connect-string: localhost:2181
3. 創(chuàng)建 Zookeeper 配置類
創(chuàng)建一個(gè)配置類來初始化 Curator 客戶端:
import org.apache.curator.framework.CuratorFramework; import org.apache.curator.framework.CuratorFrameworkFactory; import org.apache.curator.retry.RetryNTimes; import org.springframework.beans.factory.annotation.Value; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; @Configuration public class ZookeeperConfig { @Value("${zookeeper.connect-string}") private String connectString; @Bean(initMethod = "start", destroyMethod = "close") public CuratorFramework curatorFramework() { return CuratorFrameworkFactory.builder() .connectString(connectString) .sessionTimeoutMs(5000) .retryPolicy(new RetryNTimes(3, 5000)) .build(); } }
4. 創(chuàng)建分布式鎖服務(wù)類
創(chuàng)建一個(gè)服務(wù)類來實(shí)現(xiàn)分布式鎖的獲取和釋放:
import org.apache.curator.framework.CuratorFramework; import org.apache.curator.framework.recipes.locks.InterProcessMutex; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import java.util.concurrent.TimeUnit; @Service public class DistributedLockService { @Autowired private CuratorFramework curatorFramework; // 獲取分布式鎖 public boolean acquireLock(String lockPath, int timeout, TimeUnit timeUnit) { InterProcessMutex lock = new InterProcessMutex(curatorFramework, lockPath); try { return lock.acquire(timeout, timeUnit); } catch (Exception e) { e.printStackTrace(); return false; } } // 釋放分布式鎖 public void releaseLock(String lockPath) { InterProcessMutex lock = new InterProcessMutex(curatorFramework, lockPath); try { lock.release(); } catch (Exception e) { e.printStackTrace(); } } }
5. 創(chuàng)建控制器類來測試分布式鎖
創(chuàng)建一個(gè)控制器類來模擬獲取和釋放分布式鎖的操作:
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; @RestController public class DistributedLockController { @Autowired private DistributedLockService distributedLockService; @GetMapping("/lock") public String acquireDistributedLock(@RequestParam String lockPath, @RequestParam int timeout, @RequestParam String timeUnit) { TimeUnit unit = TimeUnit.valueOf(timeUnit); boolean acquired = distributedLockService.acquireLock(lockPath, timeout, unit); if (acquired) { return "成功獲取分布式鎖"; } else { return "獲取分布式鎖失敗"; } } @GetMapping("/unlock") public String releaseDistributedLock(@RequestParam String lockPath) { distributedLockService.releaseLock(lockPath); return "成功釋放分布式鎖"; } }
6. 運(yùn)行 Spring Boot 應(yīng)用
啟動 Spring Boot 應(yīng)用后,你可以通過訪問以下 URL 來測試分布式鎖的功能:
- 獲取鎖:
http://localhost:8080/lock?lockPath=/my-lock&timeout=10&timeUnit=SECONDS
- 釋放鎖:
http://localhost:8080/unlock?lockPath=/my-lock
在這個(gè)示例中:
DistributedLockService
類使用InterProcessMutex
來實(shí)現(xiàn)分布式鎖的獲取和釋放。InterProcessMutex
是 Curator 庫提供的一個(gè)用于實(shí)現(xiàn)分布式互斥鎖的類。DistributedLockController
類提供了兩個(gè) API 端點(diǎn),一個(gè)用于獲取分布式鎖,另一個(gè)用于釋放分布式鎖。
通過這種方式,你可以在 Spring Boot 應(yīng)用中利用 Zookeeper 實(shí)現(xiàn)分布式鎖,確保在分布式環(huán)境中的資源訪問控制。
到此這篇關(guān)于SpringBoot中使用Zookeeper實(shí)現(xiàn)分布式鎖的案例的文章就介紹到這了,更多相關(guān)SpringBoot Zookeeper分布式鎖內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Java 實(shí)戰(zhàn)項(xiàng)目錘煉之仿天貓網(wǎng)上商城的實(shí)現(xiàn)流程
讀萬卷書不如行萬里路,只學(xué)書上的理論是遠(yuǎn)遠(yuǎn)不夠的,只有在實(shí)戰(zhàn)中才能獲得能力的提升,本篇文章手把手帶你用java+jsp+servlet+mysql+ajax實(shí)現(xiàn)一個(gè)仿天貓網(wǎng)上商城項(xiàng)目,大家可以在過程中查缺補(bǔ)漏,提升水平2021-11-11淺談java中為什么重寫equals后需要重寫hashCode
今天帶各位學(xué)習(xí)一下java中為什么重寫equals后需要重寫hashCode,文中有非常詳細(xì)的圖文介紹及代碼示例,對正在學(xué)習(xí)java的小伙伴們有很好的幫助,需要的朋友可以參考下2021-05-05SpringBoot配置logback.xml 多環(huán)境的操作步驟
最近在研究springboot的日志,所以記錄一下,做一下總結(jié),今天重點(diǎn)給大家介紹SpringBoot配置logback.xml 多環(huán)境的操作步驟,要實(shí)現(xiàn)多環(huán)境的配置,主要是依賴于springboot的application.yml文件去實(shí)現(xiàn),感興趣的朋友跟隨小編一起看看吧2021-05-05SpringBoot對接twilio實(shí)現(xiàn)郵件信息發(fā)送功能
這篇文章主要為大家詳細(xì)介紹了SpringBoot如何對接twilio實(shí)現(xiàn)郵件信息發(fā)送功能,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2025-03-03SpringQuartz集群支持JDBC存儲與分布式執(zhí)行的最佳實(shí)踐
SpringQuartz集群通過JDBC存儲和分布式執(zhí)行機(jī)制,有效解決了單點(diǎn)故障和擴(kuò)展性問題,本文將詳細(xì)介紹SpringQuartz集群支持的實(shí)現(xiàn)原理、配置方法和最佳實(shí)踐,助力開發(fā)者構(gòu)建穩(wěn)定可靠的分布式調(diào)度系統(tǒng),感興趣的朋友一起看看吧2025-04-04SpringBoot深入探究@Conditional條件裝配的使用
這篇文章主要為大家介紹了SpringBoot底層注解@Conditional的使用分析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-06-06