spring boot redis分布式鎖
站在用戶的角度思考問題,與客戶深入溝通,找到巴宜網(wǎng)站設(shè)計與巴宜網(wǎng)站推廣的解決方案,憑借多年的經(jīng)驗,讓設(shè)計與互聯(lián)網(wǎng)技術(shù)結(jié)合,創(chuàng)造個性化、用戶體驗好的作品,建站類型包括:成都網(wǎng)站制作、網(wǎng)站設(shè)計、企業(yè)官網(wǎng)、英文網(wǎng)站、手機端網(wǎng)站、網(wǎng)站推廣、主機域名、網(wǎng)頁空間、企業(yè)郵箱。業(yè)務(wù)覆蓋巴宜地區(qū)。
參照spring boot redis分布式鎖 用注解實現(xiàn)時發(fā)現(xiàn)不能滿足使用需求
于是自己開始摸索解決問題...
如下,value 是lock的key,因為業(yè)務(wù)的需要key是 "cancelOrder_123_321" 123是訂單ID,321是用戶ID
@RedisLock(value = "cancelOrder_#{#order.orderNo}_#{#memberId}") @Transactional(rollbackFor = {RuntimeException.class, Exception.class}) public void cancelOrder(Order order, String memberId) { log.info("用戶:{},訂單號:{} 開始執(zhí)行取消流程...",order.getOrderNo(),memberId); String orderNo = order.getOrderNo(); Order updateOrder = new Order(); updateOrder.setOrderNo(orderNo); updateOrder.setOrderState(OrderStateEnum.CANCELED.getKey());
長話短說,需要了解詳情的朋友看其他大神的博客
為什么我可以這樣 cancelOrder_#{#order.orderNo}_#{#memberId} 寫?
這里最重要的是什么?
是因為我將這個el表達式字符串,解析成了復(fù)合型了.復(fù)合型簡單點就是List集合,集合里有el表達式和文本,用for順序執(zhí)行.
代碼如下:
Aspect類
private static final OperationExpressionEvaluator evaluator = new OperationExpressionEvaluator(); @Around("lockPoint()") public Object around(ProceedingJoinPoint pjp) throws Throwable{ Method method = ((MethodSignature)pjp.getSignature()).getMethod(); RedisLock redisLock = method.getAnnotation(RedisLock.class); String key = getKey(redisLock.value(),method,pjp.getArgs(),pjp.getTarget()); int retryTimes = redisLock.action().equals(RedisLock.LockFailAction.CONTINUE) ? redisLock.retryTimes() : 0; boolean lock = redisLockImpl.lock(key,redisLock.keepMills(),retryTimes,redisLock.sleepMills()); if(!lock){ log.debug("get lock failed:{}",key); return null; } log.debug("get lock success:{}",key); try{ return pjp.proceed(); }catch (Exception e){ log.error("execute locked method occured an exception",e); } finally { boolean releaseResult = redisLockImpl.releaseLock(key); log.debug("release lock:{}-{}",key,releaseResult ? "success":"failed"); } return null; } private String getKey(String key,Method method,Object [] args,Object target){ if(key.length() <= 0){ return Arrays.toString(args); } return String.valueOf(generateKey(key,method,args,target)); } private Object generateKey(String key,Method method,Object [] args,Object target) { EvaluationContext evaluationContext = evaluator.createEvaluationContext(method, args, target, target.getClass(), null); return evaluator.key(key, evaluationContext); }
OperationExpressionEvaluator 類
/** * @author liuhanling * @create 2019-01-14 17:03 * @desc 操作表達式評估器 */ public class OperationExpressionEvaluator extends CachedExpressionEvaluator{ private final ParserContext parserContext = new ParserContext() { @Override public boolean isTemplate() { return true; } @Override public String getExpressionPrefix() { return "#{"; } @Override public String getExpressionSuffix() { return "}"; } }; /** * Create an {@link EvaluationContext}. * @param method the method * @param args the method arguments * @param target the target object * @param targetClass the target class * @return the evaluation context */ public EvaluationContext createEvaluationContext(Method method, Object[] args, Object target, Class<?> targetClass, BeanFactory beanFactory) { //bean context ExpressionRootObject rootObject = new ExpressionRootObject(method, args, target, targetClass); //獲取目標方法 Method targetMethod = getTargetMethod(targetClass, method); //評測上下文,主要是處理無用變量 MethodBasedEvaluationContext evaluationContext = new MethodBasedEvaluationContext(rootObject,targetMethod,args,getParameterNameDiscoverer()); //CacheEvaluationContext evaluationContext = new CacheEvaluationContext(rootObject, targetMethod, args, getParameterNameDiscoverer()); if (beanFactory != null) { evaluationContext.setBeanResolver(new BeanFactoryResolver(beanFactory)); } return evaluationContext; } public Object key(String keyExpression, EvaluationContext evalContext) { //執(zhí)行el表達式 復(fù)合型 return getParser().parseExpression(keyExpression,parserContext).getValue(evalContext); } private Method getTargetMethod(Class<?> targetClass, Method method) { //AnnotatedElementKey methodKey = new AnnotatedElementKey(method, targetClass); //Method targetMethod = this.targetMethodCache.get(methodKey); //if (targetMethod == null) { Method targetMethod = AopUtils.getMostSpecificMethod(method, targetClass); if (targetMethod == null) { targetMethod = method; } //this.targetMethodCache.put(methodKey, targetMethod); //} return targetMethod; } }
ExpressionRootObject 類
/** * @author liuhanling * @create 2019-01-14 17:04 * @desc 表達式 rootObject */ public class ExpressionRootObject { /** * 方法 */ private final Method method; /** * 參數(shù)數(shù)組 */ private final Object[] args; /** * 目標對象 */ private final Object target; /** * 目標類 */ private final Class<?> targetClass; public ExpressionRootObject(Method method, Object[] args, Object target, Class<?> targetClass) { Assert.notNull(method, "Method is required"); Assert.notNull(targetClass, "targetClass is required"); this.method = method; this.target = target; this.targetClass = targetClass; this.args = args; } public Method getMethod() { return this.method; } public String getMethodName() { return this.method.getName(); } public Object[] getArgs() { return this.args; } public Object getTarget() { return this.target; } public Class<?> getTargetClass() { return this.targetClass; } }
[1]參考鏈接(spring boot redis分布式鎖) https://my.oschina.net/dengfuwei/blog/1600681
[2]參考鏈接(el表達式) https://blog.csdn.net/zhoudaxia/article/details/38174169
本文名稱:springbootredis分布式鎖
鏈接URL:http://m.newbst.com/article2/jhshoc.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供移動網(wǎng)站建設(shè)、響應(yīng)式網(wǎng)站、電子商務(wù)、網(wǎng)站策劃、定制網(wǎng)站、營銷型網(wǎng)站建設(shè)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)