Skip to content

Commit f87a487

Browse files
committed
Redis分布式锁-RedisLockHelper
1 parent 5ae15d1 commit f87a487

File tree

6 files changed

+432
-2
lines changed

6 files changed

+432
-2
lines changed

SpringBoot/DelayTask/pom.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,11 @@
6464
<version>3.12.1</version>
6565
</dependency>
6666

67+
<!--<dependency>
68+
<groupId>org.springframework.boot</groupId>
69+
<artifactId>spring-boot-starter-jdbc</artifactId>
70+
</dependency>-->
71+
6772
</dependencies>
6873

6974
<build>

SpringBoot/DelayTask/src/main/java/com/example/controller/WebController.java

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,13 @@
33
import com.example.redisson.OrderDto;
44
import com.example.redisson.RedissonDelayedEnum;
55
import com.example.redisson.RedissonDelayedUtil;
6-
import org.redisson.RedissonFairLock;
6+
import com.example.redisson.RedisLockHelper;
77
import org.redisson.api.RLock;
88
import org.redisson.api.RedissonClient;
99
import org.springframework.beans.factory.annotation.Autowired;
1010
import org.springframework.web.bind.annotation.*;
1111

12+
import java.io.FileInputStream;
1213
import java.util.concurrent.TimeUnit;
1314

1415
/**
@@ -52,4 +53,20 @@ public void putName(@RequestParam("name") String name) {
5253
RedissonDelayedEnum.ORDER_DEFAULT_EVALUATION.timeUnit);
5354
}
5455

56+
@Autowired
57+
private RedisLockHelper redisLockHelper;
58+
59+
@GetMapping("test")
60+
public void test() {
61+
try {
62+
redisLockHelper.tryLock("order:pay:" + 1, () -> {
63+
// 业务逻辑
64+
FileInputStream fileInputStream = null;
65+
fileInputStream.read();
66+
});
67+
} catch (Exception e) {
68+
System.out.println(e);
69+
}
70+
}
71+
5572
}

SpringBoot/DelayTask/src/main/java/com/example/redisson/DelayedTask.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515

1616
/**
1717
* 延时任务启动执行,自动消费
18+
* https://www.jianshu.com/p/f80f833ab8f6
1819
*
1920
* @author wliduo[i@dolyw.com]
2021
* @date 2020/8/14 17:01
Lines changed: 304 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,304 @@
1+
package com.example.redisson;
2+
3+
import org.redisson.api.RLock;
4+
import org.redisson.api.RedissonClient;
5+
import org.slf4j.Logger;
6+
import org.slf4j.LoggerFactory;
7+
import org.springframework.beans.factory.annotation.Autowired;
8+
import org.springframework.stereotype.Component;
9+
import org.springframework.util.StringUtils;
10+
11+
import java.util.concurrent.TimeUnit;
12+
13+
/**
14+
* RedisLockHelper
15+
* https://zhuanlan.zhihu.com/p/128131107
16+
* https://zhuanlan.zhihu.com/p/130327922
17+
* https://gitee.com/zlt2000/microservices-platform/tree/master/zlt-commons/zlt-common-core/src/main/java/com/central/common/lock
18+
*
19+
* @author wliduo[i@dolyw.com]
20+
* @date 2021/9/15 15:33
21+
*/
22+
@Component
23+
public class RedisLockHelper {
24+
25+
/**
26+
* logger
27+
*/
28+
private static final Logger logger = LoggerFactory.getLogger(RedisLockHelper.class);
29+
30+
@Autowired
31+
private RedissonClient redissonClient;
32+
33+
/**
34+
* 获取锁 - 默认非公平锁
35+
*
36+
* @param lockKey
37+
* @return
38+
* @throws Exception
39+
*/
40+
public RLock getLock(String lockKey) throws Exception {
41+
return getLock(lockKey, false);
42+
}
43+
44+
/**
45+
* 获取锁
46+
*
47+
* @param lockKey
48+
* @param fair 公平锁
49+
* @return org.redisson.api.RLock
50+
* @throws
51+
* @author wliduo[i@dolyw.com]
52+
* @date 2022/2/22 14:47
53+
*/
54+
public RLock getLock(String lockKey, boolean fair) throws Exception {
55+
if (StringUtils.isEmpty(lockKey)) {
56+
throw new RuntimeException("分布式锁lockKey为空");
57+
}
58+
if (fair) {
59+
// 当多个客户端线程同时请求加锁时,公平锁优先分配给先发出请求的线程
60+
return redissonClient.getFairLock(lockKey);
61+
}
62+
return redissonClient.getLock(lockKey);
63+
}
64+
65+
/**
66+
* 加锁业务处理 - 默认非公平锁,5S自动释放锁
67+
*
68+
* @param lockKey
69+
* @param handle
70+
* @throws Exception
71+
*/
72+
public void lock(String lockKey, VoidHandle handle) throws Exception {
73+
lock(lockKey, false, 5, TimeUnit.SECONDS, handle);
74+
}
75+
76+
public void lock(String lockKey, boolean fair, VoidHandle handle) throws Exception {
77+
lock(lockKey, fair, 5, TimeUnit.SECONDS, handle);
78+
}
79+
80+
public void lock(String lockKey, long time, TimeUnit unit, VoidHandle handle) throws Exception {
81+
lock(lockKey, false, time, unit, handle);
82+
}
83+
84+
/**
85+
* 加锁业务处理
86+
*
87+
* @param lockKey
88+
* @param fair
89+
* @param time
90+
* @param unit
91+
* @param handle
92+
* @return void
93+
* @throws
94+
* @author wliduo[i@dolyw.com]
95+
* @date 2022/2/22 15:10
96+
*/
97+
public void lock(String lockKey, boolean fair, long time, TimeUnit unit, VoidHandle handle) throws Exception {
98+
RLock rLock = getLock(lockKey, fair);
99+
try {
100+
rLock.lock(time, unit);
101+
handle.execute();
102+
} finally {
103+
rLock.unlock();
104+
}
105+
}
106+
107+
/**
108+
* 返回值加锁业务处理 - 默认非公平锁,5S自动释放锁
109+
*
110+
* @param lockKey
111+
* @param handle
112+
* @throws Exception
113+
*/
114+
public <T> T lock(String lockKey, ReturnHandle<T> handle) throws Exception {
115+
return lock(lockKey, false, 5, TimeUnit.SECONDS, handle);
116+
}
117+
118+
public <T> T lock(String lockKey, boolean fair, ReturnHandle<T> handle) throws Exception {
119+
return lock(lockKey, fair, 5, TimeUnit.SECONDS, handle);
120+
}
121+
122+
public <T> T lock(String lockKey, long time, TimeUnit unit, ReturnHandle<T> handle) throws Exception {
123+
return lock(lockKey, false, time, unit, handle);
124+
}
125+
126+
/**
127+
* 返回值加锁业务处理
128+
*
129+
* @param lockKey
130+
* @param fair
131+
* @param time
132+
* @param unit
133+
* @param handle
134+
* @return void
135+
* @throws
136+
* @author wliduo[i@dolyw.com]
137+
* @date 2022/2/22 15:10
138+
*/
139+
public <T> T lock(String lockKey, boolean fair, long time, TimeUnit unit, ReturnHandle<T> handle) throws Exception {
140+
RLock rLock = getLock(lockKey, fair);
141+
try {
142+
rLock.lock(time, unit);
143+
return handle.execute();
144+
} finally {
145+
rLock.unlock();
146+
}
147+
}
148+
149+
/**
150+
* 尝试获取锁 - 默认非公平锁
151+
*
152+
* @param lockKey
153+
* @return
154+
* @throws Exception
155+
*/
156+
public Boolean getTryLock(String lockKey) throws Exception {
157+
return getTryLock(lockKey, false);
158+
}
159+
160+
/**
161+
* 尝试获取锁
162+
*
163+
* @param lockKey
164+
* @param fair 公平锁
165+
* @return java.lang.Boolean
166+
* @throws
167+
* @author wliduo[i@dolyw.com]
168+
* @date 2022/2/22 16:05
169+
*/
170+
public Boolean getTryLock(String lockKey, boolean fair) throws Exception {
171+
if (StringUtils.isEmpty(lockKey)) {
172+
throw new RuntimeException("分布式锁lockKey为空");
173+
}
174+
RLock rLock = null;
175+
if (fair) {
176+
// 当多个客户端线程同时请求加锁时,公平锁优先分配给先发出请求的线程
177+
rLock = redissonClient.getFairLock(lockKey);
178+
} else {
179+
rLock = redissonClient.getLock(lockKey);
180+
}
181+
return rLock.tryLock();
182+
}
183+
184+
/**
185+
* 尝试加锁业务处理 - 默认非公平锁,3S等待获取锁,5S自动释放锁
186+
*
187+
* @param lockKey
188+
* @param handle
189+
* @throws Exception
190+
*/
191+
public void tryLock(String lockKey, VoidHandle handle) throws Exception {
192+
tryLock(lockKey, false, 3, 5, TimeUnit.SECONDS, handle);
193+
}
194+
195+
public void tryLock(String lockKey, boolean fair, VoidHandle handle) throws Exception {
196+
tryLock(lockKey, fair, 3, 5, TimeUnit.SECONDS, handle);
197+
}
198+
199+
public void tryLock(String lockKey, long wait, long time, TimeUnit unit, VoidHandle handle) throws Exception {
200+
tryLock(lockKey, false, wait, time, unit, handle);
201+
}
202+
203+
/**
204+
* 尝试加锁业务处理
205+
*
206+
* @param lockKey
207+
* @param fair
208+
* @param wait
209+
* @param release
210+
* @param unit
211+
* @param handle
212+
* @return void
213+
* @throws
214+
* @author wliduo[i@dolyw.com]
215+
* @date 2022/2/22 15:40
216+
*/
217+
public void tryLock(String lockKey, boolean fair, long wait, long release, TimeUnit unit, VoidHandle handle) throws Exception {
218+
RLock rLock = getLock(lockKey, false);
219+
if (!rLock.tryLock(wait, release, unit)) {
220+
throw new RuntimeException("获取锁" + lockKey + "异常");
221+
}
222+
try {
223+
handle.execute();
224+
} finally {
225+
rLock.unlock();
226+
}
227+
}
228+
229+
/**
230+
* 返回值尝试加锁业务处理 - 默认非公平锁,3S等待获取锁,5S自动释放锁
231+
*
232+
* @param lockKey
233+
* @param handle
234+
* @throws Exception
235+
*/
236+
public <T> T tryLock(String lockKey, ReturnHandle<T> handle) throws Exception {
237+
return tryLock(lockKey, false, 3, 5, TimeUnit.SECONDS, handle);
238+
}
239+
240+
public <T> T tryLock(String lockKey, boolean fair, ReturnHandle<T> handle) throws Exception {
241+
return tryLock(lockKey, fair, 3, 5, TimeUnit.SECONDS, handle);
242+
}
243+
244+
public <T> T tryLock(String lockKey, long wait, long time, TimeUnit unit, ReturnHandle<T> handle) throws Exception {
245+
return tryLock(lockKey, false, wait, time, unit, handle);
246+
}
247+
248+
/**
249+
* 返回值尝试加锁业务处理
250+
*
251+
* @param lockKey
252+
* @param fair
253+
* @param wait
254+
* @param release
255+
* @param unit
256+
* @param handle
257+
* @return T
258+
* @throws
259+
* @author wliduo[i@dolyw.com]
260+
* @date 2022/2/22 15:51
261+
*/
262+
public <T> T tryLock(String lockKey, boolean fair, long wait, long release, TimeUnit unit, ReturnHandle<T> handle) throws Exception {
263+
RLock rLock = getLock(lockKey, false);
264+
if (!rLock.tryLock(wait, release, unit)) {
265+
throw new RuntimeException("获取锁" + lockKey + "异常");
266+
}
267+
try {
268+
return handle.execute();
269+
} finally {
270+
rLock.unlock();
271+
}
272+
}
273+
274+
/**
275+
* ReturnHandle
276+
*
277+
* @author wliduo[i@dolyw.com]
278+
* @date 2021/9/15 15:32
279+
*/
280+
public interface ReturnHandle<T> {
281+
282+
/**
283+
* 业务处理
284+
*/
285+
T execute() throws Exception;
286+
287+
}
288+
289+
/**
290+
* VoidHandle
291+
*
292+
* @author wliduo[i@dolyw.com]
293+
* @date 2021/9/15 15:32
294+
*/
295+
public interface VoidHandle {
296+
297+
/**
298+
* 业务处理
299+
*/
300+
void execute() throws Exception;
301+
302+
}
303+
304+
}

0 commit comments

Comments
 (0)