怎么在SpringBoot中使用redisson实现一个分布式锁?针对这个问题,这篇文章详细介绍了相对应的分析和解答,希望可以帮助更多想解决这个问题的小伙伴找到更简单易行的方法。
成都创新互联公司专注于温岭网站建设服务及定制,我们拥有丰富的企业做网站经验。 热诚为您提供温岭营销型网站建设,温岭网站制作、温岭网页设计、温岭网站官网定制、成都微信小程序服务,打造温岭网络公司原创品牌,更为您提供温岭网站排名全网营销落地服务。
1.1、引入Maven依赖
org.redisson redisson-spring-boot-starter 3.10.6
注意:我这里引入的是redisson和springboot的集成包,网上一些教程可能是引入如下配置
org.redisson redisson 3.6.1
如果你引入的就是redisson的依赖包,如果该依赖包的版本低于3.5会需要你再引入
io.netty netty-all 4.1.25.Final com.fasterxml.jackson.core jackson-core 2.9.0 com.fasterxml.jackson.core jackson-databind 2.9.0
这样的一些依赖。
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

新建一个redisson-single.yml的配置文件 下面是单机配置
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的检查操作会直接报错 所以我直接注释掉了 #dnsMonitoring: false dnsMonitoringInterval: 5000 threads: 0 nettyThreads: 0 codec: !{} transportMode : "NIO"
1.4、写一个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、编写一个秒杀接口
@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机制
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、写一个简单的测试请求
@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 "创建订单成功";
}1.7、使用ab做接口测试



ab -n 300 -c 300 请求地址
-n 的含义就是你做多少个请求
-c 的含义就是多少个用户并发请求
数据库中的商品已经全部被秒杀完 并未出现超库存的情况。
关于怎么在SpringBoot中使用Redisson实现一个分布式锁问题的解答就分享到这里了,希望以上内容可以对大家有一定的帮助,如果你还有很多疑惑没有解开,可以关注创新互联行业资讯频道了解更多相关知识。
文章标题:怎么在SpringBoot中使用Redisson实现一个分布式锁
文章起源:http://www.jxjierui.cn/article/jegsos.html


咨询
建站咨询
