Redis集群利用Redisson實(shí)現(xiàn)分布式鎖方式
Redisson實(shí)現(xiàn)集群環(huán)境下的分布式鎖十分簡(jiǎn)單:
引入依賴
<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.1.8.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>com.lk</groupId> <artifactId>demo</artifactId> <version>0.0.1-SNAPSHOT</version> <name>demo</name> <description>Demo project for Spring Boot</description> <properties> <java.version>1.8</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-devtools</artifactId> <scope>runtime</scope> <optional>true</optional> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> //Redis起步依賴 <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-redis</artifactId> <version>2.1.7.RELEASE</version> </dependency> //Redisson起步依賴 <dependency> <groupId>org.redisson</groupId> <artifactId>redisson-spring-boot-starter</artifactId> <version>3.11.2</version> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.48</version> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
Redisson配置類
package com.lk.demo.redissionconfig; import org.redisson.Redisson; import org.redisson.api.RedissonClient; import org.redisson.config.Config; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; /* * Redisson的配置類,提供RedissonClient實(shí)例 * */ @Configuration public class RedissionConfiguration { @Bean public RedissonClient getRedissionClient(){ Config config=new Config(); //集群模式,集群節(jié)點(diǎn)的地址須使用“redis://”前綴,否則將會(huì)報(bào)錯(cuò)。 //此例集群為3節(jié)點(diǎn),各節(jié)點(diǎn)1主1從 config.useClusterServers().addNodeAddress("redis://192.168.37.134:7001","redis://192.168.37.134:7002", "redis://192.168.37.134:7003","redis://192.168.37.134:7004","redis://192.168.37.134:7005","redis://192.168.37.134:7006"); return Redisson.create(config); } }
分布式鎖
package com.lk.demo.redissionconfig; import org.redisson.api.RLock; import org.redisson.api.RedissonClient; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.data.redis.core.RedisTemplate; import org.springframework.stereotype.Repository; import java.util.concurrent.TimeUnit; /* * 利用RedissonClient部署及解除分布式鎖 * 直接調(diào)用lock、unlock方法,此方法只允許1個(gè)線程取得鎖,其余線程將自旋等待 * */ @Repository public class HandlerLock{ @Autowired private RedisTemplate<String,String> rt; //@Qualifier("redisson") //在org.redisson.spring.starter包中,可以從配置文件讀取redis配置,并返回redissonclient對(duì)象 @Qualifier("getRedissionClient")//指定從本地自寫的class中取得實(shí)例 @Autowired private RedissonClient redissonClient; //實(shí)驗(yàn)方法,測(cè)試分布式鎖 public void doLock(String lockname){ //從RedissonClient取得Rlock實(shí)例 RLock rlock=redissonClient.getLock(lockname); //嘗試取鎖,有效期為3s,到期后自動(dòng)釋放。如果取得鎖繼續(xù)執(zhí)行。取鎖失敗則自旋。 //亦可使用rlock.tryLock()方法,此方法也為嘗試取鎖,并返回boolean結(jié)果 rlock.lock(3,TimeUnit.SECONDS); //以下為測(cè)試業(yè)務(wù)代碼, System.out.println(Thread.currentThread().getName()+"取得鎖"); int store=Integer.valueOf(rt.opsForValue().get("store")); if(store>0) { System.out.printf("售出1,當(dāng)前庫(kù)存為%d\n",--store); rt.opsForValue().set("store",String.valueOf(store)); }else System.out.println("已售完,下次再來吧"); //業(yè)務(wù)完成,釋放鎖 rlock.unlock(); System.out.println("解鎖"); } }
測(cè)試Controller
@Controller public class Controllerdemo { @Autowired private HandlerLock handlerLock; @RequestMapping("/hello") @ResponseBody public void demo(){ handlerLock.doLock("luking"); } }
并發(fā)測(cè)試
啟動(dòng)SpringBoot,使用Jmeter進(jìn)行并發(fā)壓力測(cè)試,Redis中store字段現(xiàn)有值100。
500線程并發(fā),循環(huán)2次
控制臺(tái)結(jié)果輸出
redis緩存
總結(jié)
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
Redis基本數(shù)據(jù)類型String常用操作命令
這篇文章主要為大家介紹了Redis基本數(shù)據(jù)類型String常用操作命令,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-05-05Redis TTL命令實(shí)現(xiàn)數(shù)據(jù)生存時(shí)間
生存時(shí)間可以通過Redis中的不同命令來設(shè)置、查看和管理,TTL命令是其中之一,本文主要介紹了Redis TTL命令實(shí)現(xiàn)數(shù)據(jù)生存時(shí)間,具有一定的參考價(jià)值,感興趣的可以了解一下2024-06-06Redis基本數(shù)據(jù)類型哈希Hash常用操作命令
這篇文章主要為大家介紹了Redis基本數(shù)據(jù)類型哈希Hash常用操作,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-05-05利用Redis的有序集合實(shí)現(xiàn)排行榜功能實(shí)例代碼
這篇文章主要給大家介紹了關(guān)于如何利用Redis的有序集合實(shí)現(xiàn)排行榜功能的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者使用Redis具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧2019-03-03Redis存儲(chǔ)斷點(diǎn)續(xù)傳文件狀態(tài)的最佳實(shí)踐
在斷點(diǎn)續(xù)傳系統(tǒng)中,如何高效地存儲(chǔ)和更新文件上傳狀態(tài)是關(guān)鍵,得益于 Redis 高效的內(nèi)存操作和多種數(shù)據(jù)結(jié)構(gòu)的支持,它非常適合用于存儲(chǔ)上傳過程中的臨時(shí)狀態(tài)信息,下面,我們將探討如何利用 Redis 實(shí)現(xiàn)文件上傳狀態(tài)的存儲(chǔ),需要的朋友可以參考下2024-12-12