package com.cpic.zjbz.common;
import java.lang.annotation.*;
import java.util.concurrent.TimeUnit;
/**
* 锁的注解
*/
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
public @interface DistributedLock {
/**
* redis 锁key的前缀
*
* @return redis 锁key的前缀
*/
String prefix() default "distributedLock";
/**
* 过期秒数,默认为10秒
*
*/
int expire() default 10;
/**
* 重试时间,默认为10秒
*
*/
int retry() default 10;
/**
* 超时时间单位
*
* @return 秒
*/
TimeUnit timeUnit() default TimeUnit.SECONDS;
/**
* Key的分隔符(默认 :)
* @return String
*/
String delimiter() default ":";
/**
* 当不使用@LockKey注解时或者注解失效,是否锁整个方法
* @return
*/
boolean isLockMethod() default false;
}
package com.cpic.zjbz.common;
import java.lang.annotation.*;
/**
* 要锁住的属性字段
* 注意:只支持方法形参(基本类型、String或者自定义实体的第一层属性)加注解,用法可参考第6示例
* 例如:test(@LockKey int stuNo);
* test(Student input);
* Student:{
* @LockKey int stuNo;
* }
*
*/
@Target({ElementType.PARAMETER, ElementType.METHOD, ElementType.FIELD})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
public @interface LockKey {
}
package com.cpic.zjbz.common;
import com.cpic.zjbz.exception.BusinessException;
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.reflect.MethodSignature;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.util.StringUtils;
import java.lang.reflect.Method;
@Aspect
@Configuration
@Slf4j
public class LockMethodAspect {
@Autowired
public LockMethodAspect(StringRedisTemplate lockRedisTemplate) {
this.lockRedisTemplate = lockRedisTemplate;
}
private final StringRedisTemplate lockRedisTemplate;
@Around("execution(public * *(..)) && @annotation(com.cpic.zjbz.common.DistributedLock)")
public Object interceptor(ProceedingJoinPoint pjp) throws Throwable{
MethodSignature signature = (MethodSignature) pjp.getSignature();
Method method = signature.getMethod();
DistributedLock lock = method.getAnnotation(DistributedLock.class);
if (StringUtils.isEmpty(lock.prefix())) {
throw new RuntimeException("lock key prefix can't be null...");
}
final String lockKey = RedisLockUtils.getLockKey(pjp);
boolean flag = false;
try {
//key不存在才能设置成功
flag = RedisLockUtils.lock(lockRedisTemplate, lockKey, lock.expire(), lock.retry(), lock.timeUnit());
} catch (Exception e) {
log.error("redis 异常", e);
throw new BusinessException(e.getMessage());
}
if (flag) {
try {
return pjp.proceed();
} catch (Exception e) {
log.error("分布式锁的执行异常", e);
throw new BusinessException(e.getMessage());
}finally {
RedisLockUtils.unLock(lockRedisTemplate,lockKey);
}
} else {
//按理来说 我们应该抛出一个自定义的 CacheLockException 异常;
throw new BusinessException("请稍后重试");
}
}
}
package com.cpic.zjbz.common;
import com.cpic.zjbz.exception.BusinessException;
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.reflect.MethodSignature;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.util.StringUtils;
import java.lang.reflect.Method;
@Aspect
@Configuration
@Slf4j
public class LockMethodAspect {
@Autowired
public LockMethodAspect(StringRedisTemplate lockRedisTemplate) {
this.lockRedisTemplate = lockRedisTemplate;
}
private final StringRedisTemplate lockRedisTemplate;
@Around("execution(public * *(..)) && @annotation(com.cpic.zjbz.common.DistributedLock)")
public Object interceptor(ProceedingJoinPoint pjp) throws Throwable{
MethodSignature signature = (MethodSignature) pjp.getSignature();
Method method = signature.getMethod();
DistributedLock lock = method.getAnnotation(DistributedLock.class);
if (StringUtils.isEmpty(lock.prefix())) {
throw new RuntimeException("lock key prefix can't be null...");
}
final String lockKey = RedisLockUtils.getLockKey(pjp);
boolean flag = false;
try {
//key不存在才能设置成功
flag = RedisLockUtils.lock(lockRedisTemplate, lockKey, lock.expire(), lock.retry(), lock.timeUnit());
} catch (Exception e) {
log.error("redis 异常", e);
throw new BusinessException(e.getMessage());
}
if (flag) {
try {
return pjp.proceed();
} catch (Exception e) {
log.error("分布式锁的执行异常", e);
throw new BusinessException(e.getMessage());
}finally {
RedisLockUtils.unLock(lockRedisTemplate,lockKey);
}
} else {
//按理来说 我们应该抛出一个自定义的 CacheLockException 异常;
throw new BusinessException("请稍后重试");
}
}
}
sping boot redis 锁
最新推荐文章于 2024-07-18 16:30:39 发布