Skip to content

Commit 2d8b66e

Browse files
committed
分布式锁的实现方式改为Redisson,并优化抽象接口
1 parent 4be31c4 commit 2d8b66e

File tree

18 files changed

+229
-256
lines changed

18 files changed

+229
-256
lines changed

zlt-business/user-center/src/main/java/com/central/user/controller/SysRoleController.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ public Result<List<SysRole>> findAll() {
6161
* @return
6262
*/
6363
@PostMapping("/roles/saveOrUpdate")
64-
public Result saveOrUpdate(@RequestBody SysRole sysRole) {
64+
public Result saveOrUpdate(@RequestBody SysRole sysRole) throws Exception {
6565
return sysRoleService.saveOrUpdateRole(sysRole);
6666
}
6767

zlt-business/user-center/src/main/java/com/central/user/controller/SysUserController.java

+9-1
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919
import com.central.search.model.SearchDto;
2020
import com.central.user.model.SysUserExcel;
2121
import org.apache.commons.collections4.MapUtils;
22+
import org.redisson.api.RLock;
23+
import org.redisson.api.RedissonClient;
2224
import org.springframework.beans.factory.annotation.Autowired;
2325
import org.springframework.cache.annotation.CacheEvict;
2426
import org.springframework.cache.annotation.CachePut;
@@ -57,6 +59,9 @@ public class SysUserController {
5759
@Autowired
5860
private IQueryService queryService;
5961

62+
@Autowired
63+
private RedissonClient redisson;
64+
6065
/**
6166
* 当前登录用户 LoginAppUser
6267
*
@@ -65,6 +70,9 @@ public class SysUserController {
6570
@ApiOperation(value = "根据access_token当前登录用户")
6671
@GetMapping("/users/current")
6772
public Result<LoginAppUser> getLoginAppUser(@LoginUser(isFull = true) SysUser user) {
73+
RLock lock = redisson.getLock("test");
74+
lock.lock();
75+
6876
return Result.succeed(appUserService.getLoginAppUser(user));
6977
}
7078

@@ -236,7 +244,7 @@ public Result delete(@PathVariable Long id) {
236244
@CacheEvict(value = "user", key = "#sysUser.username")
237245
@PostMapping("/users/saveOrUpdate")
238246
@AuditLog(operation = "'新增或更新用户:' + #sysUser.username")
239-
public Result saveOrUpdate(@RequestBody SysUser sysUser) {
247+
public Result saveOrUpdate(@RequestBody SysUser sysUser) throws Exception {
240248
return appUserService.saveOrUpdateUser(sysUser);
241249
}
242250

zlt-business/user-center/src/main/java/com/central/user/service/ISysRoleService.java

+6-3
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,13 @@
99
import com.central.common.service.ISuperService;
1010

1111
/**
12-
* @author zlt
12+
* @author zlt
13+
* <p>
14+
* Blog: https://zlt2000.gitee.io
15+
* Github: https://github.com/zlt2000
1316
*/
1417
public interface ISysRoleService extends ISuperService<SysRole> {
15-
void saveRole(SysRole sysRole);
18+
void saveRole(SysRole sysRole) throws Exception;
1619

1720
void deleteRole(Long id);
1821

@@ -28,7 +31,7 @@ public interface ISysRoleService extends ISuperService<SysRole> {
2831
* @param sysRole
2932
* @return Result
3033
*/
31-
Result saveOrUpdateRole(SysRole sysRole);
34+
Result saveOrUpdateRole(SysRole sysRole) throws Exception;
3235

3336
/**
3437
* 查询所有角色

zlt-business/user-center/src/main/java/com/central/user/service/ISysUserService.java

+5-2
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,10 @@
1313
import com.central.common.model.SysUser;
1414

1515
/**
16-
* @author zlt
16+
* @author zlt
17+
* <p>
18+
* Blog: https://zlt2000.gitee.io
19+
* Github: https://github.com/zlt2000
1720
*/
1821
public interface ISysUserService extends ISuperService<SysUser> {
1922
/**
@@ -98,7 +101,7 @@ public interface ISysUserService extends ISuperService<SysUser> {
98101
*/
99102
List<SysUserExcel> findAllUsers(Map<String, Object> params);
100103

101-
Result saveOrUpdateUser(SysUser sysUser);
104+
Result saveOrUpdateUser(SysUser sysUser) throws Exception;
102105

103106
/**
104107
* 删除用户

zlt-business/user-center/src/main/java/com/central/user/service/impl/SysRoleServiceImpl.java

+3-3
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
@Slf4j
3030
@Service
3131
public class SysRoleServiceImpl extends SuperServiceImpl<SysRoleMapper, SysRole> implements ISysRoleService {
32-
private final static String LOCK_KEY_ROLECODE = CommonConstant.LOCK_KEY_PREFIX+"rolecode:";
32+
private final static String LOCK_KEY_ROLECODE = "rolecode:";
3333

3434
@Resource
3535
private SysUserRoleMapper userRoleMapper;
@@ -42,7 +42,7 @@ public class SysRoleServiceImpl extends SuperServiceImpl<SysRoleMapper, SysRole>
4242

4343
@Transactional(rollbackFor = Exception.class)
4444
@Override
45-
public void saveRole(SysRole sysRole) {
45+
public void saveRole(SysRole sysRole) throws Exception {
4646
String roleCode = sysRole.getCode();
4747
super.saveIdempotency(sysRole, lock
4848
, LOCK_KEY_ROLECODE+roleCode, new QueryWrapper<SysRole>().eq("code", roleCode), "角色code已存在");
@@ -67,7 +67,7 @@ public PageResult<SysRole> findRoles(Map<String, Object> params) {
6767

6868
@Override
6969
@Transactional
70-
public Result saveOrUpdateRole(SysRole sysRole) {
70+
public Result saveOrUpdateRole(SysRole sysRole) throws Exception {
7171
if (sysRole.getId() == null) {
7272
this.saveRole(sysRole);
7373
} else {

zlt-business/user-center/src/main/java/com/central/user/service/impl/SysUserServiceImpl.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@
3737
@Slf4j
3838
@Service
3939
public class SysUserServiceImpl extends SuperServiceImpl<SysUserMapper, SysUser> implements ISysUserService {
40-
private final static String LOCK_KEY_USERNAME = CommonConstant.LOCK_KEY_PREFIX+"username:";
40+
private final static String LOCK_KEY_USERNAME = "username:";
4141

4242
@Autowired
4343
private PasswordEncoder passwordEncoder;
@@ -219,7 +219,7 @@ public Result updateEnabled(Map<String, Object> params) {
219219

220220
@Transactional(rollbackFor = Exception.class)
221221
@Override
222-
public Result saveOrUpdateUser(SysUser sysUser) {
222+
public Result saveOrUpdateUser(SysUser sysUser) throws Exception {
223223
if (sysUser.getId() == null) {
224224
if (StringUtils.isBlank(sysUser.getType())) {
225225
sysUser.setType(UserType.BACKEND.name());

zlt-business/user-center/src/test/java/com/central/user/service/SysUserServiceTest.java

+17
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,21 @@
11
package com.central.user.service;
22

3+
import com.central.common.lock.DistributedLock;
34
import com.central.common.model.LoginAppUser;
45
import static org.assertj.core.api.Assertions.*;
56

67
import com.central.common.model.PageResult;
78
import com.central.common.model.SysUser;
89
import org.junit.Test;
910
import org.junit.runner.RunWith;
11+
import org.redisson.api.RLock;
1012
import org.springframework.beans.factory.annotation.Autowired;
1113
import org.springframework.boot.test.context.SpringBootTest;
1214
import org.springframework.test.context.junit4.SpringRunner;
1315

1416
import java.util.HashMap;
1517
import java.util.Map;
18+
import java.util.concurrent.TimeUnit;
1619

1720
/**
1821
* SysUserServiceTest单元测试用例
@@ -25,6 +28,20 @@ public class SysUserServiceTest {
2528
@Autowired
2629
private ISysUserService sysUserService;
2730

31+
@Autowired
32+
private DistributedLock locker;
33+
34+
@Test
35+
public void testLock() throws Exception {
36+
Object lock = null;
37+
try {
38+
lock = locker.tryLock("test", 1000, TimeUnit.MILLISECONDS, true);
39+
Thread.sleep(5000);
40+
} finally {
41+
locker.unlock(lock);
42+
}
43+
}
44+
2845
@Test
2946
public void testFindByUsername() {
3047
LoginAppUser loginAppUser = sysUserService.findByUsername("admin");

zlt-commons/zlt-common-core/src/main/java/com/central/common/lock/AbstractDistributedLock.java

-35
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -1,93 +1,48 @@
11
package com.central.common.lock;
22

3+
import java.util.concurrent.TimeUnit;
4+
35
/**
46
* 分布式锁顶级接口
5-
* 例如:
6-
* RETRY_TIMES=100,SLEEP_MILLIS=100
7-
* RETRY_TIMES * SLEEP_MILLIS = 10000 意味着如果一直获取不了锁,最长会等待10秒后抛超时异常
87
*
98
* @author zlt
109
* @date 2018/5/29 14:12
10+
* <p>
11+
* Blog: https://zlt2000.gitee.io
12+
* Github: https://github.com/zlt2000
1113
*/
1214
public interface DistributedLock {
13-
14-
/**
15-
* 默认超时时间
16-
*/
17-
long TIMEOUT_MILLIS = 5000;
18-
19-
/**
20-
* 重试次数
21-
*/
22-
int RETRY_TIMES = 100;
23-
24-
/**
25-
* 每次重试后等待的时间
26-
*/
27-
long SLEEP_MILLIS = 100;
28-
29-
/**
30-
* 获取锁
31-
*
32-
* @param key key
33-
* @return 成功/失败
34-
*/
35-
boolean lock(String key);
36-
37-
/**
38-
* 获取锁
39-
*
40-
* @param key key
41-
* @param retryTimes 重试次数
42-
* @return 成功/失败
43-
*/
44-
boolean lock(String key, int retryTimes);
45-
46-
/**
47-
* 获取锁
48-
*
49-
* @param key key
50-
* @param retryTimes 重试次数
51-
* @param sleepMillis 获取锁失败的重试间隔
52-
* @return 成功/失败
53-
*/
54-
boolean lock(String key, int retryTimes, long sleepMillis);
55-
56-
/**
57-
* 获取锁
58-
*
59-
* @param key key
60-
* @param expire 获取锁超时时间
61-
* @return 成功/失败
62-
*/
63-
boolean lock(String key, long expire);
64-
6515
/**
66-
* 获取锁
67-
*
68-
* @param key key
69-
* @param expire 获取锁超时时间
70-
* @param retryTimes 重试次数
71-
* @return 成功/失败
16+
* 获取锁,如果获取不成功则一直等待直到lock被获取
17+
* @param key 锁的key
18+
* @param leaseTime 加锁的时间,超过这个时间后锁便自动解锁;
19+
* 如果leaseTime为-1,则保持锁定直到显式解锁
20+
* @param unit {@code leaseTime} 参数的时间单位
21+
* @param isFair 是否公平锁
22+
* @return 锁对象
7223
*/
73-
boolean lock(String key, long expire, int retryTimes);
24+
Object lock(String key, long leaseTime, TimeUnit unit, boolean isFair) throws Exception;
25+
Object lock(String key, long leaseTime, TimeUnit unit) throws Exception;
26+
Object lock(String key, boolean isFair) throws Exception;
27+
Object lock(String key) throws Exception;
7428

7529
/**
76-
* 获取锁
77-
*
78-
* @param key key
79-
* @param expire 获取锁超时时间
80-
* @param retryTimes 重试次数
81-
* @param sleepMillis 获取锁失败的重试间隔
82-
* @return 成功/失败
30+
* 尝试获取锁,如果锁不可用则等待最多waitTime时间后放弃
31+
* @param key 锁的key
32+
* @param waitTime 获取锁的最大尝试时间(单位毫秒)
33+
* @param leaseTime 加锁的时间,超过这个时间后锁便自动解锁;
34+
* 如果leaseTime为-1,则保持锁定直到显式解锁
35+
* @param unit {@code waitTime} 和 {@code leaseTime} 参数的时间单位
36+
* @return 锁对象,如果获取锁失败则为null
8337
*/
84-
boolean lock(String key, long expire, int retryTimes, long sleepMillis);
38+
Object tryLock(String key, long waitTime, long leaseTime, TimeUnit unit, boolean isFair) throws Exception;
39+
Object tryLock(String key, long waitTime, long leaseTime, TimeUnit unit) throws Exception;
40+
Object tryLock(String key, long waitTime, TimeUnit unit, boolean isFair) throws Exception;
41+
Object tryLock(String key, long waitTime, TimeUnit unit) throws Exception;
8542

8643
/**
8744
* 释放锁
88-
*
89-
* @param key key值
90-
* @return 释放结果
45+
* @param lock 锁对象
9146
*/
92-
boolean releaseLock(String key);
47+
void unlock(Object lock) throws Exception;
9348
}

0 commit comments

Comments
 (0)