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會(huì)需要你再引入
<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的檢查操作會(huì)直接報(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è)簡單的測試請(qǐng)求
@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 請(qǐng)求地址
-n 的含義就是你做多少個(gè)請(qǐng)求
-c 的含義就是多少個(gè)用戶并發(fā)請(qǐng)求
數(shù)據(jù)庫中的商品已經(jīng)全部被秒殺完 并未出現(xiàn)超庫存的情況。
如果對(duì)ab不是太了解可以看看這篇文章:使用Apache ab進(jìn)行http性能測試
以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- SpringBoot集成Redisson實(shí)現(xiàn)分布式鎖的示例代碼
- Springboot使用redisson實(shí)現(xiàn)分布式鎖的代碼示例
- SpringBoot整合分布式鎖redisson的示例代碼
- 基于Redis分布式鎖Redisson及SpringBoot集成Redisson
- SpringBoot整合Redisson實(shí)現(xiàn)分布式鎖
- Springboot中如何使用Redisson實(shí)現(xiàn)分布式鎖淺析
- SpringBoot集成Redisson實(shí)現(xiàn)分布式鎖的方法示例
- Spring Task實(shí)現(xiàn)定時(shí)任務(wù)的示例
相關(guān)文章
java調(diào)用openoffice將office系列文檔轉(zhuǎn)換為PDF的示例方法
本篇文章主要介紹了java使用openoffice將office系列文檔轉(zhuǎn)換為PDF的示例方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下。2017-11-11
mybatis-plus報(bào)錯(cuò)net.sf.jsqlparser.statement.select.SelectBody的
本文主要介紹了mybatis-plus報(bào)錯(cuò)net.sf.jsqlparser.statement.select.SelectBody的問題解決,具有一定的參考價(jià)值,感興趣的可以了解一下2024-08-08
基于Java實(shí)現(xiàn)一個(gè)簡單的數(shù)據(jù)同步組件
這篇文章主要為大家詳細(xì)介紹了如何基于Java實(shí)現(xiàn)一個(gè)簡單的數(shù)據(jù)同步組件,文中的示例代碼講解詳細(xì),具有一定的借鑒價(jià)值,感興趣的小伙伴可以了解一下2023-06-06
Java項(xiàng)目中大批量數(shù)據(jù)查詢導(dǎo)致OOM的解決
本文主要介紹了Java項(xiàng)目中大批量數(shù)據(jù)查詢導(dǎo)致OOM的解決,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-06-06
Spring?Boot?使用?Disruptor?做內(nèi)部高性能消息隊(duì)列
這篇文章主要介紹了Spring?Boot?使用?Disruptor?做內(nèi)部高性能消息隊(duì)列,工作中遇到項(xiàng)目使用Disruptor做消息隊(duì)列,對(duì)你沒看錯(cuò),不是Kafka,也不是rabbitmq。Disruptor有個(gè)最大的優(yōu)點(diǎn)就是快,還有一點(diǎn)它是開源的哦,下面做個(gè)簡單的記錄2022-06-06
PowerJob?AbstractSqlProcessor方法工作流程源碼解讀
這篇文章主要為大家介紹了PowerJob?AbstractSqlProcessor方法工作流程源碼解讀,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2024-01-01
Idea如何使用System.getenv()獲取環(huán)境變量的值
文章介紹了如何在Java中使用`System.getenv()`方法讀取環(huán)境變量的值,并提供了兩種配置環(huán)境變量的方法:啟動(dòng)項(xiàng)配置和系統(tǒng)環(huán)境變量配置,對(duì)于系統(tǒng)環(huán)境變量,文章特別指出需要重啟電腦或程序才能使其生效2024-11-11

