利用redis的setIfAbsent()方法实现分布式锁

2023-05-16

再集群环境中,存在定时任务多次执行,浪费资源,那么如何避免这种情况呢,下面就说明一下如何利用一个注解解决问题,利用切面配合redis可以简单实现分布式锁,解决定时任务重复执行的问题。直接上干货了,感觉不对的朋友勿喷,请划过。

实现逻辑和基本原理
逻辑:
1、每一次访问进来都先去获得redis 锁 如果获得到 则继续执行,如果获取不到 则直接返回
2、redis 的key 设有过期时间 避免某个请求处理不当(或方法执行到一半宕机或网络原因)导致 redis key 不能正确释放 死锁
3 在 finally 方法里进行手工释放锁
基本原理(即有什么样的理论基础 才可以用redis做分布式锁):
1、setIfAbsent 即 setnx 当key不存在时设置成功,当key 存在时会设置失败
2、redis设置key 和 设置过期时间 必须为原子性,即同时设置。否则在设置完key 后系统宕机 此时还没来得及设置过期时间 那么这个可以就成了永久的key了 就会产生死锁的情况

首先自定义一个注解

@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.METHOD})
@Documented
public @interface RedisLock {
    String lockPrefix() default "";

    String lockKey() default "";

    //是否使用自定义过期时间,false->配置文件获取;true->自己指定过期时间
    boolean expireConfig() default true;

    long timeOut() default 30;

    TimeUnit timeUnit() default TimeUnit.SECONDS;
}

然后定义一个切面,

package com.mes.material.annotation.redisLock;

import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;


@Aspect
@Component
@Slf4j
public class RedisLockAspect {
    private static final Integer Max_RETRY_COUNT = 3;
    private static final String LOCK_PRE_FIX = "lockPreFix";
    private static final String LOCK_KEY = "lockKey";
    private static final String TIME_OUT = "timeOut";
    private static final String EXPIRE_CONFIG = "expireConfig";

    @Value("${schedule.expire}")
    private long timeOut;

    @Autowired
    private RedisTemplate redisTemplate;

    @Pointcut("@annotation(com.mes.material.annotation.redisLock.RedisLock)")
    public void redisLockAspect() {
    }

    @Around("redisLockAspect()")
    public void lockAroundAction(ProceedingJoinPoint proceeding) throws Exception {

        //获取注解中的参数
        Map<String, Object> annotationArgs = this.getAnnotationArgs(proceeding);
        String lockPrefix = (String) annotationArgs.get(LOCK_PRE_FIX);
        String key = (String) annotationArgs.get(LOCK_KEY);
        long expire = (long) annotationArgs.get(TIME_OUT);
        boolean expireConfig = (boolean) annotationArgs.get(EXPIRE_CONFIG);
        //分布式锁
        boolean lock = false;
        long expireTime = 0L;
        try {

                //设置过期时间
                if (expireConfig) {
                    expireTime  = expire;
                } else {
                    expireTime = timeOut;
                }
//1.占分布式锁的同时 给锁设置过期时间。
//这是一个原子性操作,要么同时成功,要么同时失败。
            //如果返回true,说明key不存在,获取到锁
            lock = redisTemplate.opsForValue().setIfAbsent(key, lockPrefix,expireTime, TimeUnit.SECONDS);
            log.info("是否获取到锁:" + lock);
            if (lock) {
                log.info("获取到锁,开启定时任务!");

                proceeding.proceed();
            } else {
                log.info("其他系统正在执行此项任务");
                return;
            }
        } catch (Exception e) {
            e.printStackTrace();
        } catch (Throwable throwable) {
            throw new RuntimeException("分布式锁执行发生异常" + throwable.getMessage(), throwable);
        }
    }

    /**
     * 获取锁参数
     *
     * @param proceeding
     * @return
     */
    private Map<String, Object> getAnnotationArgs(ProceedingJoinPoint proceeding) {
        Class target = proceeding.getTarget().getClass();
        Method[] methods = target.getMethods();
        String methodName = proceeding.getSignature().getName();
        for (Method method : methods) {
            if (method.getName().equals(methodName)) {
                Map<String, Object> result = new HashMap<String, Object>();
                RedisLock redisLock = method.getAnnotation(RedisLock.class);
                result.put(LOCK_PRE_FIX, redisLock.lockPrefix());
                result.put(LOCK_KEY, redisLock.lockKey());
                result.put(TIME_OUT, redisLock.timeUnit().toSeconds(redisLock.timeOut()));
                result.put(EXPIRE_CONFIG, redisLock.expireConfig());
                return result;
            }
        }
        return null;
    }

}

到此一个自定义注解实现redis分布式锁的代码就完成了,下面就是如何利用这个注解了

这里面需要注意,占分布式锁的同时 给锁设置过期时间。是为了保证原子性操作,要么同时成功,要么同时失败。这么做是为了防止死锁。


@RedisLock  
@Component
@Slf4j
public class MaterialStatisticsTask {

    private static final String lock_key = "material_statistics_task";

    private static final String lock_value = "material_statistics_task-ZKAW-YQS";

    @Autowired
    private IMaterialStatisticsServices materialStatisticsServices;

    // 每月1号0点30分执行
    @Scheduled(cron = "${schedule.statisticsCron}")
    @RedisLock(lockPrefix = lock_value, lockKey = lock_key, expireConfig = false)
    public void doTask() {
        try {
            log.info("物资收发存统计定时任务!");
            MaterialStatisticsVo materialStatisticsVo = new MaterialStatisticsVo();
            materialStatisticsServices.materialStatistics(materialStatisticsVo);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

这样就可以利用注解的形式实现分布式锁,这样每次需要用的时候直接一个注解就搞定了,避免了每次都要写很长的代码。

以上说的是redis实现分布式锁,那么加锁成功如何主动释放锁呢?

在解锁时要考虑到原子性,针对原子性操作,就可以考虑利用lua脚本去释放锁

String uuid = UuID.randomUuID() .tostring();
 /*================================释放锁===================================*/
            // 定义一个lua脚本
            String script = "if redis.call('get', KEYS[1]) == ARGV[1] then return redis.call('del', KEYS[1]) else return 0 end";
            // 创建对象
            DefaultRedisScript<Long> redisScript = new DefaultRedisScript();
            // 设置lua脚本
            redisScript.setScriptText(script);
            //设置lua脚本返回类型为Long
            redisScript.setResultType(Long.class);
            // redis调用lua脚本
            redisTemplate.execute(redisScript, Arrays.asList("lock"),uuid);

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

利用redis的setIfAbsent()方法实现分布式锁 的相关文章

随机推荐