Redis的分布式锁

 

1.设置超时时间,即使获取到锁后,客户端挂掉了,一定时间后锁释放。不会出现死锁。

2.每个锁对象都有一个自己的uuid作为redis 的value。当释放锁的时候,先去判断当前key对应的value是否还是本是当前对象的value.(但是本实例中没有这个场景)

3.防止操作阻塞超过了超时,本实例中会不断的重置超时时间。如果锁释放,重置超时会返回0,则中断循环。但是不断重置超时可能会造成阻塞,可以设置重置超时次数。

4.为了保证原子性,使用redis的lua脚本

1.RedisTool

public class RedisTool {
    private static Jedis jedis = getJedis();
    private static Jedis getJedis() {
        JedisPoolConfig config = new JedisPoolConfig();
        /*配置连接池最大空闲数*/
        config.setMaxIdle(50);

        /*配置连接池最大连接数*/
        config.setMaxTotal(100);

        /*最大等待毫秒数*/
        config.setMaxWaitMillis(20000);

        /*使用配置创建连接池*/
        JedisPool jedisPool = new JedisPool(config, "localhost");

        /*从连接池中获取单个链接*/
        Jedis jedis = jedisPool.getResource();
        return jedis;
    }

    public static Jedis getJedisInstance() {
        return jedis;
    }

}

2.RedisLock 

public class RedisLock {
    String lockKey;
    String value = UUID.randomUUID().toString();
    Jedis jedis;
    int timeOut;
    boolean isOpen = true;

    public  RedisLock(String lockKey, Jedis jedis, int timeOut) {
        this.jedis = jedis;
        this.lockKey = lockKey;
        this.timeOut = timeOut;
    }

    public void lock()  {
      while (true) {
          if (tryLock()) {
              break;
          }
      }

    }

    public void lockInterrupt() throws InterruptedException {
        while (true) {
            boolean interrupted = Thread.currentThread().isInterrupted();
            if (interrupted) {
                throw new InterruptedException("中断");
            }
            if (tryLock()) {
                break;
            }
        }
    }

    public boolean tryLock() {
        String result = jedis.set(lockKey,value,"NX","EX",timeOut);
        if ("OK".equals(result)) {
            System.out.println("线程id:"+Thread.currentThread().getId() + "加锁成功!时间:"+System.currentTimeMillis());
            if(isOpen) {
                new Thread(new growthTimeout()).start();
            }
            return true;
        }else {
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                return  false;
            }
        }
        return false;
    }

    private class growthTimeout implements Runnable {

        @Override
        public void run() {
            while (true) {
                String ctl = "if redis.call('get',KEYS[1]) == ARGV[1] then " +
                        "return redis.call('expire',KEYS[1],ARGV[2]) " +
                        "else " +
                        "return 0 end";
                Object re = jedis.eval(ctl,1,lockKey,value,"3000");
                if("0".equals(re.toString())) {
                    break;
                }else {
                    try {
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        break;
                    }
                }
            }
        }
    }

    public void unLock() {
        String script = "if redis.call('get',KEYS[1]) == ARGV[1] then " +
                "return redis.call('del',KEYS[1]) " +
                "else " +
                "return 0 end";
        jedis.eval(script,1,lockKey,value);

        System.out.println("线程id:"+Thread.currentThread().getId() + "解锁成功!时间"+System.currentTimeMillis());
    }

}

3.测试类 

public class MyTest {
    public static void main(String[] args) throws InterruptedException {
        Jedis jedis = RedisTool.getJedisInstance();

        RedisLock lock = new RedisLock("lcc",jedis,1000);
        new Thread(() ->{
            lock.lock();

            try {
                System.out.println("lock1");
                Thread.sleep(5000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }

            lock.unLock();
        }).start();
        Thread.sleep(500);


        /*调用克中断lock方法*/
        Thread tt = new Thread(() ->{


            try {
                lock.lockInterrupt();
                System.out.println("lock2");

            } catch (InterruptedException e) {
                e.printStackTrace();
            }

            lock.unLock();
        });
        tt.start();
        tt.interrupt();

    }
}

GitHub:https://github.com/liucc0413/Redis/tree/master/src/main/java/com/redis/demo/DistributedJedisLock

参考:https://www.cnblogs.com/fixzd/p/9479970.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值