Redis分布式锁最重要的就是要做到上锁、解锁都是原子性的。这样才能保证高并发下不会出现问题。请看实例代码:
RedisLockRequestId.java
public class RedisLockRequestId {
private String requestId;
public String getRequestId() {
return requestId;
}
public void setRequestId(String requestId) {
this.requestId = requestId;
}
}
/**
* 如果获取不到会重复去尝试获取锁
* 尝试获取分布式锁 --- 保证执行的原子性
* @param lockKey 锁
* @param redisLockRequestId 请求标识
* @param seconds 枷锁多长时间
* @return 是否获取成功
*/
public boolean lock(String lockKey, RedisLockRequestId redisLockRequestId, long seconds) {
String requestId = UUID.randomUUID().toString()+"-"+System.currentTimeMillis();
redisLockRequestId.setRequestId(requestId);
long startNano = System.nanoTime();
//将秒转换成纳秒
Long secondsNanoTime = 1000L*1000000L*seconds;
ThreadLocalRandom r = ThreadLocalRandom.current();
try(ShardedJedis jedis = getShardedJedis()){
//如果获取失败,重试n次锁时间内
while ((System.nanoTime() - startNano) < secondsNanoTime) {
String result = jedis.set(lockKey, requestId, "NX", "EX", seconds);
if (null!=result && "OK".equals(result)) {
return true;
}
// 短暂休眠,nano避免出现活锁
Thread.sleep(3, r.nextInt(500));
}
logger.info("lock error. key {} requestId {}", lockKey ,requestId);
return false;
}catch (Exception e){
logger.error("lock error. key="+lockKey+" requestId="+requestId, e);
return false;
}
}
/**
* 瞬时锁,如果获取不到锁 立刻返回获取锁失败
* @param lockKey
* @param redisLockRequestId
* @param seconds
* @return
*/
public boolean lockNoWait(String lockKey, RedisLockRequestId redisLockRequestId, long seconds) {
String requestId = UUID.randomUUID().toString()+"-"+System.currentTimeMillis();
redisLockRequestId.setRequestId(requestId);
try(ShardedJedis jedis = getShardedJedis()){
//如果获取失败,重试n次锁时间内
String result = jedis.set(lockKey, requestId, "NX", "EX", seconds);
if (null!=result && "OK".equals(result)) {
return true;
}
logger.info("lock false. key {} requestId {}", lockKey ,requestId);
return false;
}catch (Exception e){
logger.error("lock error. key="+lockKey+" requestId="+requestId, e);
return false;
}
}
/**
* 释放分布式锁 --- 使用Lua脚本 保证命令的原子性
* @param lockKey 锁
* @param redisLockRequestId 请求标识
* @return 是否释放成功
*/
public boolean unlock(String lockKey, RedisLockRequestId redisLockRequestId) {
String requestId = redisLockRequestId.getRequestId();
try(ShardedJedis shardedJedis = getShardedJedis()){
Jedis jedis = shardedJedis.getShard(lockKey);
String script = "if redis.call('get', KEYS[1]) == ARGV[1] then return redis.call('del', KEYS[1]) else return 0 end";
Object result = jedis.eval(script, Collections.singletonList(lockKey), Collections.singletonList(requestId));
if ("1".equals(String.valueOf(result))) {
return true;
}
logger.info("unlock false. key={} requestId={} result={}", lockKey ,requestId,result);
return false;
}catch (Exception e){
logger.error("unlock error. key "+lockKey+" requestId "+requestId,e);
return false;
}
}
测试代码–仅供参考:
private ShardedJedis getShardedJedis() {
return testpool.getResource();
}
static ShardedJedisPool testpool;
static{
JedisPoolConfig config =new JedisPoolConfig();//Jedis池配置
config.setMaxTotal(100);
config.setMaxIdle(100);
config.setMinIdle(100);
config.setTestOnBorrow(false);
config.setTestOnReturn(false);
String hostA = "127.0.0.1";
int portA = 6379;
String hostB = "127.0.0.1";
int portB = 6379;
List<JedisShardInfo> jdsInfoList =new ArrayList<JedisShardInfo>(2);
JedisShardInfo infoA = new JedisShardInfo(hostA, portA);
//infoA.setPassword("admin");
JedisShardInfo infoB = new JedisShardInfo(hostB, portB);
//infoB.setPassword("admin");
jdsInfoList.add(infoA);
jdsInfoList.add(infoB);
testpool =new ShardedJedisPool(config, jdsInfoList);
}
/**
* RedisLockNewService 压测试
* 使用 CountDownLatch 模仿并发100
*
*
* @param args
*/
private static CountDownLatch cl = new CountDownLatch(100);
public static void main(String[] args) throws InterruptedException {
for (int i = 0; i<100; i++){
int finalI = i;
new Thread(new Runnable() {
@SneakyThrows
@Override
public void run() {
// cl.countDown();
cl.await();
RedisLockNewService redisLockNewService = new RedisLockNewService();
RedisLockRequestId requestId = new RedisLockRequestId();
String key = "lock-key"+ finalI;
long expired = 3;
try{
if(redisLockNewService.lock(key,requestId,expired)){
//@TODO 枷锁以后的业务
System.out.println("获取锁成功"+key +" requestId="+requestId.getRequestId());
Thread.sleep(2000);
}else{
System.out.println("加锁失败");
}
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
System.out.println(redisLockNewService.unlock(key,requestId) +" unlock = "+key +" requestId="+requestId.getRequestId());
}
}
}).start();
cl.countDown();
}
}