mirror of
https://github.com/wahyd4/Martian.git
synced 2026-08-09 05:16:21 +10:00
修复版本
This commit is contained in:
+1
-1
@@ -5,7 +5,7 @@
|
||||
<parent>
|
||||
<groupId>com.github.yuyenews</groupId>
|
||||
<artifactId>Mars</artifactId>
|
||||
<version>3.0.15-HOTFIX</version>
|
||||
<version>3.0.16</version>
|
||||
</parent>
|
||||
<artifactId>mars-core</artifactId>
|
||||
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package com.mars.aop.proxy;
|
||||
|
||||
import java.lang.reflect.Method;
|
||||
import java.util.UUID;
|
||||
|
||||
import com.mars.aop.proxy.exec.ExecAop;
|
||||
import com.mars.aop.proxy.exec.ExecRedisLock;
|
||||
@@ -47,6 +48,8 @@ public class MarsBeanProxy implements MethodInterceptor {
|
||||
AopModel tractionModel = null;
|
||||
RedisLock redisLock = null;
|
||||
Boolean hasLock = false;
|
||||
/* 分布式锁,解锁时的标识 */
|
||||
String val = UUID.randomUUID().toString();
|
||||
try {
|
||||
|
||||
MarsAop marsAop = method.getAnnotation(MarsAop.class);
|
||||
@@ -56,9 +59,8 @@ public class MarsBeanProxy implements MethodInterceptor {
|
||||
tractionModel = ExecTraction.getAopModel(traction);
|
||||
aopModel = ExecAop.getAopModel(marsAop);
|
||||
|
||||
|
||||
/* 加分布式锁 */
|
||||
hasLock = ExecRedisLock.lock(redisLock);
|
||||
hasLock = ExecRedisLock.lock(redisLock,val);
|
||||
if(!hasLock){
|
||||
return null;
|
||||
}
|
||||
@@ -85,10 +87,10 @@ public class MarsBeanProxy implements MethodInterceptor {
|
||||
ExecAop.exp(aopModel, e);
|
||||
throw e;
|
||||
} finally {
|
||||
/* 解分布式锁, 如果失败了就重试,十次之后还失败,就不管了,20秒后会自动解锁 */
|
||||
/* 解分布式锁, 如果失败了就重试,十次之后还失败,就不管了,10秒后会自动解锁 */
|
||||
if(hasLock){
|
||||
for(int i = 0;i<10;i++){
|
||||
Boolean hasUnlock = ExecRedisLock.unlock(redisLock);
|
||||
Boolean hasUnlock = ExecRedisLock.unlock(redisLock,val);
|
||||
if(hasUnlock){
|
||||
break;
|
||||
}
|
||||
|
||||
@@ -28,56 +28,63 @@ public class ExecRedisLock {
|
||||
|
||||
/**
|
||||
* 加锁
|
||||
*
|
||||
* @param redisLock 注解
|
||||
* @param value 值
|
||||
* @return 加锁结果
|
||||
*/
|
||||
public static Boolean lock(RedisLock redisLock) {
|
||||
return exec(redisLock,"lock");
|
||||
public static Boolean lock(RedisLock redisLock, String value) {
|
||||
return exec(redisLock, value, "lock");
|
||||
}
|
||||
|
||||
/**
|
||||
* 解锁
|
||||
*
|
||||
* @param redisLock 注解
|
||||
* @param value 值
|
||||
* @return 解锁结果
|
||||
*/
|
||||
public static Boolean unlock(RedisLock redisLock) {
|
||||
return exec(redisLock,"unlock");
|
||||
public static Boolean unlock(RedisLock redisLock, String value) {
|
||||
return exec(redisLock, value, "unlock");
|
||||
}
|
||||
|
||||
/**
|
||||
* 执行加解锁操作
|
||||
* @param redisLock 注解
|
||||
*
|
||||
* @param redisLock 注解
|
||||
* @param value 值
|
||||
* @param methodName 执行的方法
|
||||
* @return 结果
|
||||
*/
|
||||
private static Boolean exec(RedisLock redisLock,String methodName) {
|
||||
private static Boolean exec(RedisLock redisLock, String value, String methodName) {
|
||||
try {
|
||||
if(redisLock == null){
|
||||
if (redisLock == null) {
|
||||
/* 这个true代表不需要加解锁,为了让程序继续往下走 */
|
||||
return true;
|
||||
}
|
||||
redisLockClass = getRedisLockClass();
|
||||
redisLockObj = getRedisLockObj();
|
||||
Method method = redisLockClass.getMethod(methodName,new Class[]{String.class});
|
||||
Object result = method.invoke(redisLockObj,new Object[]{redisLock.key()});
|
||||
if(result == null){
|
||||
Method method = redisLockClass.getMethod(methodName, new Class[]{String.class, String.class});
|
||||
Object result = method.invoke(redisLockObj, new Object[]{redisLock.key(), value});
|
||||
if (result == null) {
|
||||
return false;
|
||||
}
|
||||
return Boolean.parseBoolean(result.toString());
|
||||
} catch (Exception e){
|
||||
logger.error("分布式锁出现异常["+methodName+"]",e);
|
||||
} catch (Exception e) {
|
||||
logger.error("分布式锁出现异常[" + methodName + "]", e);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取分布式锁的class对象
|
||||
*
|
||||
* @return 分布式锁的class对象
|
||||
* @throws Exception 异常
|
||||
*/
|
||||
private static Class<?> getRedisLockClass() throws Exception {
|
||||
/* 这里只是为了节约性能,在首次并发的情况下,即使执行了多次,也不会存在安全问题 */
|
||||
if(redisLockClass == null) {
|
||||
if (redisLockClass == null) {
|
||||
redisLockClass = Class.forName("com.mars.redis.lock.MarsRedisLock");
|
||||
}
|
||||
return redisLockClass;
|
||||
@@ -85,13 +92,14 @@ public class ExecRedisLock {
|
||||
|
||||
/**
|
||||
* 获取分布式锁的实例对象
|
||||
*
|
||||
* @return 分布式锁的实例对象
|
||||
* @throws Exception 异常
|
||||
*/
|
||||
private static Object getRedisLockObj() throws Exception {
|
||||
/* 这里只是为了节约性能,在首次并发的情况下,即使执行了多次,也不会存在安全问题 */
|
||||
if(redisLockObj == null) {
|
||||
redisLockObj = BeanFactory.getBean("marsRedisLock",Object.class);
|
||||
if (redisLockObj == null) {
|
||||
redisLockObj = BeanFactory.getBean("marsRedisLock", Object.class);
|
||||
}
|
||||
return redisLockObj;
|
||||
}
|
||||
|
||||
+1
-1
@@ -5,7 +5,7 @@
|
||||
<parent>
|
||||
<artifactId>Mars</artifactId>
|
||||
<groupId>com.github.yuyenews</groupId>
|
||||
<version>3.0.15-HOTFIX</version>
|
||||
<version>3.0.16</version>
|
||||
</parent>
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<artifactId>mars-jdbc</artifactId>
|
||||
|
||||
+1
-1
@@ -5,7 +5,7 @@
|
||||
<parent>
|
||||
<groupId>com.github.yuyenews</groupId>
|
||||
<artifactId>Mars</artifactId>
|
||||
<version>3.0.15-HOTFIX</version>
|
||||
<version>3.0.16</version>
|
||||
</parent>
|
||||
<artifactId>mars-mvc</artifactId>
|
||||
|
||||
|
||||
+1
-1
@@ -5,7 +5,7 @@
|
||||
<parent>
|
||||
<artifactId>Mars</artifactId>
|
||||
<groupId>com.github.yuyenews</groupId>
|
||||
<version>3.0.15-HOTFIX</version>
|
||||
<version>3.0.16</version>
|
||||
</parent>
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
|
||||
@@ -22,51 +22,53 @@ public class MarsRedisLock {
|
||||
|
||||
/**
|
||||
* 加锁,使用框架上配置的redis
|
||||
* @param key 键
|
||||
*
|
||||
* @param key 键
|
||||
* @param value 值
|
||||
* @return
|
||||
*/
|
||||
public boolean lock(String key){
|
||||
public boolean lock(String key, String value) {
|
||||
try {
|
||||
ShardedJedis shardedJedis = marsRedisTemplate.getShardedJedis();
|
||||
return lock(key ,shardedJedis);
|
||||
} catch (Exception e){
|
||||
logger.error("获取redis锁发生异常",e);
|
||||
return lock(key, value, shardedJedis);
|
||||
} catch (Exception e) {
|
||||
logger.error("获取redis锁发生异常", e);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 加锁,使用你自己创建的jedis对象
|
||||
* @param key 键
|
||||
*
|
||||
* @param key 键
|
||||
* @param value 值
|
||||
* @param shardedJedis 自己创建的jedis对象
|
||||
* @return
|
||||
*/
|
||||
public boolean lock(String key, ShardedJedis shardedJedis){
|
||||
public boolean lock(String key, String value, ShardedJedis shardedJedis) {
|
||||
try {
|
||||
if(shardedJedis == null){
|
||||
if (shardedJedis == null) {
|
||||
return false;
|
||||
}
|
||||
int count = 0;
|
||||
String value = "lock";
|
||||
|
||||
SetParams params = SetParams.setParams().nx().px(20000);
|
||||
String result = shardedJedis.set(key,value,params);
|
||||
|
||||
while(result == null || !result.toUpperCase().equals("OK")){
|
||||
SetParams params = SetParams.setParams().nx().px(5000);
|
||||
String result = shardedJedis.set(key, value, params);
|
||||
|
||||
while (result == null || !result.toUpperCase().equals("OK")) {
|
||||
/* 如果设置失败,代表这个key已经存在了,也就说明锁被占用了,则进入等待 */
|
||||
Thread.sleep(2000);
|
||||
if(count >= 9){
|
||||
/* 20秒后还没有获取锁,则停止等待 */
|
||||
Thread.sleep(500);
|
||||
if (count >= 19) {
|
||||
/* 10秒后还没有获取锁,则停止等待 */
|
||||
return false;
|
||||
}
|
||||
result = shardedJedis.set(key,value,params);
|
||||
result = shardedJedis.set(key, value, params);
|
||||
|
||||
count++;
|
||||
}
|
||||
return true;
|
||||
} catch (Exception e){
|
||||
logger.error("获取redis锁发生异常",e);
|
||||
} catch (Exception e) {
|
||||
logger.error("获取redis锁发生异常", e);
|
||||
return false;
|
||||
} finally {
|
||||
marsRedisTemplate.recycleJedis(shardedJedis);
|
||||
@@ -75,31 +77,38 @@ public class MarsRedisLock {
|
||||
|
||||
/**
|
||||
* 释放锁,使用框架上配置的redis
|
||||
* @param key 键
|
||||
*
|
||||
* @param key 键
|
||||
* @param value 值
|
||||
* @return
|
||||
*/
|
||||
public boolean unlock(String key){
|
||||
public boolean unlock(String key, String value) {
|
||||
try {
|
||||
ShardedJedis shardedJedis = marsRedisTemplate.getShardedJedis();
|
||||
return unlock(key ,shardedJedis);
|
||||
} catch (Exception e){
|
||||
logger.error("释放redis锁发生异常",e);
|
||||
return unlock(key, value, shardedJedis);
|
||||
} catch (Exception e) {
|
||||
logger.error("释放redis锁发生异常", e);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 释放锁,使用你自己创建的jedis对象
|
||||
* @param key 键
|
||||
*
|
||||
* @param key 键
|
||||
* @param value 值
|
||||
* @param shardedJedis 自己创建的jedis对象
|
||||
* @return
|
||||
*/
|
||||
public boolean unlock(String key, ShardedJedis shardedJedis){
|
||||
public boolean unlock(String key, String value, ShardedJedis shardedJedis) {
|
||||
try {
|
||||
if(shardedJedis == null){
|
||||
if (shardedJedis == null) {
|
||||
return false;
|
||||
}
|
||||
shardedJedis.del(key);
|
||||
String val = shardedJedis.get(key);
|
||||
if (val != null && val.equals(value)) {
|
||||
shardedJedis.del(key);
|
||||
}
|
||||
return true;
|
||||
} catch (Exception e) {
|
||||
logger.error("释放redis锁发生异常", e);
|
||||
|
||||
+1
-1
@@ -5,7 +5,7 @@
|
||||
<parent>
|
||||
<groupId>com.github.yuyenews</groupId>
|
||||
<artifactId>Mars</artifactId>
|
||||
<version>3.0.15-HOTFIX</version>
|
||||
<version>3.0.16</version>
|
||||
</parent>
|
||||
<artifactId>mars-server</artifactId>
|
||||
|
||||
|
||||
@@ -6,7 +6,7 @@
|
||||
<parent>
|
||||
<artifactId>Mars</artifactId>
|
||||
<groupId>com.github.yuyenews</groupId>
|
||||
<version>3.0.15-HOTFIX</version>
|
||||
<version>3.0.16</version>
|
||||
</parent>
|
||||
<artifactId>mars-starter</artifactId>
|
||||
|
||||
|
||||
+1
-1
@@ -5,7 +5,7 @@
|
||||
<parent>
|
||||
<groupId>com.github.yuyenews</groupId>
|
||||
<artifactId>Mars</artifactId>
|
||||
<version>3.0.15-HOTFIX</version>
|
||||
<version>3.0.16</version>
|
||||
</parent>
|
||||
<artifactId>mars-tomcat</artifactId>
|
||||
|
||||
|
||||
Reference in New Issue
Block a user