SpringBoot——redis分布式锁实现(二十三)

依赖

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
redis:
cluster:
nodes:
- 192.168.43.32:7001
- 192.168.43.32:7002
- 192.168.43.32:7003
- 192.168.43.32:7004
- 192.168.43.32:7005
- 192.168.43.32:7006
max-redirects: 3
lettuce:
pool:
max-idle: 8
max-wait: -1
max-active: 8
min-idle: 0

RedissionConfig

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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
import cn.rojao.util.StringUtils;

import org.redisson.Redisson;

import org.redisson.api.RedissonClient;

import org.redisson.config.Config;

import org.springframework.beans.factory.annotation.Value;

import org.springframework.context.annotation.Bean;

import org.springframework.context.annotation.Configuration;


/**

* redisson配置

*/

@Configuration

public class RedissonConfig {

@Value("${spring.redis.clusters}")

private String cluster;

@Value("${spring.redis.password}")

private String password;



@Bean

public RedissonClient getRedisson(){

String[] nodes = cluster.split(",");

//redisson版本是3.5,集群的ip前面要加上“redis://”,不然会报错,3.2版本可不加

for(int i=0;i<nodes.length;i++){

nodes[i] = "redis://"+nodes[i];

}

RedissonClient redisson = null;

Config config = new Config();

config.useClusterServers() //这是用的集群server

.setScanInterval(2000) //设置集群状态扫描时间

.addNodeAddress(nodes).setRetryAttempts(5).setTimeout(10000);



if(StringUtils.isNotEmpty(password)){

config.useClusterServers().setPassword(password);

}

redisson = Redisson.create(config);



//可通过打印redisson.getConfig().toJSON().toString()来检测是否配置成功

// try {

// System.out.println(redisson.getConfig().toJSON().toString());

// } catch (IOException e) {

// e.printStackTrace();

// }

return redisson;

}
}

DistributedRedisLock

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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
package com.example.springboot_redis.redis;

import lombok.extern.slf4j.Slf4j;
import org.redisson.api.RLock;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.concurrent.TimeUnit;

/**
* 分布式Redis锁
*
* @author qp
* @date 2019/7/19 15:21
*/
@Slf4j
@Component
public class DistributedRedisLock {

@Autowired
private RedissonClient redissonClient;

// 加锁
public Boolean lock(String lockName) {
try {
if (redissonClient == null) {
log.info("DistributedRedisLock redissonClient is null");
return false;
}

RLock lock = redissonClient.getLock(lockName);
// 锁10秒后自动释放,防止死锁
lock.lock(10, TimeUnit.SECONDS);

log.info("Thread [{}] DistributedRedisLock lock [{}] success", Thread.currentThread().getName(), lockName);
// 加锁成功
return true;
} catch (Exception e) {
log.error("DistributedRedisLock lock [{}] Exception:", lockName, e);
return false;
}
}

// 释放锁
public Boolean unlock(String lockName) {
try {
if (redissonClient == null) {
log.info("DistributedRedisLock redissonClient is null");
return false;
}

RLock lock = redissonClient.getLock(lockName);
lock.unlock();
log.info("Thread [{}] DistributedRedisLock unlock [{}] success", Thread.currentThread().getName(), lockName);
// 释放锁成功
return true;
} catch (Exception e) {
log.error("DistributedRedisLock unlock [{}] Exception:", lockName, e);
return false;
}
}

}

LockTestController

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
package com.example.springboot_redis.contoller;

import com.example.springboot_redis.redis.DistributedRedisLock;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
* 分布式Redis锁测试controller
*
* @author qp
* @date 2019/7/19 17:30
*/
@RestController
@RequestMapping("/lock")
public class LockTestController {

@Autowired
private DistributedRedisLock distributedRedisLock;

// 测试分布式锁
@GetMapping("/testLock")
public void testLock() {
for (int i = 0; i < 5; i++) {
new Thread(new Runnable() {
@Override
public void run() {
Boolean lockFlag = distributedRedisLock.lock("LOCK");
}
}).start();
}
}

}
文章目录
  1. 1. 依赖
  2. 2. RedissionConfig
  3. 3. DistributedRedisLock
  4. 4. LockTestController
|