Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Spring boot redis distributed lock

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

Spring boot redis distributed lock

When using annotations to implement distributed locks with reference to spring boot redis, it is found that they can not meet the usage requirements.

So I began to fumble to solve the problem.

As follows, value is the key of lock, because the business requirement key is "cancelOrder_123_321" 123 is the order ID,321 is the user ID

@ RedisLock (value = "cancelOrder_# {# order.orderNo} _ # {# memberId}") @ Transactional (rollbackFor = {RuntimeException.class, Exception.class}) public void cancelOrder (Order order, String memberId) {log.info ("user: {}, order number: {} start the cancellation process.", order.getOrderNo (), memberId); String orderNo = order.getOrderNo (); Order updateOrder = new Order (); updateOrder.setOrderNo (orderNo) UpdateOrder.setOrderState (OrderStateEnum.CANCELED.getKey ())

To make a long story short, friends who need to know more will see the blogs of other great gods.

Why can I cancelOrder_# {# order.orderNo} _ # {# memberId} like this?

What's the most important thing here?

Because I parsed the el expression string into a compound. The compound simple point is the List collection, in which there are el expressions and text, which are executed sequentially in for.

The code is as follows:

Aspect class

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 () 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 class

/ * @ author liuhanling * @ create 2019-01-14 17:04 * @ desc expression rootObject * / public class ExpressionRootObject {/ * method * / private final Method method; / * parameter array * / private final Object [] args; / * Target object * / private final Object target / * Target class * / 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] reference link (spring boot redis distributed lock) https://my.oschina.net/dengfuwei/blog/1600681

[2] reference link (el expression) https://blog.csdn.net/zhoudaxia/article/details/38174169

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report