|
@@ -0,0 +1,69 @@
|
|
|
|
+package com.flow;
|
|
|
|
+
|
|
|
|
+import com.flow.common.core.exception.BaseException;
|
|
|
|
+import com.flow.common.redis.annotation.RedissonLock;
|
|
|
|
+import org.aspectj.lang.ProceedingJoinPoint;
|
|
|
|
+import org.aspectj.lang.annotation.Around;
|
|
|
|
+import org.aspectj.lang.annotation.Aspect;
|
|
|
|
+import org.aspectj.lang.reflect.MethodSignature;
|
|
|
|
+import org.redisson.api.RLock;
|
|
|
|
+import org.redisson.api.RedissonClient;
|
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
|
+import org.springframework.core.DefaultParameterNameDiscoverer;
|
|
|
|
+import org.springframework.core.annotation.Order;
|
|
|
|
+import org.springframework.expression.EvaluationContext;
|
|
|
|
+import org.springframework.expression.Expression;
|
|
|
|
+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 org.springframework.util.StringUtils;
|
|
|
|
+
|
|
|
|
+import java.lang.reflect.Method;
|
|
|
|
+import java.util.Optional;
|
|
|
|
+
|
|
|
|
+/**
|
|
|
|
+ * 分布式锁切面
|
|
|
|
+ */
|
|
|
|
+@Aspect
|
|
|
|
+@Component
|
|
|
|
+@Order(0) // 确保比事务注解先执行,分布式锁在事务外
|
|
|
|
+public class RedissonLockAspect {
|
|
|
|
+ private static final ExpressionParser parser = new SpelExpressionParser();
|
|
|
|
+ private static final DefaultParameterNameDiscoverer parameterNameDiscoverer = new DefaultParameterNameDiscoverer();
|
|
|
|
+ @Autowired
|
|
|
|
+ private RedissonClient redissonClient;
|
|
|
|
+
|
|
|
|
+ @Around("@annotation(com.flow.common.redis.annotation.RedissonLock)")
|
|
|
|
+ public Object around(ProceedingJoinPoint joinPoint) throws Throwable {
|
|
|
|
+ Method method = ((MethodSignature) joinPoint.getSignature()).getMethod();
|
|
|
|
+ RedissonLock redissonLock = method.getAnnotation(RedissonLock.class);
|
|
|
|
+ // 默认方法限定名+注解排名(可能多个)
|
|
|
|
+ String prefix = StringUtils.isEmpty(redissonLock.prefixKey()) ? String.format("%s#%s", method.getDeclaringClass(), method.getName()) : redissonLock.prefixKey();
|
|
|
|
+ // 创建SpringEL表达式上下文
|
|
|
|
+ EvaluationContext context = new StandardEvaluationContext();
|
|
|
|
+ String[] params = Optional.ofNullable(parameterNameDiscoverer.getParameterNames(method)).orElse(new String[]{});//解析参数名
|
|
|
|
+ Object[] args = joinPoint.getArgs();
|
|
|
|
+ for (int i = 0; i < params.length; i++) {
|
|
|
|
+ context.setVariable(params[i], args[i]);
|
|
|
|
+ }
|
|
|
|
+ // 解析SpringEL表达式
|
|
|
|
+ Expression expression = parser.parseExpression(redissonLock.key());
|
|
|
|
+ String key = expression.getValue(context, String.class);
|
|
|
|
+ // 获取锁
|
|
|
|
+ String lockName = String.format("%s:%s", prefix, key);
|
|
|
|
+ RLock lock = redissonClient.getLock(lockName);
|
|
|
|
+ boolean lockSuccess = lock.tryLock(redissonLock.waitTime(), redissonLock.unit());
|
|
|
|
+ if (!lockSuccess) {
|
|
|
|
+ throw new BaseException("请求太频繁了,请稍后再试");
|
|
|
|
+ }
|
|
|
|
+ try {
|
|
|
|
+ // 执行锁内方法
|
|
|
|
+ return joinPoint.proceed();
|
|
|
|
+ } finally {
|
|
|
|
+ if (lock.isLocked() && lock.isHeldByCurrentThread()) {
|
|
|
|
+ lock.unlock();
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+}
|