【Redisson】分布式锁源码分析如何实现多个应用实例互斥

入口org.redisson.api.RedissonClient

@Resource
private RedissonClient redissonClient;
...

RLock rLock = redissonClient.getLock(lockName);

lockName就是保存到Redis里面的key

进入org.redisson.Redisson.getLock

    @Override
    public RLock getLock(String name) {
        return new RedissonLock(commandExecutor, name);
    }

进入org.redisson.RedissonLock

直接进行构建方法里面的super(commandExecutor, name);

    public RedissonBaseLock(CommandAsyncExecutor commandExecutor, String name) {
        super(commandExecutor, name);
        this.id = getServiceManager().getId();
        this.internalLockLeaseTime = getServiceManager().getCfg().getLockWatchdogTimeout();
        this.entryName = id + ":" + name;
    }
  • org.redisson.connection.ServiceManager: private final String id = UUID.randomUUID().toString();
  • 这个id就是UUID: this.id = getServiceManager().getId();
  • 这个entryName通过UUID可以区分是哪个应用实例
  • entryName+threadId可以区分哪个应用实例的哪个进程持有锁

rLock.tryLock(60, TimeUnit.SECONDS);

尝试获取锁

进入org.redisson.RedissonLock

    <T> RFuture<T> tryLockInnerAsync(long waitTime, long leaseTime, TimeUnit unit, long threadId, RedisStrictCommand<T> command) {
        return commandExecutor.syncedEval(getRawName(), LongCodec.INSTANCE, command,
                "if ((redis.call('exists', KEYS[1]) == 0) " +
                            "or (redis.call('hexists', KEYS[1], ARGV[2]) == 1)) then " +
                        "redis.call('hincrby', KEYS[1], ARGV[2], 1); " +
                        "redis.call('pexpire', KEYS[1], ARGV[1]); " +
                        "return nil; " +
                    "end; " +
                    "return redis.call('pttl', KEYS[1]);",
                Collections.singletonList(getRawName()), unit.toMillis(leaseTime), getLockName(threadId));
    }
  • Collections.singletonList(getRawName()),这个参数就是Redis的key
  • unit.toMillis(leaseTime),这个参数是Lua脚本的第1个参数,即ARGV[1]
  • getLockName(threadId),这个参数是Lua脚本的第2个参数,即ARGV[2]
  • 这段Lua脚本的含义解析
  • (redis.call('exists', KEYS[1]) == 0),锁不存在
  • or (redis.call('hexists', KEYS[1], ARGV[2]) == 1)),锁存在,但是当前实例线程拥有该锁
  • 以上两种情况,均代表加锁成功,则返回null
  • 否则返回这个锁对应的TTL时间
  • 所以外层调用的地方是根据返回的ttl是否为null来判断加锁是否成功
        Long ttl = tryAcquire(waitTime, leaseTime, unit, threadId);
        // lock acquired
        if (ttl == null) {
            return true;
        }

进入Lua脚本的第2个参数getLockName

    protected String getLockName(long threadId) {
        return id + ":" + threadId;
    }

这个id就是上面提到的UUID,结合线程ID,可以判断是哪个应用实例的哪个进程持有锁

猜你喜欢

转载自blog.csdn.net/friendlytkyj/article/details/131341990