分布式锁

 

import lombok.extern.slf4j.Slf4j;
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.aspectj.lang.reflect.MethodSignature;
import org.redisson.api.RLock;
import org.redisson.api.RedissonClient;
import org.springframework.aop.support.AopUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.annotation.AnnotationUtils;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;
import java.lang.reflect.Method;
import java.util.concurrent.TimeUnit;

@Aspect
@Component
@Slf4j
public class RedisLockAspect {
@Autowired
private RedissonClient redissonClient;
@Pointcut("@annotation(com.aexpec.cmd.core.annotation.RedisLock)")
public void redisLockAspectAspect() {
// 切面位置以及方法定义
}

@Around("redisLockAspectAspect()")
public Object around(ProceedingJoinPoint joinPoint) throws Throwable {
MethodSignature signature = (MethodSignature) joinPoint.getSignature();
Method method = signature.getMethod();
Method targetMethod = AopUtils.getMostSpecificMethod(method, joinPoint.getTarget().getClass());
Object[] arguments = joinPoint.getArgs();
RedisLock redisLock = AnnotationUtils.findAnnotation(targetMethod, RedisLock.class);
if (redisLock == null) {
return new ApiResponse<>().error(BaseCode.REDIS_FRAME_ERROR);
}
RLock lock = null;
try {
lock = redissonClient.getLock(redisLock.lockKey());
// 尝试加锁,自动续锁功能开启,上锁以后10秒自动解锁
if (lock.tryLock(10, TimeUnit.SECONDS)) {
return joinPoint.proceed();
}
} catch (InterruptedException interruptedExcep) {
BizException bizException = new BizException(BizCode.REDIS_LOCK_FAILED);
LoggerUtil.error(log, "redis加锁失败{}", redisLock.lockKey(), bizException);
Thread.currentThread().interrupt();
} catch (Throwable e) {
throw e;
} finally {
//解锁
if (null != lock) {
lock.unlock();
}
}
return null;
}
}

@Service
public class ModelServiceImpl implements ModelService {
  public static final String LOCK_KEY_MODEL_ADD = "lock_key_model_add";
  @RedisLock(lockKey = LOCK_KEY_MODEL_ADD)
  public String add(ModelDto modelDto) {
    // ...
  }
}


import java.lang.annotation.*;
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
public @interface RedisLock {
String lockKey();
/**
* 默认毫秒
* @return
*/
int expire() default 15000;

/**
* 默认给前端的提示
* @return
*/
String errorMsg() default "请稍后重试";
}

posted @ 2022-06-04 18:21  剑阁丶神灯  阅读(28)  评论(0编辑  收藏  举报