From c3636f1527cfc0041a7b54396da7f4ff2dd71e4f Mon Sep 17 00:00:00 2001
From: 疯狂的狮子li <15040126243@163.com>
Date: 星期三, 16 六月 2021 19:24:44 +0800
Subject: [PATCH] update 更新 feign demo 更清晰的用法
---
ruoyi-common/src/main/java/com/ruoyi/common/core/redis/RedisLockManager.java | 100 ++++++++++++++++++++++++++++++++++---------------
1 files changed, 69 insertions(+), 31 deletions(-)
diff --git a/ruoyi-common/src/main/java/com/ruoyi/common/core/redis/RedisLockManager.java b/ruoyi-common/src/main/java/com/ruoyi/common/core/redis/RedisLockManager.java
index fb665a9..0688bb1 100644
--- a/ruoyi-common/src/main/java/com/ruoyi/common/core/redis/RedisLockManager.java
+++ b/ruoyi-common/src/main/java/com/ruoyi/common/core/redis/RedisLockManager.java
@@ -22,15 +22,43 @@
@Autowired
private RedissonClient redissonClient;
- private final ThreadLocal<RLock> threadLocal = new ThreadLocal<>();
+ /**
+ * 閫氱敤閿�
+ */
+ private final static Integer BASE_LOCK = 1;
+
+ /**
+ * 鍏钩閿�
+ */
+ private final static Integer FAIR_LOCK = 2;
+
+ /**
+ * 瀛樻斁褰撳墠绾跨▼鑾峰彇閿佺殑绫诲瀷
+ */
+ private final ThreadLocal<Integer> threadLocal = new ThreadLocal<>();
+
+ /**
+ * 鑾峰彇閿�
+ */
+ private RLock getLock(String key, Integer lockType) {
+ Assert.isTrue(StrUtil.isNotBlank(key), "key涓嶈兘涓虹┖");
+ threadLocal.set(lockType);
+ RLock lock;
+ if (BASE_LOCK.equals(lockType)) {
+ lock = redissonClient.getLock(key);
+ } else if (FAIR_LOCK.equals(lockType)) {
+ lock = redissonClient.getFairLock(key);
+ } else {
+ throw new RuntimeException("閿佷笉瀛樺湪!");
+ }
+ return lock;
+ }
/**
* 鑾峰彇閿侊紙涓嶇敤璁剧疆瓒呮椂鏃堕棿锛屼竴鐩寸瓑寰咃級
*/
public boolean getLock(String key) {
- Assert.isTrue(StrUtil.isNotBlank(key), "key涓嶈兘涓虹┖");
- RLock lock = redissonClient.getLock(key);
- threadLocal.set(lock);
+ RLock lock = getLock(key, BASE_LOCK);
return lock.tryLock();
}
@@ -41,13 +69,16 @@
* @param time 杩囨湡鏃堕棿
* @param expireUnit 鏃堕棿鍗曚綅
*/
- public boolean getLock(String key, long time, TimeUnit expireUnit) throws InterruptedException {
- Assert.isTrue(StrUtil.isNotBlank(key), "key涓嶈兘涓虹┖");
+ public boolean getLock(String key, long time, TimeUnit expireUnit) {
Assert.isTrue(time > 0, "杩囨湡鏃堕棿蹇呴』澶т簬0");
Assert.isTrue(Validator.isNotEmpty(expireUnit), "鏃堕棿鍗曚綅涓嶈兘涓虹┖");
- RLock lock = redissonClient.getLock(key);
- threadLocal.set(lock);
- return lock.tryLock(time, expireUnit);
+ RLock lock = getLock(key, BASE_LOCK);
+ try {
+ return lock.tryLock(time, expireUnit);
+ } catch (InterruptedException e) {
+ e.printStackTrace();
+ return false;
+ }
}
/**
@@ -57,17 +88,20 @@
* @param waitTime 鑾峰彇閿佺瓑寰呮椂闂�
* @param leaseTime 淇濈暀閿佺殑鏃堕棿
* @param expireUnit 鏃堕棿鍗曚綅
- * @throws InterruptedException
*/
- public boolean getLock(String key, long waitTime, long leaseTime, TimeUnit expireUnit) throws InterruptedException {
- Assert.isTrue(StrUtil.isNotBlank(key), "key涓嶈兘涓虹┖");
+ public boolean getLock(String key, long waitTime, long leaseTime, TimeUnit expireUnit) {
Assert.isTrue(waitTime > 0, "鑾峰彇閿佺瓑寰呮椂闂村繀椤诲ぇ浜�0");
Assert.isTrue(leaseTime > 0, "淇濈暀閿佺殑鏃堕棿蹇呴』澶т簬0");
Assert.isTrue(Validator.isNotEmpty(expireUnit), "鏃堕棿鍗曚綅涓嶈兘涓虹┖");
- RLock lock = redissonClient.getLock(key);
- threadLocal.set(lock);
- return lock.tryLock(waitTime, leaseTime, expireUnit);
+ RLock lock = getLock(key, BASE_LOCK);
+ try {
+ return lock.tryLock(waitTime, leaseTime, expireUnit);
+ } catch (InterruptedException e) {
+ e.printStackTrace();
+ return false;
+ }
}
+
/**
* 鑾峰彇璁℃暟鍣ㄩ攣
@@ -75,8 +109,7 @@
* @param key
* @param count countDownLatch 鐨勬暟閲�
*/
- public RCountDownLatch countDownLatch(String key, long count) {
- Assert.isTrue(StrUtil.isNotBlank(key), "key涓嶈兘涓虹┖");
+ public RCountDownLatch getCountDownLatch(String key, long count) {
Assert.isTrue(count >= 0, "count鏁伴噺蹇呴』澶т簬绛変簬0");
RCountDownLatch rCountDownLatch = redissonClient.getCountDownLatch(key);
rCountDownLatch.trySetCount(count);
@@ -93,14 +126,17 @@
* @return
* @throws InterruptedException
*/
- public boolean getFairLock(String key, long waitTime, long leaseTime, TimeUnit expireUnit) throws InterruptedException {
- Assert.isTrue(StrUtil.isNotBlank(key), "key涓嶈兘涓虹┖");
+ public boolean getFairLock(String key, long waitTime, long leaseTime, TimeUnit expireUnit) {
Assert.isTrue(waitTime > 0, "鑾峰彇閿佺瓑寰呮椂闂村繀椤诲ぇ浜�0");
Assert.isTrue(leaseTime > 0, "淇濈暀閿佺殑鏃堕棿蹇呴』澶т簬0");
Assert.isTrue(Validator.isNotEmpty(expireUnit), "鏃堕棿鍗曚綅涓嶈兘涓虹┖");
- RLock lock = redissonClient.getFairLock(key);
- threadLocal.set(lock);
- return lock.tryLock(waitTime, leaseTime, expireUnit);
+ RLock lock = getLock(key, FAIR_LOCK);
+ try {
+ return lock.tryLock(waitTime, leaseTime, expireUnit);
+ } catch (InterruptedException e) {
+ e.printStackTrace();
+ return false;
+ }
}
/**
@@ -109,23 +145,25 @@
* @param key
* @param leaseTime 鎸佹湁閿佺殑鏃堕棿
* @param expireUnit 鏃堕棿鍗曚綅
- * @return
- * @throws InterruptedException
*/
- public boolean getFairLock(String key, long leaseTime, TimeUnit expireUnit) throws InterruptedException {
- Assert.isTrue(StrUtil.isNotBlank(key), "key涓嶈兘涓虹┖");
+ public boolean getFairLock(String key, long leaseTime, TimeUnit expireUnit) {
Assert.isTrue(leaseTime > 0, "淇濈暀閿佺殑鏃堕棿蹇呴』澶т簬0");
Assert.isTrue(Validator.isNotEmpty(expireUnit), "鏃堕棿鍗曚綅涓嶈兘涓虹┖");
- RLock lock = redissonClient.getFairLock(key);
- threadLocal.set(lock);
- return lock.tryLock(leaseTime, expireUnit);
+ RLock lock = getLock(key, FAIR_LOCK);
+ try {
+ return lock.tryLock(leaseTime, expireUnit);
+ } catch (InterruptedException e) {
+ e.printStackTrace();
+ return false;
+ }
}
/**
* 閲婃斁閿�(缁熶竴閲婃斁)
*/
- public void unLock() {
- RLock lock = threadLocal.get();
+ public void unLock(String key) {
+ Integer lockType = threadLocal.get();
+ RLock lock = getLock(key, lockType);
lock.unlock();
threadLocal.remove();
}
--
Gitblit v1.9.3