SpringBoot之使用Redisson实现分布式锁(含完整例子)
# SpringBoot之使用Redisson实现分布式锁(含完整例子)
分布式环境有很多问题,比如你前一个请求访问的是服务器A,第二个请求访问到了服务器B,就会发生并发重复插入问题,这个时候需要依赖单点来实现分布锁,而redis就是。 Redisson的分布式锁的实现,一般提及到Redis的分布式锁我们更多的使用的是Redisson的分布式锁,Redis的官方也是建议我们这样去做的。Redisson点我可以直接跳转到Redisson的官方文档
# 引入Maven依赖
<dependency>
<groupId>org.redisson</groupId>
<artifactId>redisson-spring-boot-starter</artifactId>
<version>3.10.6</version>
</dependency>
1
2
3
4
5
2
3
4
5
注意:我这里引入的是redisson和springboot的集成包,网上一些教程可能是引入如下配置
<dependency>
<groupId>org.redisson</groupId>
<artifactId>redisson</artifactId>
<version>3.6.1</version>
</dependency>
1
2
3
4
5
2
3
4
5
# 配置redis信息
spring:
redis:
host: ${
REDIS_URL:192.168.6.52}
port: ${
REDIS_PORT:6379}
password: ${
REDIS_PWD:123456}
1
2
3
4
5
6
7
8
2
3
4
5
6
7
8
# 配置redisson
新建一个redisson-single.yml的配置文件 下面是单机配
singleServerConfig:
idleConnectionTimeout: 10000
pingTimeout: 1000
connectTimeout: 10000
timeout: 3000
retryAttempts: 3
retryInterval: 1500
reconnectionTimeout: 3000
failedAttempts: 3
password: 123456
subscriptionsPerConnection: 5
clientName: null
address: "redis://192.168.6.52:6379"
subscriptionConnectionMinimumIdleSize: 1
subscriptionConnectionPoolSize: 50
connectionMinimumIdleSize: 32
connectionPoolSize: 64
database: 0
#在最新版本中dns的检查操作会直接报错 所以我直接注释掉了
#dnsMonitoring: false
dnsMonitoringInterval: 5000
threads: 0
nettyThreads: 0
codec: !<org.redisson.codec.JsonJacksonCodec> {
}
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
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# RedissonConfig配置类 来配置redisson
/**
* @author tbb
*
*/
@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
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
或者使用如下配置类
/**
* @author tbb
*
*/
@Configuration
public class RedssonConfig
{
@Bean
public RedissonClient getRedisson()
{
Config config = new Config();
config.useSingleServer()
.setAddress("redis://127.0.0.1:6379").setPassword("123456")
.setRetryInterval(5000)
.setTimeout(10000)
.setConnectTimeout(10000);
return Redisson.create(config);
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# 写一个简单的测试请求
@ApiOperation("分布式锁测试")
@ResponseBody
@GetMapping("test")
public String createOrderTest()
{
return entityService.decreasePrice("1234-1234-1234", 1);
}
1
2
3
4
5
6
7
8
2
3
4
5
6
7
8
# 编写一个砍价接口
@Service
@Transactional(readOnly = true, rollbackFor = Exception.class)
public class ShwzTestService extends CrudService<ShwzTestDao, ShwzTest, String>
{
@Autowired
private RedissonClient redissonClient;
public String decreasePrice(String productId, Integer productQuantity)
{
String key = "dec_store_lock_" + productId;
RLock lock = redissonClient.getLock(key);
ShwzTest entity = new ShwzTest();
entity.setId(productId);
ShwzTest shwzTest = this.get(entity);
lock.lock();
// 加锁 操作很类似Java的ReentrantLock机制
try
{
// 简单减价操作 没有重新写其他接口了
if (shwzTest.getPrice() == 0)
{
return "当前价格为0,不能在减了!!";
}
shwzTest.setPrice(shwzTest.getPrice() - 1);
this.save(shwzTest);
}
catch (Exception e)
{
System.out.println(e.getMessage());
}
finally
{
// 解锁
lock.unlock();
}
String result = "当前价格:" + shwzTest.getPrice();
return result;
}
}
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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# 使用ab做接口测试
ab -c 1000 -n 1000 -H "Authorization:bearer46a2b87f-0f8a-477d-bcaf-dad144860a23" http://192.168.0.6:7001/a/shwz/shwzTest/test
1
ab -n 300 -c 300 请求地址
-n 的含义就是你做多少个请求
-c 的含义就是多少个用户并发请求
-H 的含义就是请求的Header头文件
1
2
3
4
2
3
4
# 测试完成 price 价格减到0 之后就没有在减少了 
上次更新: 2024/10/09, 22:49:20