针对项目中使用的分布式锁进行简单的示例配置以及源码解析,并列举源码中使用到的一些基础知识点,可是没有对redisson中使用到的netty知识进行解析。node
本篇主要是对如下几个方面进行了探索redis
Maven配置服务器
1并发 2异步 3分布式 4ide 5源码分析 6post 7测试 8 9 10 |
<dependency> <groupId>org.redisson</groupId> <artifactId>redisson</artifactId> <version>2.2.12</version> </dependency> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-annotations</artifactId> <version>2.6.0</version> </dependency> |
RedissonLock简单示例
redission支持4种链接redis方式,分别为单机、主从、Sentinel、Cluster 集群,项目中使用的链接方式是Sentinel。
redis服务器不在本地的同窗请注意权限问题。
Sentinel配置
1 2 3 |
Config config = new Config(); config.useSentinelServers().addSentinelAddress("127.0.0.1:6479", "127.0.0.1:6489").setMasterName("master").setPassword("password").setDatabase(0); RedissonClient redisson = Redisson.create(config); |
简单使用
1 2 3 4 5 6 7 8 9 10 |
RLock lock = redisson.getLock("test_lock"); try{ boolean isLock=lock.tryLock(); if(isLock){ doBusiness(); } }catch(exception e){ }finally{ lock.unlock(); } |
源码中使用到的Redis命令
分布式锁主要须要如下redis命令,这里列举一下。在源码分析部分能够继续参照命令的操做含义。
源码中使用到的lua脚本语义
Redisson源码中,执行redis命令的是lua脚本,其中主要用到以下几个概念。
须要注意的是,在redis执行lua脚本时,至关于一个redis级别的锁,不能执行其余操做,相似于原子操做,也是redisson实现的一个关键点。
另外,若是lua脚本执行过程当中出现了异常或者redis服务器直接宕掉了,执行redis的根据日志回复的命令,会将脚本中已经执行的命令在日志中删除。
源码分析
RLOCK结构
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
public interface RLock extends Lock, RExpirable { void lockInterruptibly(long leaseTime, TimeUnit unit) throws InterruptedException; boolean tryLock(long waitTime, long leaseTime, TimeUnit unit) throws InterruptedException; void lock(long leaseTime, TimeUnit unit); void forceUnlock(); boolean isLocked(); boolean isHeldByCurrentThread(); int getHoldCount(); Future<Void> unlockAsync(); Future<Boolean> tryLockAsync(); Future<Void> lockAsync(); Future<Void> lockAsync(long leaseTime, TimeUnit unit); Future<Boolean> tryLockAsync(long waitTime, TimeUnit unit); Future<Boolean> tryLockAsync(long waitTime, long leaseTime, TimeUnit unit); } |
该接口主要继承了Lock接口, 并扩展了部分方法, 好比:boolean tryLock(long waitTime, long leaseTime, TimeUnit unit)新加入的leaseTime主要是用来设置锁的过时时间, 若是超过leaseTime尚未解锁的话, redis就强制解锁. leaseTime的默认时间是30s
RedissonLock获取锁 tryLock源码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
Future<Long> tryLockInnerAsync(long leaseTime, TimeUnit unit, long threadId) { internalLockLeaseTime = unit.toMillis(leaseTime); return commandExecutor.evalWriteAsync(getName(), LongCodec.INSTANCE, RedisCommands.EVAL_LONG, "if (redis.call('exists', KEYS[1]) == 0) then " + "redis.call('hset', KEYS[1], ARGV[2], 1); " + "redis.call('pexpire', KEYS[1], ARGV[1]); " + "return nil; " + "end; " + "if (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.<Object>singletonList(getName()), internalLockLeaseTime, getLockName(threadId)); } |
其中
KEYS[1] 表示的是 getName() ,表明的是锁名 test_lock
ARGV[1] 表示的是 internalLockLeaseTime 默认值是30s
ARGV[2] 表示的是 getLockName(threadId) 表明的是 id:threadId 用锁对象id+线程id, 表示当前访问线程,用于区分不一样服务器上的线程.
逐句分析:
1 2 3 4 5 |
if (redis.call('exists', KEYS[1]) == 0) then redis.call('hset', KEYS[1], ARGV[2], 1); redis.call('pexpire', KEYS[1], ARGV[1]); return nil; end; |
1 2 3 4 5 |
if (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; |
1 |
return redis.call('pttl', KEYS[1]); |
RedissonLock解锁 unlock源码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
public void unlock() { Boolean opStatus = commandExecutor.evalWrite(getName(), LongCodec.INSTANCE, RedisCommands.EVAL_BOOLEAN, "if (redis.call('exists', KEYS[1]) == 0) then " + "redis.call('publish', KEYS[2], ARGV[1]); " + "return 1; " + "end;" + "if (redis.call('hexists', KEYS[1], ARGV[3]) == 0) then " + "return nil;" + "end; " + "local counter = redis.call('hincrby', KEYS[1], ARGV[3], -1); " + "if (counter > 0) then " + "redis.call('pexpire', KEYS[1], ARGV[2]); " + "return 0; " + "else " + "redis.call('del', KEYS[1]); " + "redis.call('publish', KEYS[2], ARGV[1]); " + "return 1; "+ "end; " + "return nil;", Arrays.<Object>asList(getName(), getChannelName()), LockPubSub.unlockMessage, internalLockLeaseTime, getLockName(Thread.currentThread().getId())); if (opStatus == null) { throw new IllegalMonitorStateException("attempt to unlock lock, not locked by current thread by node id: " + id + " thread-id: " + Thread.currentThread().getId()); } if (opStatus) { cancelExpirationRenewal(); } } |
其中
KEYS[1] 表是的是getName() 表明锁名test_lock
KEYS[2] 表示getChanelName() 表示的是发布订阅过程当中使用的Chanel
ARGV[1] 表示的是LockPubSub.unLockMessage 是解锁消息,实际表明的是数字 0,表明解锁消息
ARGV[2] 表示的是internalLockLeaseTime 默认的有效时间 30s
ARGV[3] 表示的是getLockName(thread.currentThread().getId()),是当前锁id+线程id
语义分析:
1 2 3 4 |
if (redis.call('exists', KEYS[1]) == 0) then redis.call('publish', KEYS[2], ARGV[1]); return 1; end; |
1 2 3 |
if (redis.call('hexists', KEYS[1], ARGV[3]) == 0) then return nil; end; |
1 2 3 4 5 6 7 8 9 |
local counter = redis.call('hincrby', KEYS[1], ARGV[3], -1); if (counter > 0) then redis.call('pexpire', KEYS[1], ARGV[2]); return 0; else redis.call('del', KEYS[1]); redis.call('publish', KEYS[2], ARGV[1]); return 1; end; |
1 |
return nil; |
1 2 3 4 |
if (opStatus == null) { throw new IllegalMonitorStateException("attempt to unlock lock, not locked by current thread by node id: " + id + " thread-id: " + Thread.currentThread().getId()); } |
RedissonLock强制解锁源码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
public void forceUnlock() { get(forceUnlockAsync()); } Future<Boolean> forceUnlockAsync() { cancelExpirationRenewal(); return commandExecutor.evalWriteAsync(getName(), LongCodec.INSTANCE, RedisCommands.EVAL_BOOLEAN, "if (redis.call('del', KEYS[1]) == 1) then " + "redis.call('publish', KEYS[2], ARGV[1]); " + "return 1 " + "else " + "return 0 " + "end", Arrays.<Object>asList(getName(), getChannelName()), LockPubSub.unlockMessage); } |
总结
这里只是简单的一个redisson分布式锁的测试用例,并分析了执行lua脚本这部分,若是要继续分析执行结束以后的操做,须要进行netty源码分析 ,redisson使用了netty完成异步和同步的处理。