release branch 1.1.0

This commit is contained in:
binbin.hou
2022-12-07 21:00:45 +08:00
parent 27a931ae77
commit c45f6b53a0
27 changed files with 444 additions and 78 deletions

View File

@@ -5,7 +5,7 @@
<parent>
<artifactId>lock</artifactId>
<groupId>com.github.houbb</groupId>
<version>1.0.0</version>
<version>1.1.0</version>
</parent>
<modelVersion>4.0.0</modelVersion>
@@ -25,6 +25,11 @@
<groupId>com.github.houbb</groupId>
<artifactId>aop-spring</artifactId>
</dependency>
<dependency>
<groupId>com.github.houbb</groupId>
<artifactId>redis-config-spring</artifactId>
</dependency>
</dependencies>
</project>
</project>

View File

@@ -34,10 +34,13 @@ public @interface EnableLock {
/**
* 缓存实现策略 bean 名称
*
* 默认引入 redis-config 中的配置
*
* @return 实现
* @since 1.1.0
*/
String cache() default "lockCache";
String cache() default "springRedisService";
}

View File

@@ -3,7 +3,6 @@ package com.github.houbb.lock.spring.aop;
import com.github.houbb.aop.spring.util.SpringAopUtil;
import com.github.houbb.heaven.util.lang.ObjectUtil;
import com.github.houbb.heaven.util.lang.StringUtil;
import com.github.houbb.heaven.util.lang.reflect.ReflectMethodUtil;
import com.github.houbb.heaven.util.util.ArrayUtil;
import com.github.houbb.lock.api.exception.LockException;
import com.github.houbb.lock.core.bs.LockBs;
@@ -11,9 +10,11 @@ import com.github.houbb.lock.spring.annotation.Lock;
import com.github.houbb.log.integration.core.Log;
import com.github.houbb.log.integration.core.LogFactory;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.expression.EvaluationContext;
@@ -23,8 +24,8 @@ import org.springframework.expression.spel.support.StandardEvaluationContext;
import org.springframework.stereotype.Component;
import java.lang.reflect.Method;
import java.lang.reflect.Parameter;
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.TimeUnit;
/**
@@ -63,15 +64,16 @@ public class LockAspect {
Lock lock = method.getAnnotation(Lock.class);
// 如果构建 key
Object[] args = point.getArgs();
String lockKey = buildLockKey(lock, method, args);
String lockKey = buildLockKey(lock, point);
boolean tryLockFlag = false;
try {
long tryLockMills = lock.tryLockMills();
boolean tryLockFlag = lockBs.tryLock(tryLockMills, TimeUnit.MILLISECONDS, lockKey);
tryLockFlag = lockBs.tryLock(tryLockMills, TimeUnit.MILLISECONDS, lockKey);
if(!tryLockFlag) {
log.warn("尝试获取锁失败 {}", lockKey);
throw new LockException("尝试获取锁失败 " + lockKey);
log.warn("[LOCK] TRY LOCK FAILED {}", lockKey);
throw new LockException("[LOCK] TRY LOCK FAILED " + lockKey);
}
// 执行业务
@@ -79,10 +81,10 @@ public class LockAspect {
} catch (Throwable e) {
throw new RuntimeException(e);
} finally {
boolean unLockFlag = lockBs.unlock(lockKey);
if(!unLockFlag) {
log.warn("尝试释放锁失败 {}", lockKey);
// 这里释放异常,没有意义。
// 只有获取锁的情况下,才尝试释放锁
if(tryLockFlag) {
boolean unLockFlag = lockBs.unlock(lockKey);
// 异常处理等
}
}
} else {
@@ -97,19 +99,19 @@ public class LockAspect {
*
* https://www.cnblogs.com/best/p/5748105.html SpEL
* @param lock 注解信息
* @param args 参数
* @param joinPoint 参数
* @return 结果
*/
private String buildLockKey(Lock lock,
Method method,
Object[] args) {
ProceedingJoinPoint joinPoint) {
final Object[] args = joinPoint.getArgs();
final String lockValue = lock.value();
//创建SpEL表达式的解析器
ExpressionParser parser = new SpelExpressionParser();
//1. 如果没有入参怎么办?
if(ArrayUtil.isEmpty(args)) {
log.warn("对应的数组信息为空,直接返回 key 的值 {}", lockValue);
log.warn("[LOCK] method args is empty, return lock.value() {}", lockValue);
return lockValue;
}
@@ -123,10 +125,14 @@ public class LockAspect {
//解析表达式需要的上下文,解析时有一个默认的上下文
// jdk1.7 之前,直接使用 arg0, arg1...
EvaluationContext ctx = new StandardEvaluationContext();
List<String> paramNameList = ReflectMethodUtil.getParamNames(method);
for(int i = 0; i < paramNameList.size(); i++) {
String paramName = paramNameList.get(i);
// 利用 spring 的处理方式
Signature signature = joinPoint.getSignature();
MethodSignature methodSignature = (MethodSignature) signature;
String[] paramNameList = methodSignature.getParameterNames();
for(int i = 0; i < paramNameList.length; i++) {
String paramName = paramNameList[i];
Object paramValue = args[i];
//在上下文中设置变量变量名为user内容为user对象

View File

@@ -0,0 +1,16 @@
package com.github.houbb.lock.spring.config;
import com.github.houbb.redis.config.spring.annotation.EnableRedisConfig;
import org.springframework.context.annotation.Configuration;
/**
* bean 配置
*
* @author binbin.hou
* @since 0.0.2
*/
@Configuration
@EnableRedisConfig
public class CommonCacheConfig {
}

View File

@@ -23,7 +23,7 @@ import org.springframework.core.type.AnnotationMetadata;
*/
@Configuration
@ComponentScan(basePackages = "com.github.houbb.lock.spring")
@Import(LockBeanConfig.class)
@Import({LockBeanConfig.class, CommonCacheConfig.class})
public class LockAopConfig implements ImportAware, BeanFactoryPostProcessor {
@Bean("lockBs")

View File

@@ -1,11 +1,7 @@
package com.github.houbb.lock.spring.config;
import com.github.houbb.common.cache.api.service.ICommonCacheService;
import com.github.houbb.heaven.util.lang.StringUtil;
import com.github.houbb.id.api.Id;
import com.github.houbb.id.core.core.Ids;
import com.github.houbb.redis.config.core.factory.JedisRedisServiceFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
@@ -20,24 +16,6 @@ import org.springframework.context.annotation.Configuration;
@ComponentScan(basePackages = "com.github.houbb.lock.spring")
public class LockBeanConfig {
@Value("${redis.address:127.0.0.1}")
private String redisAddress;
@Value("${redis.port:6379}")
private int redisPort;
@Value("${redis.password:}")
private String redisPassword;
@Bean("lockCache")
public ICommonCacheService lockCache() {
if(StringUtil.isNotEmpty(redisPassword)) {
return JedisRedisServiceFactory.pooled(redisAddress, redisPort, redisPassword);
}
return JedisRedisServiceFactory.simple(redisAddress, redisPort);
}
@Bean("lockId")
public Id lockId() {
return Ids.uuid32();