[Feature] add for new
This commit is contained in:
30
lock-spring/pom.xml
Normal file
30
lock-spring/pom.xml
Normal file
@@ -0,0 +1,30 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<parent>
|
||||
<artifactId>lock</artifactId>
|
||||
<groupId>com.github.houbb</groupId>
|
||||
<version>1.0.0</version>
|
||||
</parent>
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<artifactId>lock-spring</artifactId>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>com.github.houbb</groupId>
|
||||
<artifactId>lock-core</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>com.github.houbb</groupId>
|
||||
<artifactId>aop-core</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.github.houbb</groupId>
|
||||
<artifactId>aop-spring</artifactId>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
</project>
|
||||
@@ -0,0 +1,43 @@
|
||||
package com.github.houbb.lock.spring.annotation;
|
||||
|
||||
import com.github.houbb.lock.spring.config.LockAopConfig;
|
||||
import org.springframework.context.annotation.EnableAspectJAutoProxy;
|
||||
import org.springframework.context.annotation.Import;
|
||||
|
||||
import java.lang.annotation.*;
|
||||
|
||||
/**
|
||||
* 启用注解
|
||||
* @author binbin.hou
|
||||
* @since 0.0.2
|
||||
*/
|
||||
@Target(ElementType.TYPE)
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
@Documented
|
||||
@Import(LockAopConfig.class)
|
||||
@EnableAspectJAutoProxy
|
||||
public @interface EnableLock {
|
||||
|
||||
/**
|
||||
* 加锁过期时间
|
||||
* @return 时间
|
||||
* @since 1.1.0
|
||||
*/
|
||||
int lockExpireMills() default 60 * 1000;
|
||||
|
||||
/**
|
||||
* 唯一标识生成策略
|
||||
* @return 结果
|
||||
* @since 1.1.0
|
||||
*/
|
||||
String id() default "lockId";
|
||||
|
||||
/**
|
||||
* 缓存实现策略 bean 名称
|
||||
* @return 实现
|
||||
* @since 1.1.0
|
||||
*/
|
||||
String cache() default "lockCache";
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
package com.github.houbb.lock.spring.annotation;
|
||||
|
||||
import java.lang.annotation.*;
|
||||
|
||||
/**
|
||||
* 分布式加锁注解
|
||||
* @author binbin.hou
|
||||
* @since 0.0.2
|
||||
*/
|
||||
@Target(ElementType.METHOD)
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
@Documented
|
||||
@Inherited
|
||||
public @interface Lock {
|
||||
|
||||
/**
|
||||
* 缓存的 key 策略,支持 SpEL
|
||||
* @return 结果
|
||||
*/
|
||||
String value() default "";
|
||||
|
||||
/**
|
||||
* 尝试获取锁等待时间
|
||||
* @return 结果
|
||||
*/
|
||||
long tryLockMills() default 10 * 1000;
|
||||
|
||||
}
|
||||
@@ -0,0 +1,141 @@
|
||||
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;
|
||||
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.annotation.Around;
|
||||
import org.aspectj.lang.annotation.Aspect;
|
||||
import org.aspectj.lang.annotation.Pointcut;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.beans.factory.annotation.Qualifier;
|
||||
import org.springframework.expression.EvaluationContext;
|
||||
import org.springframework.expression.ExpressionParser;
|
||||
import org.springframework.expression.spel.standard.SpelExpressionParser;
|
||||
import org.springframework.expression.spel.support.StandardEvaluationContext;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.lang.reflect.Method;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
/**
|
||||
* @author binbin.hou
|
||||
* @since 1.1.0
|
||||
*/
|
||||
@Aspect
|
||||
@Component
|
||||
public class LockAspect {
|
||||
|
||||
private final Log log = LogFactory.getLog(LockAspect.class);
|
||||
|
||||
@Autowired
|
||||
@Qualifier("lockBs")
|
||||
private LockBs lockBs;
|
||||
|
||||
@Pointcut("@annotation(com.github.houbb.lock.spring.annotation.Lock)")
|
||||
public void lockPointcut() {
|
||||
}
|
||||
|
||||
/**
|
||||
* 执行核心方法
|
||||
*
|
||||
* 相当于 MethodInterceptor
|
||||
* @param point 切点
|
||||
* @return 结果
|
||||
* @throws Throwable 异常信息
|
||||
* @since 0.0.2
|
||||
*/
|
||||
@Around("lockPointcut()")
|
||||
public Object around(ProceedingJoinPoint point) throws Throwable {
|
||||
Method method = SpringAopUtil.getCurrentMethod(point);
|
||||
|
||||
boolean isLockAnnotation = method.isAnnotationPresent(Lock.class);
|
||||
if(isLockAnnotation) {
|
||||
Lock lock = method.getAnnotation(Lock.class);
|
||||
|
||||
// 如果构建 key?
|
||||
Object[] args = point.getArgs();
|
||||
String lockKey = buildLockKey(lock, method, args);
|
||||
try {
|
||||
long tryLockMills = lock.tryLockMills();
|
||||
|
||||
boolean tryLockFlag = lockBs.tryLock(tryLockMills, TimeUnit.MILLISECONDS, lockKey);
|
||||
if(!tryLockFlag) {
|
||||
log.warn("尝试获取锁失败 {}", lockKey);
|
||||
throw new LockException("尝试获取锁失败 " + lockKey);
|
||||
}
|
||||
|
||||
// 执行业务
|
||||
return point.proceed();
|
||||
} catch (Throwable e) {
|
||||
throw new RuntimeException(e);
|
||||
} finally {
|
||||
boolean unLockFlag = lockBs.unlock(lockKey);
|
||||
if(!unLockFlag) {
|
||||
log.warn("尝试释放锁失败 {}", lockKey);
|
||||
// 这里释放异常,没有意义。
|
||||
}
|
||||
}
|
||||
} else {
|
||||
return point.proceed();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 构建加锁的 key
|
||||
*
|
||||
* https://www.cnblogs.com/fashflying/p/6908028.html spring cache
|
||||
*
|
||||
* https://www.cnblogs.com/best/p/5748105.html SpEL
|
||||
* @param lock 注解信息
|
||||
* @param args 参数
|
||||
* @return 结果
|
||||
*/
|
||||
private String buildLockKey(Lock lock,
|
||||
Method method,
|
||||
Object[] args) {
|
||||
final String lockValue = lock.value();
|
||||
|
||||
//创建SpEL表达式的解析器
|
||||
ExpressionParser parser = new SpelExpressionParser();
|
||||
//1. 如果没有入参怎么办?
|
||||
if(ArrayUtil.isEmpty(args)) {
|
||||
log.warn("对应的数组信息为空,直接返回 key 的值 {}", lockValue);
|
||||
return lockValue;
|
||||
}
|
||||
|
||||
// 如果 lockValue 的值为空呢?
|
||||
if(StringUtil.isEmpty(lockValue)) {
|
||||
return Arrays.toString(args);
|
||||
}
|
||||
|
||||
// 如何获取参数名称呢?
|
||||
|
||||
//解析表达式需要的上下文,解析时有一个默认的上下文
|
||||
// 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);
|
||||
Object paramValue = args[i];
|
||||
|
||||
//在上下文中设置变量,变量名为user,内容为user对象
|
||||
ctx.setVariable(paramName, paramValue);
|
||||
}
|
||||
|
||||
// 直接 toString,比较简单。
|
||||
Object value = parser.parseExpression(lockValue).getValue(ctx);
|
||||
return ObjectUtil.objectToString(value, "null");
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,69 @@
|
||||
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.lock.core.bs.LockBs;
|
||||
import com.github.houbb.lock.spring.annotation.EnableLock;
|
||||
import com.github.houbb.redis.config.core.factory.JedisRedisServiceFactory;
|
||||
import org.springframework.beans.BeansException;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.beans.factory.config.BeanFactoryPostProcessor;
|
||||
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
|
||||
import org.springframework.context.annotation.*;
|
||||
import org.springframework.core.annotation.AnnotationAttributes;
|
||||
import org.springframework.core.type.AnnotationMetadata;
|
||||
|
||||
/**
|
||||
* aop 配置
|
||||
*
|
||||
* @author binbin.hou
|
||||
* @since 0.0.2
|
||||
*/
|
||||
@Configuration
|
||||
@ComponentScan(basePackages = "com.github.houbb.lock.spring")
|
||||
@Import(LockBeanConfig.class)
|
||||
public class LockAopConfig implements ImportAware, BeanFactoryPostProcessor {
|
||||
|
||||
@Bean("lockBs")
|
||||
public LockBs lockBs() {
|
||||
int lockExpireMills = (int) enableLockAttributes.get("lockExpireMills");
|
||||
ICommonCacheService commonCacheService = beanFactory.getBean(enableLockAttributes.getString("cache"), ICommonCacheService.class);
|
||||
Id id = beanFactory.getBean(enableLockAttributes.getString("id"), Id.class);
|
||||
|
||||
return LockBs.newInstance()
|
||||
.cache(commonCacheService)
|
||||
.id(id)
|
||||
.lockExpireMills(lockExpireMills)
|
||||
.init();
|
||||
}
|
||||
|
||||
/**
|
||||
* 属性信息
|
||||
*/
|
||||
private AnnotationAttributes enableLockAttributes;
|
||||
|
||||
/**
|
||||
* bean 工厂
|
||||
*
|
||||
* @since 0.0.5
|
||||
*/
|
||||
private ConfigurableListableBeanFactory beanFactory;
|
||||
|
||||
@Override
|
||||
public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {
|
||||
this.beanFactory = beanFactory;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setImportMetadata(AnnotationMetadata annotationMetadata) {
|
||||
enableLockAttributes = AnnotationAttributes.fromMap(
|
||||
annotationMetadata.getAnnotationAttributes(EnableLock.class.getName(), false));
|
||||
if (enableLockAttributes == null) {
|
||||
throw new IllegalArgumentException(
|
||||
"@EnableLock is not present on importing class " + annotationMetadata.getClassName());
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,46 @@
|
||||
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;
|
||||
|
||||
/**
|
||||
* bean 配置
|
||||
*
|
||||
* @author binbin.hou
|
||||
* @since 0.0.2
|
||||
*/
|
||||
@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();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1 @@
|
||||
package com.github.houbb.lock.spring;
|
||||
Reference in New Issue
Block a user