日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > java >内容正文

java

redission java_Java注解如何基于Redission实现分布式锁

發布時間:2025/3/11 java 22 豆豆
生活随笔 收集整理的這篇文章主要介紹了 redission java_Java注解如何基于Redission实现分布式锁 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

這篇文章主要介紹了Java注解如何基于Redission實現分布式鎖,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下

1、定義注解類

@Target({ ElementType.METHOD })

@Retention(RetentionPolicy.RUNTIME)

@Documented

public @interface DistributedLock {

//鎖名稱

String lockName() default "";

//釋放時間

long releaseTime() default 5*1000;

//時間單位

TimeUnit timeUnit() default TimeUnit.MILLISECONDS;

}

2、定義切面攔截 DistributedLock 注解

@Aspect

@Component

@Slf4j

public class DistributedLockAspect {

@Autowired

private RedissonClient redissonClient;

//這里需要修改對應的包名

@Pointcut("@annotation(com.utils.annotation.DistributedLock)")

public void RlockAspect() {

}

@Around("RlockAspect()")

public Object arround(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {

Object object = null;

RLock lock = null;

log.info("rlockAspect start ");

try {

DistributedLock rlockInfo = getRlockInfo(proceedingJoinPoint);

String lockKey = getLocalKey(proceedingJoinPoint, rlockInfo);

lock = redissonClient.getLock(lockKey);

if (lock != null) {

final boolean status = lock.tryLock(rlockInfo.releaseTime(), rlockInfo.timeUnit());

if (status) {

object = proceedingJoinPoint.proceed();

}

} else {

log.info("未獲取到鎖:{}", lockKey);

}

} finally {

// 當前線程獲取到鎖再釋放鎖

if (lock != null && lock.isHeldByCurrentThread()) {

lock.unlock();

}

}

return object;

}

public DistributedLock getRlockInfo(ProceedingJoinPoint proceedingJoinPoint) {

MethodSignature methodSignature = (MethodSignature) proceedingJoinPoint.getSignature();

return methodSignature.getMethod().getAnnotation(DistributedLock.class);

}

/**

* 獲取redis lock key

*

* @param proceedingJoinPoint

* @return

*/

public String getLocalKey(ProceedingJoinPoint proceedingJoinPoint, DistributedLock rlockInfo) {

StringBuilder localKey = new StringBuilder("Rlock");

final Object[] args = proceedingJoinPoint.getArgs();

String businessNo = "";

// 如果沒有設置鎖值

if (StringUtils.isNotEmpty(rlockInfo.lockName())) {

businessNo = rlockInfo.lockName();

} else {

MethodSignature methodSignature = (MethodSignature) proceedingJoinPoint.getSignature();

Class[] parameters = methodSignature.getParameterTypes();

String methodName = methodSignature.getMethod().getName();

if (parameters != null) {

for (int i = 0; i < parameters.length; i++) {

Class parameter = parameters[i];

if (parameter.getSimpleName().equals("NDevice")) {

NDevice de = (NDevice) args[i];

businessNo = de.getUuid() + methodName;

}

if (parameter.getSimpleName().equals("FrameBean")) {

FrameBean de = (FrameBean) args[i];

businessNo = de.getColumn1() + methodName;

}

}

// 如果沒有獲取到業務編號,則使用方法簽名

if (StringUtils.isEmpty(businessNo)) {

businessNo = methodName;

}

}

}

return businessNo;

}

}

3、使用方法:在需要用分布式鎖的方法上面加 @DistributedLock 注解即可

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。

總結

以上是生活随笔為你收集整理的redission java_Java注解如何基于Redission实现分布式锁的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。