Skip to content

Commit 7522d27

Browse files
committed
redisson 分布式锁
1 parent cf27d1d commit 7522d27

File tree

7 files changed

+49
-90
lines changed

7 files changed

+49
-90
lines changed

springboot-redisson-lock/pom.xml

Lines changed: 10 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -3,37 +3,20 @@
33
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
44
<modelVersion>4.0.0</modelVersion>
55
<parent>
6-
<groupId>org.springframework.boot</groupId>
7-
<artifactId>spring-boot-starter-parent</artifactId>
8-
<version>2.2.5.RELEASE</version>
9-
<relativePath/>
6+
<groupId>com.notebook</groupId>
7+
<artifactId>Springboot-Notebook</artifactId>
8+
<version>0.0.1-SNAPSHOT</version>
109
</parent>
11-
<groupId>com.chengxy</groupId>
1210
<artifactId>springboot-redisson-lock</artifactId>
1311
<version>0.0.1-SNAPSHOT</version>
1412
<name>springboot-redisson-lock</name>
15-
<description>Demo project for Spring Boot</description>
16-
17-
<properties>
18-
<java.version>1.8</java.version>
19-
</properties>
2013

2114
<dependencies>
22-
<dependency>
23-
<groupId>org.springframework.boot</groupId>
24-
<artifactId>spring-boot-starter</artifactId>
25-
</dependency>
2615

16+
<!-- 模板引擎 -->
2717
<dependency>
2818
<groupId>org.springframework.boot</groupId>
29-
<artifactId>spring-boot-starter-test</artifactId>
30-
<scope>test</scope>
31-
<exclusions>
32-
<exclusion>
33-
<groupId>org.junit.vintage</groupId>
34-
<artifactId>junit-vintage-engine</artifactId>
35-
</exclusion>
36-
</exclusions>
19+
<artifactId>spring-boot-starter-thymeleaf</artifactId>
3720
</dependency>
3821

3922
<dependency>
@@ -42,18 +25,11 @@
4225
<version>3.11.4</version>
4326
</dependency>
4427

45-
<!--<dependency>-->
46-
<!--<groupId>org.springframework.boot</groupId>-->
47-
<!--<artifactId>spring-boot-starter-data-redis</artifactId>-->
48-
<!--<version>2.3.1.RELEASE</version>-->
49-
<!--</dependency>-->
50-
51-
<!--<dependency>-->
52-
<!--<groupId>org.redisson</groupId>-->
53-
<!--&lt;!&ndash; for Spring Data Redis v.2.2.x &ndash;&gt;-->
54-
<!--<artifactId>redisson-spring-data-22</artifactId>-->
55-
<!--<version>2.3.1.RELEASE</version>-->
56-
<!--</dependency>-->
28+
<dependency>
29+
<groupId>org.springframework.boot</groupId>
30+
<artifactId>spring-boot-starter-data-redis</artifactId>
31+
<version>2.3.1.RELEASE</version>
32+
</dependency>
5733

5834
</dependencies>
5935

springboot-redisson-lock/src/main/java/com/xiaofu/redisson/config/RedissonConfig.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
import org.springframework.context.annotation.Configuration;
77

88
/**
9-
* @Author: xinzhifu
9+
* @Author: xiaofu
1010
* @Description:
1111
*/
1212
@Configuration

springboot-redisson-lock/src/main/java/com/xiaofu/redisson/controller/LiveObjectController.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.xiaofu.redisson.controller;
22

3+
34
import com.xiaofu.redisson.entity.OrderInfo;
45
import org.redisson.api.RLiveObjectService;
56
import org.redisson.api.RedissonClient;
@@ -9,7 +10,7 @@
910
import org.springframework.web.bind.annotation.ResponseBody;
1011

1112
/**
12-
* @Author: xinzhifu
13+
* @Author: xiaofu
1314
* @Description:
1415
*/
1516
@Controller

springboot-redisson-lock/src/main/java/com/xiaofu/redisson/controller/LockController.java

Lines changed: 31 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -6,83 +6,75 @@
66
import org.redisson.api.RedissonClient;
77
import org.springframework.beans.factory.annotation.Autowired;
88
import org.springframework.stereotype.Controller;
9+
import org.springframework.web.bind.annotation.GetMapping;
910
import org.springframework.web.bind.annotation.RequestMapping;
1011
import org.springframework.web.bind.annotation.ResponseBody;
1112

1213
import java.util.concurrent.TimeUnit;
1314

1415
/**
15-
* @Author: xinzhifu
16+
* @Author: xiaofu
1617
* @Description:
1718
*/
1819
@Controller
19-
@RequestMapping
20+
@RequestMapping("/lock")
2021
public class LockController {
2122

2223
@Autowired
2324
private RedissonClient redissonClient;
2425

25-
@RequestMapping("/reduceStock")
26-
@ResponseBody
27-
public String reduceStock() {
28-
29-
RLock lock = redissonClient.getLock("order:A0001");
30-
try {
31-
lock.lock(5, TimeUnit.SECONDS);
32-
Thread.currentThread().sleep(30000);
33-
lock.unlock();
34-
System.out.println("锁释放~");
35-
36-
} catch (Exception e) {
37-
lock.unlock();
38-
}
39-
return null;
40-
}
41-
42-
4326
/**
44-
* @author xinzhifu
27+
* @author xiaofu
4528
* @description 可重入锁-普通
4629
* @date 2020/7/16 15:20
4730
*/
48-
public void reentrantLock() {
31+
@GetMapping("/reduceStock2")
32+
@ResponseBody
33+
public String reentrantLock(String order) {
4934

50-
RLock reentrantLock = redissonClient.getLock("order:A0002");
35+
RLock reentrantLock = redissonClient.getLock(order);
5136
try {
52-
reentrantLock.lock();
53-
//TODO something
54-
reentrantLock.unlock();
55-
37+
if (reentrantLock.tryLock(100, 8, TimeUnit.SECONDS)) {
38+
System.out.println(Thread.currentThread().getName() + "---" + order + "---" + "加锁成功");
39+
Thread.currentThread().sleep(10000);
40+
return "YES";
41+
} else {
42+
System.out.println("线程 " + Thread.currentThread().getName() + "---" + order + "---" + "加锁失败");
43+
}
5644
} catch (Exception e) {
57-
reentrantLock.unlock();
45+
//reentrantLock.unlock();
5846
} finally {
59-
reentrantLock.unlock();
47+
//reentrantLock.unlock();
6048
}
49+
return "NO";
6150
}
6251

6352
/**
64-
* @author xinzhifu
53+
* @author xiaofu
6554
* @description 可重入锁-设置过期时间
6655
* @date 2020/7/16 15:20
6756
*/
68-
public void reentrantLock2() {
57+
public String reentrantLock2(String order) {
6958

70-
RLock reentrantLock = redissonClient.getLock("order:A0001");
59+
RLock reentrantLock = redissonClient.getLock(order);
7160
try {
72-
7361
// 100尝试加锁的时间 ,10锁过期时间
7462
if (reentrantLock.tryLock(100, 10, TimeUnit.SECONDS)) {
75-
//TODO something
63+
//Thread.currentThread().sleep(10000);
64+
System.out.println(order + "加锁成功");
65+
} else {
66+
System.out.println(order + "已加锁");
7667
}
7768
} catch (Exception e) {
7869
reentrantLock.unlock();
7970
} finally {
8071
reentrantLock.unlock();
8172
}
73+
return "success";
8274
}
8375

8476
/**
85-
* @author xinzhifu
77+
* @author xiaofu
8678
* @description 异步可重入锁-设置过期时间
8779
* @date 2020/7/16 15:20
8880
*/
@@ -104,7 +96,7 @@ public void reentrantLock2Async() {
10496
}
10597

10698
/**
107-
* @author xinzhifu
99+
* @author xiaofu
108100
* @description 公平锁
109101
* @date 2020/7/16 15:20
110102
*/
@@ -128,7 +120,7 @@ public void fairLock() {
128120
}
129121

130122
/**
131-
* @author xinzhifu
123+
* @author xiaofu
132124
* @description 联锁
133125
* @date 2020/7/16 15:20
134126
*/
@@ -151,7 +143,7 @@ public void multiLock() {
151143
}
152144

153145
/**
154-
* @author xinzhifu
146+
* @author xiaofu
155147
* @description 红锁
156148
* @date 2020/7/16 15:20
157149
*/
@@ -174,7 +166,7 @@ public void redLock() {
174166
}
175167

176168
/**
177-
* @author xinzhifu
169+
* @author xiaofu
178170
* @description 读写锁
179171
* @date 2020/7/16 15:20
180172
*/

springboot-redisson-lock/src/main/java/com/xiaofu/redisson/entity/OrderInfo.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
import org.redisson.api.annotation.RIndex;
66

77
/**
8-
* @Author: xinzhifu
8+
* @Author: xiaofu
99
* @Description:
1010
*/
1111
@REntity
Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
spring:
22
redis:
33
redisson:
4-
config: "classpath:redisson.yml"
4+
config: "classpath:redisson.yml"
5+
server:
6+
port: 8083
7+

springboot-redisson-lock/src/test/java/com/xiaofu/redisson/SpringbootRedissonLockApplicationTests.java

Lines changed: 0 additions & 13 deletions
This file was deleted.

0 commit comments

Comments
 (0)