Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

Spring boot redis distributed lock

Shulou Source: shulou.com Published: 2022-06-02 19:14:35 09月23日 Update

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

Tags: Expression distributed compound goal method user order link reference important long story short up and down context business code blog parameter variable god character Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Information Xiaomi Linux NVidia Shulou Technology