java——redis随笔——实战——优惠券秒杀——分布式锁——基于redisson
发布时间 2023-10-29 13:23:09作者: 小白龙白龙马
Redisson入门
- 导入依赖
1 2 3 4 5
|
<dependency> <groupId>org.redisson</groupId> <artifactId>redisson</artifactId> <version>3.13.6</version> </dependency>
|
- 配置Redisson客户端,在config包下新建
RedissonConfig
类
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
|
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;
@Configuration public class RedissonConfig { @Bean public RedissonClient redissonClient() { Config config = new Config(); config.useSingleServer() .setAddress("redis://101.XXX.XXX.160:6379") .setPassword("root"); return Redisson.create(config); } }
|
- 使用Redisson的分布式锁
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
|
@Resource private RedissonClient redissonClient;
@Test void testRedisson() throws InterruptedException {
|
- 替换我们之前自己写的分布式锁
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
|
@Resource private RedissonClient redissonClient; @Override public Result seckillVoucher(Long voucherId) { LambdaQueryWrapper<SeckillVoucher> queryWrapper = new LambdaQueryWrapper<>();
|
- 使用Jmeter进行压力测试,依旧是只能抢到一张优惠券,满足我们的需求
1
1
1