SpringBoot系列之基于Jedis实现分布式锁

发布时间:2023年12月17日

Redis系列之基于Jedis实现分布式锁

1、为什么需要分布式锁

在单机环境,我们使用最多的是juc包里的单机锁,但是随着微服务分布式项目的普及,juc里的锁是不能控制分布锁环境的线程安全的,因为单机锁只能控制同个进程里的线程安全,不能控制多节点的线程安全,所以就需要使用分布式锁

2、redis分布式锁原理

学习之前先了解redis的命令,setnxexpire

  • setnx命令

    SETNX是SET if not exists的简写,设置key的值,如果key值不存在,则可以设置,否则不可以设置,这个有点像juc中cas锁的原理

    # setnx命令,相当于set和nx命令一起用
    setnx tkey aaa
    

    EX : 设置指定的到期时间(以秒为单位)。

    PX : 设置指定的到期时间(以毫秒为单

    NX : 仅在键不存在时设置键。

    XX : 只有在键已存在时才设置。

  • expire命令

如果只使用setnx不加上过期时间,手动释放锁时候出现异常,就会导致一直解不了锁,所以还是要加上expire命令来设置过期时间。

  • 保证原子性

但是又有一个问题,设置过期时间时候报错了,也同样会导致锁释放不了,所以为了保证原子性,需要这两个命令一起执行

# set tkey过期时间10秒,nx:如果键不存在时设置
set tkey aaa ex 10 nx

3、基于jedis手写分布锁锁

基于上面的原理,我们就可以简单写一个分布锁锁

项目环境:

  • JDK 1.8

  • SpringBoot 2.2.1

  • Maven 3.2+

  • Mysql 8.0.26

  • spring-boot-starter-data-redis 2.2.1

  • jedis3.1.0

  • 开发工具

    • IntelliJ IDEA

    • smartGit

先搭建一个springboot集成jedis的例子工程,参考我之前的博客,大体的类图如图所示:

在这里插入图片描述

写一个分布锁的通用接口,因为以后可能会通过其它中间件实现分布锁锁

package com.example.jedis.common;

public interface DistributedLock {

    default boolean acquire(String lockKey, String requestId) {
        return acquire(lockKey, requestId, RedisConstant.DEFAULT_EXPIRE);
    }

    default boolean acquire(String lockKey, String requestId, int expireTime) {
        return acquire(lockKey, requestId, expireTime, RedisConstant.DEFAULT_TIMEOUT);
    }

    boolean acquire(String lockKey, String requestId, int expireTime, int timeout);

    boolean release(String lockKey, String requestId);

}

写一个抽象的分布锁锁类,实现一些可以共用的逻辑,其它的业务给子类去实现

package com.example.jedis.common;

import lombok.extern.slf4j.Slf4j;

import java.net.SocketTimeoutException;
import java.util.concurrent.TimeUnit;

import static com.example.jedis.common.RedisConstant.DEFAULT_EXPIRE;
import static com.example.jedis.common.RedisConstant.DEFAULT_TIMEOUT;

@Slf4j
public abstract class AbstractDistributedLock implements DistributedLock {

    @Override
    public boolean acquire(String lockKey, String requestId, int expireTime, int timeout) {
        expireTime = expireTime <= 0 ? DEFAULT_EXPIRE : expireTime;
        timeout = timeout < 0 ? DEFAULT_TIMEOUT : timeout * 1000;

        long start = System.currentTimeMillis();
        try {
            do {
                if (doAcquire(lockKey, requestId, expireTime)) {
                    watchDog(lockKey, requestId, expireTime);
                    return true;
                }
                TimeUnit.MILLISECONDS.sleep(100);
            } while (System.currentTimeMillis() - start < timeout);

        } catch (Exception e) {
            Throwable cause = e.getCause();
            if (cause instanceof SocketTimeoutException) {
                // ignore exception
                log.error("sockTimeout exception:{}", e);
            }
            else if (cause instanceof  InterruptedException) {
                // ignore exception
                log.error("Interrupted exception:{}", e);
            }
            else {
                log.error("lock acquire exception:{}", e);
            }
            throw new LockException(e.getMessage(), e);
        }
        return false;
    }

    @Override
    public boolean release(String lockKey, String requestId) {
        try {
            return doRelease(lockKey, requestId);
        } catch (Exception e) {
            log.error("lock release exception:{}", e);
            throw new LockException(e.getMessage(), e);
        }
    }

    protected abstract boolean doAcquire(String lockKey, String requestId, int expireTime);

    protected abstract boolean doRelease(String lockKey, String requestId);

    protected abstract void watchDog(String lockKey, String requestId, int expireTime);

}

redis的分布锁锁抽象类

package com.example.jedis.common;

public abstract class AbstractRedisLock extends AbstractDistributedLock{

}

基于jedis的分布锁实现类,主要通过lua脚本控制解锁的原子性,同时加上watch dog定时续期,避免有些长业务执行时间比较长,而锁已经释放的情况

package com.example.jedis.common;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.convert.Convert;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.concurrent.ScheduledThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

@Component
@Slf4j
public class JedisLockTemplate extends AbstractRedisLock implements InitializingBean {

    private String UNLOCK_LUA = "if redis.call('get', KEYS[1]) == ARGV[1] then return redis.call('del', KEYS[1]) else return 0 end";

    private String WATCH_DOG_LUA = "local lock_key=KEYS[1]\n" +
            "local lock_value=ARGV[1]\n" +
            "local lock_ttl=ARGV[2]\n" +
            "local current_value=redis.call('get',lock_key)\n" +
            "local result=0\n" +
            "if lock_value==current_value then\n" +
            "    redis.call('expire',lock_key,lock_ttl)\n" +
            "    result=1\n" +
            "end\n" +
            "return result";

    private static final Long UNLOCK_SUCCESS = 1L;

    private static final Long RENEWAL_SUCCESS = 1L;

    @Autowired
    private JedisTemplate jedisTemplate;

    private ScheduledThreadPoolExecutor scheduledExecutorService;


    @Override
    public void afterPropertiesSet() throws Exception {
        this.UNLOCK_LUA = jedisTemplate.scriptLoad(UNLOCK_LUA);
        this.WATCH_DOG_LUA = jedisTemplate.scriptLoad(WATCH_DOG_LUA);
        scheduledExecutorService = new ScheduledThreadPoolExecutor(1);
    }


    @Override
    public boolean doAcquire(String lockKey, String requestId, int expire) {
        return jedisTemplate.setnxex(lockKey, requestId, expire);
    }

    @Override
    public boolean doRelease(String lockKey, String requestId) {
        Object eval = jedisTemplate.evalsha(UNLOCK_LUA, CollUtil.newArrayList(lockKey), CollUtil.newArrayList(requestId));
        if (UNLOCK_SUCCESS.equals(eval)) {
            scheduledExecutorService.shutdown();
            return true;
        }
        return false;
    }

    @Override
    public void watchDog(String lockKey, String requestId, int expire) {
        int period = getPeriod(expire);
        if (scheduledExecutorService.isShutdown()) {
            scheduledExecutorService = new ScheduledThreadPoolExecutor(1);
        }
        scheduledExecutorService.scheduleAtFixedRate(
                new WatchDogTask(scheduledExecutorService, CollUtil.newArrayList(lockKey), CollUtil.newArrayList(requestId, Convert.toStr(expire))),
                1,
                period,
                TimeUnit.SECONDS
                );
    }

    class WatchDogTask implements Runnable {

        private ScheduledThreadPoolExecutor scheduledThreadPoolExecutor;
        private List<String> keys;
        private List<String> args;

        public WatchDogTask(ScheduledThreadPoolExecutor scheduledThreadPoolExecutor, List<String> keys, List<String> args) {
            this.scheduledThreadPoolExecutor = scheduledThreadPoolExecutor;
            this.keys = keys;
            this.args = args;
        }

        @Override
        public void run() {
            log.info("watch dog for renewal...");
            Object evalsha = jedisTemplate.evalsha(WATCH_DOG_LUA, keys, args);
            if (!evalsha.equals(RENEWAL_SUCCESS)) {
                scheduledThreadPoolExecutor.shutdown();
            }
            log.info("renewal result:{}, keys:{}, args:{}", evalsha, keys, args);
        }
    }

    private int getPeriod(int expire) {
        if (expire < 1)
            throw new LockException("expire不允许小于1");
        return expire - 1;
    }



}

写一个通用的jedis常有api的封装类,setnxex加上synchronized,因为redis是单线程的,加上同步锁,避免并发请求时候出现,jedispool加载不到的情况

package com.example.jedis.common;

import cn.hutool.core.collection.ConcurrentHashSet;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.ScanParams;
import redis.clients.jedis.ScanResult;
import redis.clients.jedis.exceptions.JedisConnectionException;
import redis.clients.jedis.exceptions.JedisDataException;
import redis.clients.jedis.exceptions.JedisException;
import redis.clients.jedis.params.SetParams;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.function.Consumer;
import java.util.function.Function;


@Slf4j
@Component
public class JedisTemplate implements InitializingBean {



    @Resource
    private JedisPool jedisPool;

    private Jedis jedis;

    public JedisTemplate() {

    }

    @Override
    public void afterPropertiesSet() {
        jedis = jedisPool.getResource();
    }


    public <T> T execute(Function<Jedis, T> action) {
        T apply = null;
        try {
            jedis = jedisPool.getResource();
            apply = action.apply(jedis);
        } catch (JedisException e) {
            handleException(e);
            throw e;
        } finally {
            jedis.close();
        }
        return apply;
    }

    public void execute(Consumer<Jedis> action) {
        try {
            jedis = jedisPool.getResource();
            action.accept(jedis);
        } catch (JedisException e) {
            handleException(e);
            throw e;
        } finally {
            jedis.close();
        }
    }

    public JedisPool getJedisPool() {
        return this.jedisPool;
    }

   
    public synchronized Boolean setnxex(final String key, final String value, int seconds) {
        return execute(e -> {
            SetParams setParams = new SetParams();
            setParams.nx();
            setParams.ex(seconds);
            return isStatusOk(jedis.set(key, value, setParams));
        });
    }
    

    public Object eval(final String script,final Integer keyCount,final String... params) {
        return execute(e -> {
            return jedis.eval(script, keyCount, params);
        });
    }

    public Object eval(final String script, final List<String> keys, final List<String> params) {
        return execute(e -> {
            return jedis.eval(script, keys, params);
        });
    }

    public Object evalsha(final String script, final List<String> keys, final List<String> params) {
        return execute(e -> {
            return jedis.evalsha(script, keys, params);
        });
    }

    public String scriptLoad(final String script) {
        return execute(e -> {
            return jedis.scriptLoad(script);
        });
    }
    

    protected void handleException(JedisException e) {
        if (e instanceof JedisConnectionException) {
            log.error("redis connection exception:{}", e);
        } else if (e instanceof JedisDataException) {
            log.error("jedis data exception:{}", e);
        } else {
            log.error("jedis exception:{}", e);
        }
    }

    protected synchronized static boolean isStatusOk(String status) {
        return status != null && ("OK".equals(status) || "+OK".equals(status));
    }

}

常量类

package com.example.jedis.common;

public class RedisConstant {

    public static final Integer DEFAULT_EXPIRE = 30;
    public static final Integer DEFAULT_TIMEOUT = 1;


}

自定义的异常类:

package com.example.jedis.common;

public class LockException extends RuntimeException{

    public LockException(String message) {
        super(message);
    }

    public LockException(String message, Throwable t) {
        super(message, t);
    }

}

SpringBoot启动的Application类

package com.example.jedis;

import cn.hutool.core.date.StopWatch;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.annotation.EnableScheduling;

import javax.annotation.PreDestroy;
import javax.annotation.Resource;


@SpringBootApplication
@EnableScheduling
@EnableAsync
@Slf4j
public class SpringbootJedisApplication {

    @Resource
    RedisConnectionFactory factory;


    public static void main(String[] args) {
        StopWatch stopWatch = new StopWatch();
        stopWatch.start("springbootJedis");
        SpringApplication.run(SpringbootJedisApplication.class, args);
        stopWatch.stop();
        log.info("Springboot项目启动成功时间:{}ms \n", stopWatch.getTotalTimeMillis());
        log.info(stopWatch.prettyPrint());
    }

    @PreDestroy
    public void flushDB() {
        factory.getConnection().flushDb();
    }

}

上面的逻辑已经基本实现了一款分布式锁,也可以加一个自定义注解来实现

package com.example.jedis.common;

import java.lang.annotation.*;

@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface Lock {

    String lockKey();

    String requestId();

    int expire() default 30;

    int timeout() default  1;

}

自定义一个切面类,实现业务处理

package com.example.jedis.common;


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.reflect.MethodSignature;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.lang.reflect.Method;
import java.util.concurrent.Future;

@Component
@Aspect
@Slf4j
public class WatchDog {

    @Resource
    private JedisLockTemplate jedisLockTemplate;

    @Resource
    private ThreadPoolTaskExecutor executor;


    @Around("@annotation(Lock)")
    public Object proxy (ProceedingJoinPoint joinPoint) throws Throwable {
        MethodSignature methodSignature = (MethodSignature) joinPoint.getSignature();
        Method method = methodSignature.getMethod();
        Lock lock = method.getAnnotation(Lock.class);

        boolean acquire = jedisLockTemplate.acquire(lock.lockKey(), lock.requestId(), lock.expire(), lock.timeout());
        if (!acquire)
            throw new LockException("获取锁失败!");

        Future<Object> future = executor.submit(() -> {
            try {
                return joinPoint.proceed();
            } catch (Throwable e) {
                log.error("任务执行错误:{}", e);
                jedisLockTemplate.release(lock.lockKey(), lock.requestId());
                throw new RuntimeException("任务执行错误");
            } finally {
                jedisLockTemplate.release(lock.lockKey(), lock.requestId());
            }
        });

        return future.get();
    }


}

写一个测试Controller类,开始用SpringBoot测试类的,但是发现有时候还是经常出现一些连接超时情况,这个可能是框架兼容的bug

package com.example.jedis.controller;

import com.example.jedis.common.JedisLockTemplate;
import com.example.jedis.common.Lock;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Random;
import java.util.concurrent.CountDownLatch;
import java.util.stream.IntStream;

@RestController
@Slf4j
public class TestController {


    private static final String REDIS_KEY = "test:lock";

    @Autowired
    private JedisLockTemplate jedisLockTemplate;

    @GetMapping("test")
    public void test(@RequestParam("threadNum")Integer threadNum) throws InterruptedException {

        CountDownLatch countDownLatch = new CountDownLatch(threadNum);
        IntStream.range(0, threadNum).forEach(e->{
            new Thread(new RunnableTask(countDownLatch)).start();
        });
        countDownLatch.await();


    }

    @GetMapping("testLock")
    @Lock(lockKey = "test:api", requestId = "123", expire = 5, timeout = 3)
    public void testLock() throws InterruptedException {
        doSomeThing();
    }

    class RunnableTask implements Runnable {

        CountDownLatch countDownLatch;

        public RunnableTask(CountDownLatch countDownLatch) {
            this.countDownLatch = countDownLatch;
        }

        @Override
        public void run() {
            redisLock();
            countDownLatch.countDown();
        }


    }


    private void redisLock() {
        String requestId = getRequestId();
        Boolean lock = jedisLockTemplate.acquire(REDIS_KEY, requestId, 5, 3);
        if (lock) {
            try {
                doSomeThing();
            } catch (Exception e) {
                jedisLockTemplate.release(REDIS_KEY, requestId);
            } finally {
                jedisLockTemplate.release(REDIS_KEY, requestId);
            }
        } else {
            log.warn("获取锁失败!");
        }
    }

    private void doSomeThing() throws InterruptedException {
        log.info("do some thing");
        Thread.sleep(15 * 1000);
    }

    private String getRequestId() {
        String str="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
        Random random=new Random();
        StringBuffer sb=new StringBuffer();
        for(int i=0;i<32;i++){
            int number=random.nextInt(62);
            sb.append(str.charAt(number));
        }
        return sb.toString();

    }



}

# 模拟100个并发请求
curl http://127.0.0.1:8080/springboot-jedis/test?threadNum=100

在这里插入图片描述

在这里插入图片描述

项目启动出现这种问题,有可能是在SpringBoot的junit测试类里测试的,setnxex方法加上synchronize同步锁

java.net.SocketTimeoutException: Read timed out

Could not get a resource from the pool

总结:本文基于jedis、jua脚本实现一个分布式锁,redis分布式锁是基于AP模式的,所以效率还是比较快的,但是不能保证分布式的CP模式,如果要保证高一致性,可以选用其它分布式锁方案,本文还考虑到长事务的情况,使用watchdog对key进行续期

文章来源:https://blog.csdn.net/u014427391/article/details/134884618
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。