关于springboot:Spring-Boot-Redis-实现分布式锁还有谁不会

3次阅读

共计 8367 个字符,预计需要花费 21 分钟才能阅读完成。

一、业务背景

有些业务申请,属于耗时操作,须要加锁,避免后续的并发操作,同时对数据库的数据进行操作,须要防止对之前的业务造成影响。


二、剖析流程

应用 Redis 作为分布式锁,将锁的状态放到 Redis 对立保护,解决集群中单机 JVM 信息不互通的问题,规定操作程序,爱护用户的数据正确。 梳理设计流程

  1. 新建注解 @interface,在注解里设定入参标记
  2. 减少 AOP 切点,扫描特定注解
  3. 建设 @Aspect 切面工作,注册 bean 和拦挡特定办法
  4. 特定办法参数 ProceedingJoinPoint,对办法 pjp.proceed() 前后进行拦挡
  5. 切点前进行加锁,工作执行后进行删除 key

外围步骤:加锁、解锁和续时

应用了 RedisTemplate 的 opsForValue.setIfAbsent 办法,判断是否有 key,设定一个随机数 UUID.random().toString,生成一个随机数作为 value。从 redis 中获取锁之后,对 key 设定 expire 生效工夫,到期后主动开释锁。依照这种设计,只有第一个胜利设定 Key 的申请,能力进行后续的数据操作,后续其它申请因为无奈取得🔐资源,将会失败完结。

超时问题

放心 pjp.proceed() 切点执行的办法太耗时,导致 Redis 中的 key 因为超时提前开释了。例如,线程 A 先获取锁,proceed 办法耗时,超过了锁超时工夫,到期开释了锁,这时另一个线程 B 胜利获取 Redis 锁,两个线程同时对同一批数据进行操作,导致数据不精确。

解决方案:减少一个「续时」

工作不实现,锁不开释: 保护了一个定时线程池 ScheduledExecutorService,每隔 2s 去扫描退出队列中的 Task,判断是否生效工夫是否快到了,公式为:【生效工夫】<=【以后工夫】+【生效距离(三分之一超时)】

/**
 * 线程池,每个 JVM 应用一个线程去保护 keyAliveTime,定时执行 runnable
 */
private static final ScheduledExecutorService SCHEDULER =
new ScheduledThreadPoolExecutor(1,
new BasicThreadFactory.Builder().namingPattern("redisLock-schedule-pool").daemon(true).build());
static {SCHEDULER.scheduleAtFixedRate(() -> {// do something to extend time}, 0,  2, TimeUnit.SECONDS);
}

三、设计方案

通过下面的剖析,共事小🐟设计出了这个计划:

后面曾经说了整体流程,这里强调一下几个外围步骤:

  • 拦挡注解 @RedisLock,获取必要的参数
  • 加锁操作
  • 续时操作
  • 完结业务,开释锁
    • *

四、实操

之前也有整顿过 AOP 应用办法,能够参考一下。

相干属性类配置

业务属性枚举设定

`public enum RedisLockTypeEnum {
    /**
     * 自定义 key 前缀
     */
    ONE("Business1", "Test1"),

    TWO("Business2", "Test2");
    private String code;
    private String desc;
    RedisLockTypeEnum(String code, String desc) {
        this.code = code;
        this.desc = desc;
    }
    public String getCode() {return code;}
    public String getDesc() {return desc;}
    public String getUniqueKey(String key) {return String.format("%s:%s", this.getCode(), key);
    }
}`

工作队列保留参数

`public class RedisLockDefinitionHolder {
    /**
     * 业务惟一 key
     */
    private String businessKey;
    /**
     * 加锁工夫 (秒 s)
     */
    private Long lockTime;
    /**
     * 上次更新工夫(ms)*/
    private Long lastModifyTime;
    /**
     * 保留以后线程
     */
    private Thread currentTread;
    /**
     * 总共尝试次数
     */
    private int tryCount;
    /**
     * 以后尝试次数
     */
    private int currentCount;
    /**
     * 更新的工夫周期(毫秒), 公式 = 加锁工夫(转成毫秒)/ 3
     */
    private Long modifyPeriod;
    public RedisLockDefinitionHolder(String businessKey, Long lockTime, Long lastModifyTime, Thread currentTread, int tryCount) {
        this.businessKey = businessKey;
        this.lockTime = lockTime;
        this.lastModifyTime = lastModifyTime;
        this.currentTread = currentTread;
        this.tryCount = tryCount;
        this.modifyPeriod = lockTime * 1000 / 3;
    }
}`

设定被拦挡的注解名字

`@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.METHOD, ElementType.TYPE})
public @interface RedisLockAnnotation {
    /**
     * 特定参数辨认,默认取第 0 个下标
     */
    int lockFiled() default 0;
    /**
     * 超时重试次数
     */
    int tryCount() default 3;
    /**
     * 自定义加锁类型
     */
    RedisLockTypeEnum typeEnum();
    /**
     * 开释工夫,秒 s 单位
     */
    long lockTime() default 30;}`

外围切面拦挡的操作

RedisLockAspect.java 该类分成三局部来形容具体作用

Pointcut 设定

/**
 * @annotation 中的门路示意拦挡特定注解
 */
@Pointcut("@annotation(cn.sevenyuan.demo.aop.lock.RedisLockAnnotation)")
public void redisLockPC() {}

Around 前后进行加锁和开释锁

后面步骤定义了咱们想要拦挡的切点,下一步就是在切点前后做一些自定义操作:

@Around(value = "redisLockPC()")
public Object around(ProceedingJoinPoint pjp) throws Throwable {
    // 解析参数
    Method method = resolveMethod(pjp);
    RedisLockAnnotation annotation = method.getAnnotation(RedisLockAnnotation.class);
    RedisLockTypeEnum typeEnum = annotation.typeEnum();
    Object[] params = pjp.getArgs();
    String ukString = params[annotation.lockFiled()].toString();
    // 省略很多参数校验和判空
    String businessKey = typeEnum.getUniqueKey(ukString);
    String uniqueValue = UUID.randomUUID().toString();
    // 加锁
    Object result = null;
    try {boolean isSuccess = redisTemplate.opsForValue().setIfAbsent(businessKey, uniqueValue);
        if (!isSuccess) {throw new Exception("You can't do it,because another has get the lock =-=");
        }
        redisTemplate.expire(businessKey, annotation.lockTime(), TimeUnit.SECONDS);
        Thread currentThread = Thread.currentThread();
        // 将本次 Task 信息退出「延时」队列中
        holderList.add(new RedisLockDefinitionHolder(businessKey, annotation.lockTime(), System.currentTimeMillis(),
                currentThread, annotation.tryCount()));
        // 执行业务操作
        result = pjp.proceed();
        // 线程被中断,抛出异样,中断此次申请
        if (currentThread.isInterrupted()) {throw new InterruptedException("You had been interrupted =-=");
        }
    } catch (InterruptedException e) {log.error("Interrupt exception, rollback transaction", e);
        throw new Exception("Interrupt exception, please send request again");
    } catch (Exception e) {log.error("has some error, please check again", e);
    } finally {
        // 申请完结后,强制删掉 key,开释锁
        redisTemplate.delete(businessKey);
        log.info("release the lock, businessKey is [" + businessKey + "]");
    }
    return result;
}

上述流程简略总结一下:

  • 解析注解参数,获取注解值和办法上的参数值
  • redis 加锁并且设置超时工夫
  • 将本次 Task 信息退出「延时」队列中,进行续时,形式提前开释锁
  • 加了一个线程中断标记
  • 完结申请,finally 中开释锁

续时操作这里用了 ScheduledExecutorService,保护了一个线程,一直对工作 [队列中的工作进行判断和缩短超时工夫:

`// 扫描的工作队列
private static ConcurrentLinkedQueue<RedisLockDefinitionHolder> holderList = new ConcurrentLinkedQueue();
/**
 * 线程池,保护 keyAliveTime
 */
private static final ScheduledExecutorService SCHEDULER = new ScheduledThreadPoolExecutor(1,
        new BasicThreadFactory.Builder().namingPattern("redisLock-schedule-pool").daemon(true).build());
{
    // 两秒执行一次「续时」操作
    SCHEDULER.scheduleAtFixedRate(() -> {
        // 这里记得加 try-catch,否者报错后定时工作将不会再执行 =-=
        Iterator<RedisLockDefinitionHolder> iterator = holderList.iterator();
        while (iterator.hasNext()) {RedisLockDefinitionHolder holder = iterator.next();
            // 判空
            if (holder == null) {iterator.remove();
                continue;
            }
            // 判断 key 是否还无效,有效的话进行移除
            if (redisTemplate.opsForValue().get(holder.getBusinessKey()) == null) {iterator.remove();
                continue;
            }
            // 超时重试次数,超过时给线程设定中断
            if (holder.getCurrentCount() > holder.getTryCount()) {holder.getCurrentTread().interrupt();
                iterator.remove();
                continue;
            }
            // 判断是否进入最初三分之一工夫
            long curTime = System.currentTimeMillis();
            boolean shouldExtend = (holder.getLastModifyTime() + holder.getModifyPeriod()) <= curTime;
            if (shouldExtend) {holder.setLastModifyTime(curTime);
                redisTemplate.expire(holder.getBusinessKey(), holder.getLockTime(), TimeUnit.SECONDS);
                log.info("businessKey : [" + holder.getBusinessKey() + "], try count :" + holder.getCurrentCount());
                holder.setCurrentCount(holder.getCurrentCount() + 1);
            }
        }
    }, 0, 2, TimeUnit.SECONDS);
}`

这段代码,用来实现设计图中虚线框的思维,防止一个申请非常耗时,导致提前开释了锁。 这里加了「线程中断」Thread#interrupt,心愿超过重试次数后,能让线程中断 **(未经谨严测试,仅供参考哈哈哈哈)不过倡议如果遇到这么耗时的申请,还是可能从本源上查找,剖析耗时门路,进行业务优化或其它解决,防止这些耗时操作。所以记得多打点 Log,剖析问题时能够更快一点。如何应用 SpringBoot AOP 记录操作日志、异样日志?


五、开始测试

在一个入口办法中,应用该注解,而后在业务中模仿耗时申请,应用了 Thread#sleep

@GetMapping("/testRedisLock")
@RedisLockAnnotation(typeEnum = RedisLockTypeEnum.ONE, lockTime = 3)
public Book testRedisLock(@RequestParam("userId") Long userId) {
    try {log.info("睡眠执行前");
        Thread.sleep(10000);
        log.info("睡眠执行后");
    } catch (Exception e) {
        // log error
        log.info("has some error", e);
    }
    return null;
}

应用时,在办法上增加该注解,而后设定相应参数即可,依据 typeEnum 能够辨别多种业务,限度该业务被同时操作。测试后果:

2020-04-04 14:55:50.864  INFO 9326 --- [nio-8081-exec-1] c.s.demo.controller.BookController       : 睡眠执行前
2020-04-04 14:55:52.855  INFO 9326 --- [k-schedule-pool] c.s.demo.aop.lock.RedisLockAspect        : businessKey : [Business1:1024], try count : 0
2020-04-04 14:55:54.851  INFO 9326 --- [k-schedule-pool] c.s.demo.aop.lock.RedisLockAspect        : businessKey : [Business1:1024], try count : 1
2020-04-04 14:55:56.851  INFO 9326 --- [k-schedule-pool] c.s.demo.aop.lock.RedisLockAspect        : businessKey : [Business1:1024], try count : 2
2020-04-04 14:55:58.852  INFO 9326 --- [k-schedule-pool] c.s.demo.aop.lock.RedisLockAspect        : businessKey : [Business1:1024], try count : 3
2020-04-04 14:56:00.857  INFO 9326 --- [nio-8081-exec-1] c.s.demo.controller.BookController       : has some error
java.lang.InterruptedException: sleep interrupted
 at java.lang.Thread.sleep(Native Method) [na:1.8.0_221]

我这里测试的是重试次数过多,失败的场景,如果缩小睡眠工夫,就能让业务失常执行。如果同时申请,你将会发现以下错误信息:

示意咱们的锁🔐确实失效了,防止了反复申请。


六、总结

对于耗时业务和外围数据,不能让反复的申请同时操作数据,防止数据的不正确,所以要应用分布式锁来对它们进行爱护。再来梳理一下设计流程:

  1. 新建注解 @interface,在注解里设定入参标记
  2. 减少 AOP 切点,扫描特定注解
  3. 建设 @Aspect 切面工作,注册 bean 和拦挡特定办法
  4. 特定办法参数 ProceedingJoinPoint,对办法 pjp.proceed() 前后进行拦挡
  5. 切点前进行加锁,工作执行后进行删除 key

本次学习是通过 Review 小伙伴的代码设计,从中理解分布式锁的具体实现,仿照他的设计,从新写了一份简化版的业务解决。对于之前没思考到的「续时」操作,这里应用了守护线程来定时判断和缩短超时工夫,防止了锁提前开释。于是乎,同时回顾了三个知识点:1、AOP 的实现和罕用办法 2、定时线程池 ScheduledExecutorService 的应用和参数含意 3、线程 Thread#interrupt 的含意以及用法(这个挺有意思的,能够深刻再学习一下)

正文完
 0