基于RedisTemplate的redis分布式锁, 以及注解实现

7 篇文章 1 订阅
3 篇文章 0 订阅

基于Redis的分布式锁最佳实践

欢迎转载,转载请注明网址:https://blog.csdn.net/qq_41910280

版本说明
2019-03-07 初始版本
2020-06-17 新增springboot 1.5版本代码
2021-12-07 1.加锁改为使用lua脚本,解耦对jedis的依赖,可以使用lutte、redisson、jedis等任意客户端
    2.增加redisson的分布式锁
    本文不再更新,新地址为https://blog.csdn.net/qq_41910280/article/details/121769533

简介
  本文基于spring data redis完成了redis分布式锁, 以及注解实现。Talk is cheap, let’s Getting Started.

1. 环境

  redis: 5.0.0
  spring data redis:2.1.5
  spring boot: 2.1.3.RELEASE

2. 核心代码

  主要注意: 上锁lock 以及 解锁unlock 的原子性操作, 以此避免了死锁或误删其他线程或服务所上的锁

2. 1 springboot 2.0以上版本代码

package com.example.lock;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.connection.RedisStringCommands;
import org.springframework.data.redis.core.RedisCallback;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.script.RedisScript;
import org.springframework.data.redis.core.types.Expiration;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.TimeUnit;

/**
 * @author zhouyou
 * @blog https://blog.csdn.net/qq_41910280
 */
@Component
public class RedisLockTool {
    private static final Logger logger = LoggerFactory.getLogger(RedisLockTool.class);
    @Autowired
    StringRedisTemplate redisTemplate;
    /**
     * 存放本台服务器注册的所有key, 定期对所有的key设置过期时间操作
     */
    private Map<String, String> keepAliveLocks = new ConcurrentHashMap<>();
    private final Timer timer = new Timer();
    private static StringRedisSerializer stringRedisSerializer = new StringRedisSerializer();
    @Value("${redis.lock.aliveMilis:600000}")
    private long aliveMilis;
    @Value("${redis.lock.intervalMilis:300000}")
    private long intervalMilis;

    /**
     * 分布式锁lock
     * @param key         锁名
     * @param id          标识符, 只有同一个id才能解key锁
     * @param expireMilis 超时时间, (expireMilis==-1 && keepAlive=false)表示为永不过期
     * @param keepAlive   是否让key锁保持alive
     * @return
     */
    public Boolean lock(String key, String id, long expireMilis, boolean keepAlive) {
        logger.info("lock入参 key={}, id={}, expireMilis={}, keepAlive={}", key, id, expireMilis, keepAlive);
        Boolean execute = redisTemplate.execute((RedisCallback<Boolean>) connection -> {
            RedisStringCommands redisStringCommands = connection.stringCommands();
            Boolean set = redisStringCommands.set(stringRedisSerializer.serialize(key), stringRedisSerializer.serialize(id),
                    Expiration.milliseconds(expireMilis), RedisStringCommands.SetOption.SET_IF_ABSENT);
            connection.close();
            return set;
        });
        logger.info("lock 出参 execute={}",execute);
        if (execute && keepAlive) {
            keepAliveLocks.put(key, "");
            keeplockAlive();
        }
        return execute;
    }

    //在bean初始化完成之后启动timer,在timer任务中给锁续命
    @PostConstruct
    private void init() {
        timer.schedule(new TimerTask() {
            @Override
            public void run() {
                keeplockAlive();
            }
        }, 0, intervalMilis);
    }

    //在销毁之前将timer取消
    @PreDestroy
    private void destroy() {
        timer.cancel();
    }

    private void keeplockAlive() {
        if (keepAliveLocks.size() > 0) {
            for (String key : keepAliveLocks.keySet()) {
                redisTemplate.expire(key, aliveMilis, TimeUnit.MILLISECONDS);
            }
        }
    }

    /**
     * 解锁 解铃还需系铃人
     * @param key 锁名
     * @param id  标识符, id相同才能解锁
     * @return 1.id相同而且key没有过期 2.key过期id=null 返回true
     */
    public boolean unlock(String key, String id) {
        /*
        必须先从concurrentHashMap中删除 再删redis的key
        避免其他线程中途刚在redis注册key就被从concurrentHashMap删除
         */
        String redisId = redisTemplate.opsForValue().get(key);
        logger.info("unlock入参 key={}, id={}; redis中id={}", key, id, redisId);
        String script = "if redis.call('get', KEYS[1]) == ARGV[1] then return redis.call('del', KEYS[1]) else return '0' end";
        if (Objects.equals(id, redisId)) {
            keepAliveLocks.remove(key);
            /*
            不直接使用redisTemplate.delete(key)
            因为可能在delete之前刚好key过期并且key被另一线程或机器注册导致误删
             */
            Long execute = redisTemplate.execute((RedisScript<Long>) RedisScript.of(script, Long.class),// java的Long对应redis的integer
//                    stringRedisSerializer, new GenericJackson2JsonRedisSerializer(),
                    Collections.singletonList(key), id);
            // 与上述方法相同
//            Object execute = redisTemplate.execute(
//                    (RedisConnection connection) -> connection.eval(
//                            script.getBytes(),
//                            ReturnType.INTEGER,
//                            1,
//                            key.getBytes(),
//                            id.getBytes())
//            );
            logger.info("unlock 出参 execute={}", execute);
            return Objects.equals(execute, 1L);
        }
        return false;
    }

}

2. 2 springboot 1.5版本代码


import com.alibaba.fastjson.support.spring.FastJsonRedisSerializer;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisCallback;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.script.DefaultRedisScript;
import org.springframework.data.redis.core.script.RedisScript;
import org.springframework.data.redis.serializer.RedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import org.springframework.stereotype.Component;
import redis.clients.jedis.Jedis;

import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.Collections;
import java.util.Objects;
import java.util.concurrent.TimeUnit;

/**
 * redis分布式锁
 *
 * @author zhouyou
 * @date 2020/1/17 17:22
 * @email zhouyouchn@126.com
 * 版权声明:本文为CSDN博主「Spark4J」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
 * 原文链接:https://blog.csdn.net/qq_41910280/article/details/88837576
 */
@Component
public class RedisLockTool {
    private static final Logger logger = LoggerFactory.getLogger(RedisLockTool.class);
    private final StringRedisSerializer stringRedisSerializer = new StringRedisSerializer();
    private static final RedisSerializer<Long> VALUE_SERIALIZER = new FastJsonRedisSerializer(Long.class);
    @Autowired
    StringRedisTemplate redisTemplate;
    private static final long DEFAUT_WAIT_MS = 120_000L;
    private static final RedisScript<Long> SCRIPT = new DefaultRedisScript("if redis.call('get', KEYS[1]) == ARGV[1] then return redis.call('del', KEYS[1]) else return '0' end", Long.class);

    /**
     * 加锁
     *
     * @param key         锁的key
     * @param identifier  锁标识
     * @param lockTimeout 锁的超时时间 毫秒
     * @return 锁标识 null表示加锁失败
     */
    public String lock(String key, String identifier, long lockTimeout) {
        return lock(key, identifier, lockTimeout, DEFAUT_WAIT_MS);
    }

    /**
     * 加锁
     *
     * @param key         锁的key
     * @param identifier  锁标识
     * @param lockTimeout 锁的超时时间 毫秒
     * @return 锁标识 null表示加锁失败
     */
    public String lock(String key, String identifier, long lockTimeout, long waitTimeout) {
        identifier = getIdentifier(identifier);
        key = getKey(key);
        // 根据并发调整超时时间
        long end = System.currentTimeMillis() + waitTimeout;
        while (System.currentTimeMillis() < end) {
            if (setNx(key, identifier, lockTimeout)) {
                // 返回value值,用于释放锁时间确认
                logger.info("lock success, key:{}, identifier:{}", key, identifier);
                return identifier;
            } else {
                logger.info("lock fail, key:{}", key);
            }

            try {
                Thread.sleep(100);
            } catch (InterruptedException e) {
                Thread.currentThread().interrupt();
            }
        }
        return null;
    }

    /**
     * 加锁 不等待 直接返回
     * 用于分布式中只需要一个服务进行运算
     *
     * @param key        锁的key
     * @param identifier 锁标识
     * @param timeout    锁的超时时间 毫秒
     * @return 锁标识 null表示加锁失败
     */
    public String lockNoWait(String key, String identifier, long timeout) {
        identifier = getIdentifier(identifier);
        key = getKey(key);
        if (setNx(key, identifier, timeout)) {
            // 返回value值,用于释放锁时间确认
            logger.info("lock success, key:{}, identifier:{}", key, identifier);
            return identifier;
        } else {
            logger.info("lock fail, key:{}", key);
        }
        return null;
    }

    /**
     * 重置锁时效
     *
     * @param keyName    锁的key
     * @param identifier 锁标识
     * @param timeout    锁的超时时间 毫秒
     * @return 锁标识 null表示失败
     */
    public String resetLockExp(String keyName, String identifier, long timeout) {
        identifier = getIdentifier(identifier);
        String lockKey = getKey(keyName);

        if (identifier.equals(getValue(lockKey))) {
            expireByMill(lockKey, timeout);
            // 返回value值,用于释放锁时间确认
            logger.info("reset lock success, key:{}, id:{}", lockKey, identifier);
            return identifier;
        } else {
            logger.info("reset lock fail, key:{}", lockKey);
            return null;
        }

    }

// TimeUtils只是我基于jdk8时间类编写的工具类 这里是获得uuuuMMddHHmmss格式
    private String getIdentifier(String identifier) {
        if (StringUtils.isBlank(identifier)) {
            identifier = TimeUtils.now(TimeUtils.ISO_DATETIME_WITHOUT_HYPHEN) + "-" + getLocalHostName();
        }
        return identifier;
    }

    /**
     * 释放锁
     *
     * @param key        锁的key
     * @param identifier 释放锁的标识
     * @return
     */
    public boolean unlock(String key, String identifier) {
        key = getKey(key);
        // 此为方法一 或直接采用jedis执行脚本
        Long execute = redisTemplate.execute(SCRIPT, redisTemplate.getStringSerializer(), VALUE_SERIALIZER, Collections.singletonList(key), identifier);
        return Objects.equals(execute, 1L);
    }

    public String getValue(String key) {
        return redisTemplate.opsForValue().get(key);
    }

    public String getLockValue(String key) {
        return redisTemplate.opsForValue().get(getKey(key));
    }

    public String getKey(String keyName) {
        return "LOCK:" + keyName;
    }

    private String getLocalHostName() {
        try {
            return InetAddress.getLocalHost().getHostName();
        } catch (UnknownHostException e) {
            logger.error("InetAddress.getLocalHost出现异常", e);
            return StringUtils.EMPTY;
        }
    }

    private void remove(String key) {
        redisTemplate.delete(key);
    }

    private boolean setNx(final String key, final String value, long ms) {
        boolean b = false;
        try {
            b = redisTemplate.execute((RedisCallback<Boolean>) connection -> {
                Jedis jedis = (Jedis) connection.getNativeConnection();
                String set = jedis.set(key, value, "NX", "PX", ms);
                return "OK".equals(set);
            });
        } catch (Exception e) {
            logger.error("redis setNxEx error, key : {}", key);
        }
        return b;
    }

    private void expireByMill(String key, long exp) {
        redisTemplate.expire(key, exp, TimeUnit.MILLISECONDS);
    }

    /**
     * @param key
     * @return 秒
     */
    public Long ttl(String key) {
        return redisTemplate.getExpire(key);
    }
}

  …

3. 注解

1.注解类

/**
 * 分布式同步锁
 */
package com.example.annotation;

import java.lang.annotation.*;

/**
 * 分布式同步锁
 */
@Documented
@Target({ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
public @interface RedisSynchronized {
    String value() default "";

    String id() default "no identifier";

    long expireMilis() default 60000L;
}

2.处理注解的类

package com.example.annotation;

import com.example.lock.RedisLockTool;
import org.springframework.aop.support.AopUtils;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.cglib.proxy.Enhancer;
import org.springframework.cglib.proxy.InvocationHandler;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

import java.lang.reflect.Method;

@Component
public class RedisLockAutoProxyCreator implements BeanPostProcessor, ApplicationContextAware {
    @Autowired
    RedisLockTool redisLockTool;
//    private Map<String, ReflectiveMethodInvocation> fallbackInvocations = new HashMap<String, ReflectiveMethodInvocation>();

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
    }

    @Override
    public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
        return bean;
    }

    @Override
    public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
        Class<?> clazz = AopUtils.getTargetClass(bean);
        for (Method method : clazz.getDeclaredMethods()) {
            if (method.isAnnotationPresent(RedisSynchronized.class)) {
                Enhancer enhancer = new Enhancer();
                enhancer.setSuperclass(clazz);
                enhancer.setCallback(new RedisLockAnnotationHandler(bean));
                return enhancer.create();
            }
        }
        return bean;
    }

    class RedisLockAnnotationHandler implements InvocationHandler {
        private Object o;

        RedisLockAnnotationHandler(Object o) {
            this.o = o;
        }

        @Override
        public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
            if (!method.isAnnotationPresent(RedisSynchronized.class)) {
                return method.invoke(o, args);
            }
            RedisSynchronized annotation = method.getAnnotation(RedisSynchronized.class);
            String key = annotation.value();
            if ("".equals(key)) {
                key = method.toGenericString();
            }
            String id = annotation.id();
            long expireMilis = annotation.expireMilis();
            for (int i = 0; i < 400; i++) {
                boolean locked = redisLockTool.lock(key, id, expireMilis, false);// false避免服务器崩溃造成死锁
                if (locked) {
                    try {
                        return method.invoke(o, args);
                    } finally {
                        redisLockTool.unlock(key, id);
                    }
                }
                Thread.sleep(300);
            }
            throw new RuntimeException("lock fail, key=" + key);
        }

    }
}

测试代码:

@RequestMapping("/annotationTest")
    @ResponseBody
    @RedisSynchronized("123")
    public String annotationTest() throws Exception {
            System.out.println("do something");
            Thread.sleep(30000);
        return "annotationTest over";
    }
    @RequestMapping("/annotationTest2")
    @ResponseBody
    @RedisSynchronized("123")
    public String annotationTest2() throws Exception {
        System.out.println("do something");
        Thread.sleep(30000);
        return "annotationTest over";
    }

就个人而言, 注解没有直接使用RedisLockTool灵活

参考文献

1.https://zhuanlan.zhihu.com/p/48156279 (有更完善的注解实现)
2.https://wudashan.cn/2017/10/23/Redis-Distributed-Lock-Implement/

神奇的小尾巴:
本人邮箱:zhouyouchn@126.com zhoooooouyou@gmail.com
zhouyou@whut.edu.cn 欢迎交流,共同进步。
欢迎转载,转载请注明本网址。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值