基于SpringBoot AOP面向切面編程實(shí)現(xiàn)Redis分布式鎖
基于SpringBoot AOP面向切面編程實(shí)現(xiàn)Redis分布式鎖
基于SpringBoot AOP面向切面編程實(shí)現(xiàn)Redis分布式鎖
鎖定的目標(biāo)是確保相互排斥其訪問(wèn)的資源。實(shí)際上堤尾,此資源通常是字符串肝劲。使用redis實(shí)現(xiàn)鎖主要是將資源放入redis中并利用其原子性。當(dāng)其他線程訪問(wèn)時(shí)郭宝,如果Redis中已經(jīng)存在此資源辞槐,則不允許進(jìn)行某些后續(xù)操作。
Spring Boot通過(guò)RedisTemplate使用Redis粘室,在實(shí)際使用過(guò)程中榄檬,分布式鎖可以在封裝后在方法級(jí)別使用,這樣使用起來(lái)就更方便了育特,無(wú)需到處獲取和釋放鎖丙号。
首先先朦,定義一個(gè)注解:
@Target({ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@Inherited
public @interface RedisLock {
//鎖定的資源缰冤,redis的鍵
String value() default "default";
//鎖定保持時(shí)間(以毫秒為單位)
long keepMills() default 30000;
//失敗時(shí)執(zhí)行的操作
LockFailAction action() default LockFailAction.CONTINUE;
//失敗時(shí)執(zhí)行的操作--枚舉
public enum LockFailAction{
GIVEUP,
CONTINUE;
}
//重試的間隔
long sleepMills() default 200;
//重試次數(shù)
int retryTimes() default 5;
}
具有分布式鎖的Bean
@Configuration
@AutoConfigureAfter(RedisAutoConfiguration.class)
public class DistributedLockAutoConfiguration {
@Bean
@ConditionalOnBean(RedisTemplate.class)
public DistributedLock redisDistributedLock(RedisTemplate redisTemplate){
return new RedisDistributedLock(redisTemplate);
}
}
面向切面編程-定義切面
@Aspect
@Configuration
@ConditionalOnClass(DistributedLock.class)
@AutoConfigureAfter(DistributedLockAutoConfiguration.class)
public class DistributedLockAspectConfiguration {
private final Logger logger = LoggerFactory.getLogger(DistributedLockAspectConfiguration.class);
@Autowired
private DistributedLock distributedLock;
@Pointcut("@annotation(com.itopener.lock.redis.spring.boot.autoconfigure.annotations.RedisLock)")
private void lockPoint(){
}
@Around("lockPoint()")
public Object around(ProceedingJoinPoint pjp) throws Throwable{
Method method = ((MethodSignature) pjp.getSignature()).getMethod();
RedisLock redisLock = method.getAnnotation(RedisLock.class);
String key = redisLock.value();
if(StringUtils.isEmpty(key)){
Object\[\] args = pjp.getArgs();
key = Arrays.toString(args);
}
int retryTimes = redisLock.action().equals(LockFailAction.CONTINUE) ? redisLock.retryTimes() : 0;
//獲取分布式鎖
boolean lock = distributedLock.lock(key, redisLock.keepMills(), retryTimes, redisLock.sleepMills());
if(!lock) {
logger.debug("get lock failed : " + key);
return null;
}
//執(zhí)行方法之后,釋放分布式鎖
logger.debug("get lock success : " + key);
try {
return pjp.proceed(); //執(zhí)行方法
} catch (Exception e) {
logger.error("execute locked method occured an exception", e);
} finally {
boolean releaseResult = distributedLock.releaseLock(key); //釋放分布式鎖
logger.debug("release lock :" + key + (releaseResult ?" success" : "failed"));
}
return null;
}
}
使用方法
- 進(jìn)入該方法時(shí)喳魏,占用分布式鎖棉浸,
- 方法執(zhí)行完成時(shí),釋放分布式鎖
- 使用同一個(gè)資源刺彩,如your-custom-service-redis-key的多個(gè)函數(shù)迷郑,搶占同一個(gè)鎖。誰(shuí)搶到誰(shuí)先執(zhí)行创倔。
@RedisLock(value="your-custom-service-redis-key")
public void serviceMethod(){
//正常寫方法實(shí)現(xiàn)
}
歡迎關(guān)注我的博客嗡害,里面有很多精品合集
- 本文轉(zhuǎn)載注明出處(必須帶連接,不能只轉(zhuǎn)文字):字母哥博客畦攘。
覺(jué)得對(duì)您有幫助的話霸妹,幫我點(diǎn)贊、分享知押!您的支持是我不竭的創(chuàng)作動(dòng)力叹螟! 鹃骂。另外,筆者最近一段時(shí)間輸出了如下的精品內(nèi)容罢绽,期待您的關(guān)注畏线。