public class DistributedRedisLock implements Lock {
private final JedisPool jedisPool;
private final String lockKey;
private final String randomString;
private final long millisecondsToExpire;
public DistributedRedisLock(JedisPool jedisPool, String key, TimeUnit unit, long duration) {
this.jedisPool = jedisPool; // 使用 JedisPool 获取线程安全的 Redis 连接
this.lockKey = key;
this.randomString = UUID.randomUUID().toString();
this.millisecondsToExpire = unit.toMillis(duration);
}
@Override
public void lock() {
try (Jedis jedis = jedisPool.getResource()) {
while (true) {
String result = jedis.set(lockKey, randomString, SetParams.setParams().px(millisecondsToExpire).nx());
if (result != null) {
break;
} else {
try {
TimeUnit.MILLISECONDS.sleep(50);
} catch (InterruptedException e) {
// ignore
}
}
}
}
}
@Override
public void lockInterruptibly() throws InterruptedException {
try (Jedis jedis = jedisPool.getResource()) {
while (!Thread.currentThread().isInterrupted()) {
String result = jedis.set(lockKey, randomString, SetParams.setParams().px(millisecondsToExpire).nx());
if (result != null) {
break;
} else {
TimeUnit.MILLISECONDS.sleep(50);
}
}
}
}
@Override
public boolean tryLock() {
try (Jedis jedis = jedisPool.getResource()) {
String result = jedis.set(lockKey, randomString, SetParams.setParams().px(millisecondsToExpire).nx());
return result != null;
}
}
@Override
public boolean tryLock(long time, @Nonnull TimeUnit unit) throws InterruptedException {
try (Jedis jedis = jedisPool.getResource()) {
long startTime = System.nanoTime();
long expireTime = startTime + unit.toNanos(time);
while (!Thread.currentThread().isInterrupted()) {
String result = jedis.set(lockKey, randomString, SetParams.setParams().px(millisecondsToExpire).nx());
if (result != null) {
return true;
} else if (System.nanoTime() >= expireTime) {
return false;
} else {
TimeUnit.MILLISECONDS.sleep(50);
}
}
return false;
}
}
@Override
public void unlock() {
try (Jedis jedis = jedisPool.getResource()) {
final String script = "if redis.call(\"get\",KEYS[1]) == ARGV[1] then return redis.call(\"del\",KEYS[1]) else return 0 end";
jedis.eval(script, Collections.singletonList(lockKey), Collections.singletonList(randomString));
}
}
@Override
public Condition newCondition() {
throw new UnsupportedOperationException();
}
}
在使用单实例的 Redis 分布式锁的情况下,如果唯一的 Redis 实例挂了,那么分布式锁功能则完全失效了。为此 Redis 社区提出了一种多实例 Redis 的分布式锁算法——Redlock。
Redlock 的大致思路是假设存在 2n+1 台 Redis 实例,当应用获取锁时,需在限定的超时时间内向这些 Redis 实例批量地设置相同的 key 和 value,如果某台 Redis 实例在超时时间内没有设值成功,则应该尽快跳过这台实例。最终,如果在 >n 台实例上设值成功,则表示获取锁成功,否则表示获取锁失败,并且需要删除所有 Redis 实例上的 key。当应用释放锁时,只是简单地向所有 Redis 实例发送释放锁的命令。