当前位置 主页 > 网站技术 > 代码类 >

    SpringBoot集成Redisson实现分布式锁的方法示例

    栏目:代码类 时间:2019-10-13 18:05

    上篇 《SpringBoot 集成 redis 分布式锁优化》对死锁的问题进行了优化,今天介绍的是 redis 官方推荐使用的 Redisson ,Redisson 架设在 redis 基础上的 Java 驻内存数据网格(In-Memory Data Grid),基于NIO的 Netty 框架上,利用了 redis 键值数据库。功能非常强大,解决了很多分布式架构中的问题。

    Github的wiki地址: https://github.com/redisson/redisson/wiki

    官方文档: https://github.com/redisson/redisson/wiki/目录

    项目代码结构图:

     

    导入依赖

    <dependency>
    	<groupId>org.redisson</groupId>
    	<artifactId>redisson</artifactId>
    	<version>3.8.0</version>
    </dependency>

    属性配置

    application.properites 资源文件中添加单机&哨兵相关配置

    server.port=3000
    
    # redisson lock 单机模式
    redisson.address=redis://127.0.0.1:6379
    redisson.password=
    
    #哨兵模式
    #redisson.master-name= master
    #redisson.password=
    #redisson.sentinel-addresses=10.47.91.83:26379,10.47.91.83:26380,10.47.91.83:26381

    注意:

    这里如果不加 redis:// 前缀会报 URI 构建错误

    Caused by: java.net.URISyntaxException: Illegal character in scheme name at index 0

    更多的配置信息可以去官网查看

    定义Lock的接口定义类

    package com.tuhu.thirdsample.service;
    
    import org.redisson.api.RLock;
    import java.util.concurrent.TimeUnit;
    /**
     * @author chendesheng
     * @create 2019/10/12 10:48
     */
    public interface DistributedLocker {
    
     RLock lock(String lockKey);
    
     RLock lock(String lockKey, int timeout);
    
     RLock lock(String lockKey, TimeUnit unit, int timeout);
    
     boolean tryLock(String lockKey, TimeUnit unit, int waitTime, int leaseTime);
    
     void unlock(String lockKey);
    
     void unlock(RLock lock);
    }

    Lock接口实现类

    package com.tuhu.thirdsample.service;
    
    import org.redisson.api.RLock;
    import org.redisson.api.RedissonClient;
    
    import java.util.concurrent.TimeUnit;
    
    /**
     * @author chendesheng
     * @create 2019/10/12 10:49
     */
    public class RedissonDistributedLocker implements DistributedLocker{
    
     private RedissonClient redissonClient;
    
     @Override
     public RLock lock(String lockKey) {
      RLock lock = redissonClient.getLock(lockKey);
      lock.lock();
      return lock;
     }
    
     @Override
     public RLock lock(String lockKey, int leaseTime) {
      RLock lock = redissonClient.getLock(lockKey);
      lock.lock(leaseTime, TimeUnit.SECONDS);
      return lock;
     }
    
     @Override
     public RLock lock(String lockKey, TimeUnit unit ,int timeout) {
      RLock lock = redissonClient.getLock(lockKey);
      lock.lock(timeout, unit);
      return lock;
     }
    
     @Override
     public boolean tryLock(String lockKey, TimeUnit unit, int waitTime, int leaseTime) {
      RLock lock = redissonClient.getLock(lockKey);
      try {
       return lock.tryLock(waitTime, leaseTime, unit);
      } catch (InterruptedException e) {
       return false;
      }
     }
    
     @Override
     public void unlock(String lockKey) {
      RLock lock = redissonClient.getLock(lockKey);
      lock.unlock();
     }
    
     @Override
     public void unlock(RLock lock) {
      lock.unlock();
     }
    
     public void setRedissonClient(RedissonClient redissonClient) {
      this.redissonClient = redissonClient;
     }
    }