SpringBoot使用Redisson實(shí)現(xiàn)分布式鎖(秒殺系統(tǒng))
前面講完了Redis的分布式鎖的實(shí)現(xiàn),接下來講Redisson的分布式鎖的實(shí)現(xiàn),一般提及到Redis的分布式鎖我們更多的使用的是Redisson的分布式鎖,Redis的官方也是建議我們這樣去做的。Redisson點(diǎn)我可以直接跳轉(zhuǎn)到Redisson的官方文檔。
1.1、引入Maven依賴
<dependency> <groupId>org.redisson</groupId> <artifactId>redisson-spring-boot-starter</artifactId> <version>3.10.6</version> </dependency>
注意:我這里引入的是redisson和springboot的集成包,網(wǎng)上一些教程可能是引入如下配置
<dependency> <groupId>org.redisson</groupId> <artifactId>redisson</artifactId> <version>3.6.1</version> </dependency>
如果你引入的就是redisson的依賴包,如果該依賴包的版本低于3.5會需要你再引入
<dependency> <groupId>io.netty</groupId> <artifactId>netty-all</artifactId> <version>4.1.25.Final</version> </dependency> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-core</artifactId> <version>2.9.0</version> </dependency> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-databind</artifactId> <version>2.9.0</version> </dependency>
這樣的一些依賴。
1.2、配置redis信息
spring:
application:
name: spring-cloud-product
redis:
port: 6379
host: 127.0.0.1
password:
database: 0
timeout: 2000
1.3、配置redisson

新建一個(gè)redisson-single.yml的配置文件 下面是單機(jī)配置
singleServerConfig:
idleConnectionTimeout: 10000
pingTimeout: 1000
connectTimeout: 10000
timeout: 3000
retryAttempts: 3
retryInterval: 1500
reconnectionTimeout: 3000
failedAttempts: 3
password: null
subscriptionsPerConnection: 5
clientName: null
address: "redis://127.0.0.1:6379"
subscriptionConnectionMinimumIdleSize: 1
subscriptionConnectionPoolSize: 50
connectionMinimumIdleSize: 32
connectionPoolSize: 64
database: 0
#在最新版本中dns的檢查操作會直接報(bào)錯(cuò) 所以我直接注釋掉了
#dnsMonitoring: false
dnsMonitoringInterval: 5000
threads: 0
nettyThreads: 0
codec: !<org.redisson.codec.JsonJacksonCodec> {}
transportMode : "NIO"
1.4、寫一個(gè)RedissonConfig配置類 來配置你的redisson
/**
* @Description //TODO
* @Date $ $
* @Author huangwb
**/
@Configuration
public class RedssonConfig {
@Bean(destroyMethod="shutdown")
public RedissonClient redisson() throws IOException {
RedissonClient redisson = Redisson.create(
Config.fromYAML(new ClassPathResource("redisson-single.yml").getInputStream()));
return redisson;
}
}
1.5、編寫一個(gè)秒殺接口
@Autowired
private RedissonClient redissonClient;
@Override
public boolean decrementProductStore(Long productId, Integer productQuantity) {
String key = "dec_store_lock_" + productId;
RLock lock = redissonClient.getLock(key);
try {
//加鎖 操作很類似Java的ReentrantLock機(jī)制
lock.lock();
ProductInfo productInfo = productInfoMapper.selectByPrimaryKey(productId);
//如果庫存為空
if (productInfo.getProductStock() == 0) {
return false;
}
//簡單減庫存操作 沒有重新寫其他接口了
productInfo.setProductStock(productInfo.getProductStock() - 1);
productInfoMapper.updateByPrimaryKey(productInfo);
} catch (Exception e) {
System.out.println(e.getMessage());
} finally {
//解鎖
lock.unlock();
}
return true;
}
1.6、寫一個(gè)簡單的測試請求
@GetMapping("test")
public String createOrderTest() {
if (!productInfoService.decrementProductStore(1L, 1)) {
return "庫存不足";
}
OrderMaster orderMaster = new OrderMaster();
//未支付
orderMaster.setOrderStatus(0);
//未支付
orderMaster.setPayStatus(0);
orderMaster.setBuyerName(name);
orderMaster.setBuyerAddress("湖南長沙");
orderMaster.setBuyerPhone("18692794847");
orderMaster.setOrderAmount(BigDecimal.ZERO);
orderMaster.setCreateTime(DateUtils.getCurrentDate());
orderMaster.setOrderId(UUID.randomUUID().toString().replaceAll("-", ""));
orderMasterService.insert(orderMaster);
return "創(chuàng)建訂單成功";
}
1.7、使用ab做接口測試



ab -n 300 -c 300 請求地址
-n 的含義就是你做多少個(gè)請求
-c 的含義就是多少個(gè)用戶并發(fā)請求
數(shù)據(jù)庫中的商品已經(jīng)全部被秒殺完 并未出現(xiàn)超庫存的情況。
如果對ab不是太了解可以看看這篇文章:使用Apache ab進(jìn)行http性能測試
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- SpringBoot+Redis隊(duì)列實(shí)現(xiàn)Java版秒殺的示例代碼
- Springboot+redis+Vue實(shí)現(xiàn)秒殺的項(xiàng)目實(shí)踐
- springboot?+rabbitmq+redis實(shí)現(xiàn)秒殺示例
- SpringBoot+RabbitMQ+Redis實(shí)現(xiàn)商品秒殺的示例代碼
- 基于Redis結(jié)合SpringBoot的秒殺案例詳解
- SpringBoot之使用Redis實(shí)現(xiàn)分布式鎖(秒殺系統(tǒng))
- springboot集成redis實(shí)現(xiàn)簡單秒殺系統(tǒng)
- 基于SpringBoot+Redis+Lua 實(shí)現(xiàn)高并發(fā)秒殺系統(tǒng)
相關(guān)文章
Java兩種方式實(shí)現(xiàn)動態(tài)代理
Java 在 java.lang.reflect 包中有自己的代理支持,該類(Proxy.java)用于動態(tài)生成代理類,只需傳入目標(biāo)接口、目標(biāo)接口的類加載器以及 InvocationHandler 便可為目標(biāo)接口生成代理類及代理對象。我們稱這個(gè)Java技術(shù)為:動態(tài)代理2020-10-10
Java JDBC批量執(zhí)行executeBatch方法詳解
這篇文章主要介紹了Java JDBC批量執(zhí)行executeBatch方法詳解,本篇文章通過簡要的案例,講解了該項(xiàng)技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下2021-08-08
Springboot集成Springbrick實(shí)現(xiàn)動態(tài)插件的步驟詳解
這篇文章主要介紹了Springboot集成Springbrick實(shí)現(xiàn)動態(tài)插件的詳細(xì)過程,文中的流程通過代碼示例介紹的非常詳細(xì),感興趣的同學(xué)可以參考一下2023-06-06
Idea如何配置Maven才能優(yōu)先從本地倉庫獲取依賴(親測方法有效)
對于Idea怎么配置Maven才能優(yōu)先從本地倉庫獲取依賴,網(wǎng)上說法有很多種,都不太靠譜,最終都沒有效果,最好的解決方法是通過修改maven配置文件settings.xml,本文給大家介紹的非常詳細(xì),需要的朋友參考下吧2023-10-10
BlockingQueue隊(duì)列處理高并發(fā)下的日志
這篇文章主要介紹了BlockingQueue隊(duì)列處理高并發(fā)下的日志示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步2022-03-03
SpringBoot利用模板實(shí)現(xiàn)自動生成Word合同的功能
這篇文章主要為大家詳細(xì)介紹了SpringBoot如何利用模板實(shí)現(xiàn)自動生成Word合同的功能,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以了解下2025-12-12

