From f098222a43bb37a94563fd83932d8cc2becac1c2 Mon Sep 17 00:00:00 2001
From: 疯狂的狮子li <15040126243@163.com>
Date: 星期二, 22 六月 2021 16:45:37 +0800
Subject: [PATCH] update 集成 Lock4j 分布式锁
---
ruoyi-demo/src/main/java/com/ruoyi/demo/service/impl/TestDemoServiceImpl.java | 45 ---------------
ruoyi-admin/src/main/resources/application.yml | 9 ++
ruoyi-demo/src/main/java/com/ruoyi/demo/controller/RedisLockController.java | 49 ++++++++++++----
ruoyi-demo/src/main/java/com/ruoyi/demo/service/ITestDemoService.java | 4 -
README.md | 3 +
ruoyi-admin/src/main/resources/application-dev.yml | 38 +++++++-----
6 files changed, 71 insertions(+), 77 deletions(-)
diff --git a/README.md b/README.md
index 65f335e..9c5dbd7 100644
--- a/README.md
+++ b/README.md
@@ -21,6 +21,8 @@
* 搴忓垪鍖栨鏋� 缁熶竴浣跨敤 jackson 楂樻晥鍙潬
* 浠g爜鐢熸垚鍣� 涓�閿敓鎴愬墠鍚庣浠g爜
* 澶氭暟鎹簮妗嗘灦 dynamic-datasource 鏀寔涓讳粠涓庡绉嶇被鏁版嵁搴撳紓鏋�
+* Redis瀹㈡埛绔� 閲囩敤 Redisson 鎬ц兘鏇村己
+* 鍒嗗竷寮忛攣 Lock4j 娉ㄨВ閿併�佸伐鍏烽攣 澶氱澶氭牱
## 鍙傝�冩枃妗�
@@ -43,6 +45,7 @@
* 澧炲姞 redisson 楂樻�ц兘 Redis 瀹㈡埛绔�
* 绉婚櫎 fastjson 缁熶竴浣跨敤 jackson 搴忓垪鍖�
* 闆嗘垚 dynamic-datasource 澶氭暟鎹簮(榛樿鏀寔MySQL,鍏朵粬绉嶇被闇�鑷閫傞厤)
+* 闆嗘垚 Lock4j 瀹炵幇鍒嗗竷寮� 娉ㄨВ閿併�佸伐鍏烽攣 澶氱澶氭牱
### 浠g爜鏀瑰姩
diff --git a/ruoyi-admin/src/main/resources/application-dev.yml b/ruoyi-admin/src/main/resources/application-dev.yml
index f3d1029..35901b5 100644
--- a/ruoyi-admin/src/main/resources/application-dev.yml
+++ b/ruoyi-admin/src/main/resources/application-dev.yml
@@ -1,21 +1,27 @@
# 鏁版嵁婧愰厤缃�
spring:
+ autoconfigure:
+ exclude: com.alibaba.druid.spring.boot.autoconfigure.DruidDataSourceAutoConfigure
datasource:
type: com.alibaba.druid.pool.DruidDataSource
- driverClassName: com.mysql.cj.jdbc.Driver
+ # 鍔ㄦ�佹暟鎹簮鏂囨。 https://www.kancloud.cn/tracy5546/dynamic-datasource/content
+ dynamic:
+ #璁剧疆榛樿鐨勬暟鎹簮鎴栬�呮暟鎹簮缁�,榛樿鍊煎嵆涓� master
+ primary: master
+ datasource:
+ # 涓诲簱鏁版嵁婧�
+ master:
+ driverClassName: com.mysql.cj.jdbc.Driver
+ url: jdbc:mysql://localhost:3306/ry-vue?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=true&serverTimezone=GMT%2B8&autoReconnect=true
+ username: root
+ password: root
+ # 浠庡簱鏁版嵁婧�
+ slave:
+ driverClassName: com.mysql.cj.jdbc.Driver
+ url:
+ username:
+ password:
druid:
- # 涓诲簱鏁版嵁婧�
- master:
- url: jdbc:mysql://47.118.76.192:3306/ry_vue?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=true&serverTimezone=GMT%2B8&autoReconnect=true
- username: root
- password: Shen1992#
- # 浠庡簱鏁版嵁婧�
- slave:
- # 浠庢暟鎹簮寮�鍏�/榛樿鍏抽棴
- enabled: false
- url:
- username:
- password:
# 鍒濆杩炴帴鏁�
initialSize: 5
# 鏈�灏忚繛鎺ユ睜鏁伴噺
@@ -35,6 +41,8 @@
testWhileIdle: true
testOnBorrow: false
testOnReturn: false
+ # 娉ㄦ剰杩欎釜鍊煎拰druid鍘熺敓涓嶄竴鑷达紝榛樿鍚姩浜唖tat
+ filters: stat
webStatFilter:
enabled: true
statViewServlet:
@@ -58,13 +66,13 @@
# redis 閰嶇疆
redis:
# 鍦板潃
- host: 47.118.76.192
+ host: localhost
# 绔彛锛岄粯璁や负6379
port: 6379
# 鏁版嵁搴撶储寮�
database: 0
# 瀵嗙爜
- password: "019920212"
+ password:
# 杩炴帴瓒呮椂鏃堕棿
timeout: 10s
# 鏄惁寮�鍚痵sl
diff --git a/ruoyi-admin/src/main/resources/application.yml b/ruoyi-admin/src/main/resources/application.yml
index ef50842..e153d19 100644
--- a/ruoyi-admin/src/main/resources/application.yml
+++ b/ruoyi-admin/src/main/resources/application.yml
@@ -15,7 +15,7 @@
captcha:
# 楠岃瘉鐮佸紑鍏�
- enabled: false
+ enabled: true
# 楠岃瘉鐮佺被鍨� math 鏁扮粍璁$畻 char 瀛楃楠岃瘉
type: math
# line 绾挎骞叉壈 circle 鍦嗗湀骞叉壈 shear 鎵洸骞叉壈
@@ -256,6 +256,13 @@
circuitbreaker:
enabled: true
+--- # 鍒嗗竷寮忛攣 lock4j 鍏ㄥ眬閰嶇疆
+lock4j:
+ # 鑾峰彇鍒嗗竷寮忛攣瓒呮椂鏃堕棿锛岄粯璁や负 3000 姣
+ acquire-timeout: 3000
+ # 鍒嗗竷寮忛攣鐨勮秴鏃舵椂闂达紝榛樿涓� 30 姣
+ expire: 30000
+
--- # 瀹氭椂浠诲姟閰嶇疆
spring:
quartz:
diff --git a/ruoyi-demo/src/main/java/com/ruoyi/demo/controller/RedisLockController.java b/ruoyi-demo/src/main/java/com/ruoyi/demo/controller/RedisLockController.java
index b872a5f..f6649ed 100644
--- a/ruoyi-demo/src/main/java/com/ruoyi/demo/controller/RedisLockController.java
+++ b/ruoyi-demo/src/main/java/com/ruoyi/demo/controller/RedisLockController.java
@@ -1,14 +1,18 @@
package com.ruoyi.demo.controller;
+import com.baomidou.lock.LockInfo;
+import com.baomidou.lock.LockTemplate;
+import com.baomidou.lock.annotation.Lock4j;
+import com.baomidou.lock.executor.RedissonLockExecutor;
import com.ruoyi.common.core.domain.AjaxResult;
-import com.ruoyi.common.core.redis.RedisLockManager;
-import com.ruoyi.demo.service.ITestDemoService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
+
+import java.time.LocalTime;
/**
@@ -22,27 +26,48 @@
public class RedisLockController {
@Autowired
- private ITestDemoService testDemoService;
+ private LockTemplate lockTemplate;
/**
- * 娴嬭瘯lock4j
- * @param key
- * @param value
- * @return
+ * 娴嬭瘯lock4j 娉ㄨВ
*/
+ @Lock4j(keys = {"#key"})
@GetMapping("/testLock4j")
public AjaxResult<String> testLock4j(String key,String value){
- testDemoService.testLock4j(key);
+ System.out.println("start:"+key+",time:"+ LocalTime.now().toString());
+ try {
+ Thread.sleep(10000);
+ } catch (InterruptedException e) {
+ e.printStackTrace();
+ }
+ System.out.println("end :"+key+",time:"+LocalTime.now().toString());
return AjaxResult.success("鎿嶄綔鎴愬姛",value);
}
+
+ /**
+ * 娴嬭瘯lock4j 宸ュ叿
+ */
@GetMapping("/testLock4jLockTemaplate")
public AjaxResult<String> testLock4jLockTemaplate(String key,String value){
- testDemoService.testLock4jLockTemaplate(key);
+ final LockInfo lockInfo = lockTemplate.lock(key, 30000L, 5000L, RedissonLockExecutor.class);
+ if (null == lockInfo) {
+ throw new RuntimeException("涓氬姟澶勭悊涓�,璇风◢鍚庡啀璇�");
+ }
+ // 鑾峰彇閿佹垚鍔燂紝澶勭悊涓氬姟
+ try {
+ try {
+ Thread.sleep(8000);
+ } catch (InterruptedException e) {
+ //
+ }
+ System.out.println("鎵ц绠�鍗曟柟娉�1 , 褰撳墠绾跨▼:" + Thread.currentThread().getName());
+ } finally {
+ //閲婃斁閿�
+ lockTemplate.releaseLock(lockInfo);
+ }
+ //缁撴潫
return AjaxResult.success("鎿嶄綔鎴愬姛",value);
}
-
-
-
/**
* 娴嬭瘯spring-cache娉ㄨВ
diff --git a/ruoyi-demo/src/main/java/com/ruoyi/demo/service/ITestDemoService.java b/ruoyi-demo/src/main/java/com/ruoyi/demo/service/ITestDemoService.java
index 36f300c..c011325 100644
--- a/ruoyi-demo/src/main/java/com/ruoyi/demo/service/ITestDemoService.java
+++ b/ruoyi-demo/src/main/java/com/ruoyi/demo/service/ITestDemoService.java
@@ -19,10 +19,6 @@
*/
public interface ITestDemoService extends IServicePlus<TestDemo> {
- void testLock4j(String key);
-
- void testLock4jLockTemaplate(String key);
-
/**
* 鏌ヨ鍗曚釜
* @return
diff --git a/ruoyi-demo/src/main/java/com/ruoyi/demo/service/impl/TestDemoServiceImpl.java b/ruoyi-demo/src/main/java/com/ruoyi/demo/service/impl/TestDemoServiceImpl.java
index 8d80e39..4d35efe 100644
--- a/ruoyi-demo/src/main/java/com/ruoyi/demo/service/impl/TestDemoServiceImpl.java
+++ b/ruoyi-demo/src/main/java/com/ruoyi/demo/service/impl/TestDemoServiceImpl.java
@@ -2,18 +2,12 @@
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
-import com.baomidou.lock.LockInfo;
-import com.baomidou.lock.LockTemplate;
-import com.baomidou.lock.annotation.Lock4j;
-import com.baomidou.lock.executor.RedissonLockExecutor;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruoyi.common.annotation.DataScope;
-import com.ruoyi.common.annotation.DataSource;
import com.ruoyi.common.core.mybatisplus.core.ServicePlusImpl;
import com.ruoyi.common.core.page.PagePlus;
import com.ruoyi.common.core.page.TableDataInfo;
-import com.ruoyi.common.enums.DataSourceType;
import com.ruoyi.common.utils.PageUtils;
import com.ruoyi.demo.bo.TestDemoAddBo;
import com.ruoyi.demo.bo.TestDemoEditBo;
@@ -22,10 +16,8 @@
import com.ruoyi.demo.mapper.TestDemoMapper;
import com.ruoyi.demo.service.ITestDemoService;
import com.ruoyi.demo.vo.TestDemoVo;
-import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
-import java.time.LocalTime;
import java.util.Collection;
import java.util.List;
import java.util.Map;
@@ -38,43 +30,6 @@
*/
@Service
public class TestDemoServiceImpl extends ServicePlusImpl<TestDemoMapper, TestDemo> implements ITestDemoService {
-
-
- @Autowired
- private LockTemplate lockTemplate;
-
- @Override
- public void testLock4jLockTemaplate(String key) {
- final LockInfo lockInfo = lockTemplate.lock(key, 30000L, 5000L, RedissonLockExecutor.class);
- if (null == lockInfo) {
- throw new RuntimeException("涓氬姟澶勭悊涓�,璇风◢鍚庡啀璇�");
- }
- // 鑾峰彇閿佹垚鍔燂紝澶勭悊涓氬姟
- try {
- try {
- Thread.sleep(8000);
- } catch (InterruptedException e) {
- //
- }
- System.out.println("鎵ц绠�鍗曟柟娉�1 , 褰撳墠绾跨▼:" + Thread.currentThread().getName());
- } finally {
- //閲婃斁閿�
- lockTemplate.releaseLock(lockInfo);
- }
- //缁撴潫
- }
-
- @Override
- @Lock4j(executor = RedissonLockExecutor.class,keys = {"#key"})
- public void testLock4j(String key) {
- System.out.println("start:"+key+",time:"+LocalTime.now().toString());
- try {
- Thread.sleep(10000);
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- System.out.println("end :"+key+",time:"+LocalTime.now().toString());
- }
@Override
public TestDemoVo queryById(Long id) {
--
Gitblit v1.9.3