Redis实现分布式锁的几种方案

2019-06-21  本文已影响0人  飘颜

1.1方案一
利用setnx和expire命令实现加锁。当一个线程执行setnx返回1,说明key不存在,该线程获得锁;当一个线程执行setnx返回0,说明key已经存在,则获取锁失败。expire就是给锁加一个过期时间。伪代码如下:

if(setnx(key,value)==1){
     expire(key,expireTime)
     try{
        //业务处理
     }finally{
       del(key)
     }
}
    该方案有一个致命问题,由于setnx和expire是两条Redis命令,不具备原子性,如果一个线程在执行完setnx()之后突然崩溃,导致锁没有设置过期时间,那么将会发生死锁。

.2方案二
利用setnx命令加锁,其中key是锁,value是锁的过期时间,1.通过setnx()方法尝试加锁,如果当前锁不存在,返回加锁成功。2. 如果锁已经存在则获取锁的过期时间,和当前时间比较,如果锁已经过期,则设置新的过期时间,返回加锁成功。伪代码如下:

 long expires = System.currentTimeMillis() + expireTime;
    String expiresStr = String.valueOf(expires);

    // 如果当前锁不存在,返回加锁成功
    if (setnx(key, expiresStr) == 1) {
        return true;
    }

    // 如果锁存在,获取锁的过期时间
    String currentValueStr = get(key);
    if (currentValueStr != null && Long.parseLong(currentValueStr) < System.currentTimeMillis()) {
        // 锁已过期,获取上一个锁的过期时间,并设置现在锁的过期时间
        String oldValueStr = jedis.getSet(lockKey, expiresStr);
        if (oldValueStr != null && oldValueStr.equals(currentValueStr)) {
            // 考虑多线程并发的情况,只有一个线程的设置值和当前值相同,它才有权利加锁
            return true;
        }
    }
        
    // 其他情况,一律返回加锁失败
    return false;

下面附上源码:

public class RedisLock {

    private final static Logger LOGGER = LoggerFactory.getLogger(RedisLock.class);

    private CacheService cacheService;

    /**
     * 加锁
     *
     * @param key
     * @param value 当前时间+超时时间
     * @return
     */
    public boolean lock(String key, String value) {
        if (cacheService.opsForValue().setIfAbsent(key, value)) {
            return true;
        }
        String currentValue = cacheService.opsForValue().get(key);
        //如果锁过期  解决死锁
        if (StringUtils.isNotBlank(currentValue) && Long.parseLong(currentValue) < System.currentTimeMillis()) {
            //获取上一个锁的时间,锁过期后,GETSET将原来的锁替换成新锁
            String oldValue = cacheService.opsForValue().getAndSet(key, value);
            if (StringUtils.isNotBlank(oldValue) && oldValue.equals(currentValue)) {
                return true;
            }
        }
        return false;
    }

    /**
     * 解锁
     *
     * @param key
     * @param value
     */
    public void unlock(String key, String value) {
        try {
            String currentValue = cacheService.opsForValue().get(key);
            if (StringUtils.isNotBlank(currentValue) && currentValue.equals(value)) {
                cacheService.opsForValue().getOperations().delete(key);
            }
        } catch (Exception e) {
            LOGGER.error("【redis分布式锁】解锁异常, {}", e);
        }
    }

    public void setCacheService(CacheService cacheService) {
        this.cacheService = cacheService;
    }

1.3方案三
Redis2.6.12以上版本为set命令增加了可选参数,伪代码如下:

if(redis.set(key,value,"ex 180","nx")){
     //业务处理
     do something;
     //释放锁
     redis.delete(key);
}
    我们对锁设置了过期时间,即使锁的持有者后续发生崩溃而没有解锁,锁也会因为到了过期时间而自动解锁(即key被删除),不会发生死锁。
上一篇下一篇

猜你喜欢

热点阅读