原文:https://blog.csdn.net/sunct/article/details/80178197

定义分布式锁接口

package com.redis.lock.redisson_springboot;

import java.util.concurrent.TimeUnit;

public interface DistributedLocker {

    void lock(String lockKey);

    void unlock(String lockKey);

    void lock(String lockKey, int timeout);

    void lock(String lockKey, TimeUnit unit ,int timeout);
}

基于Redisson的分布式锁实现

package com.redis.lock.redisson_springboot;

import org.redisson.api.RLock;
import org.redisson.api.RedissonClient; import java.util.concurrent.TimeUnit; public class RedissonDistributedLocker implements DistributedLocker { private RedissonClient redissonClient; @Override
public void lock(String lockKey) {
RLock lock = redissonClient.getLock(lockKey);
lock.lock();
} @Override
public void unlock(String lockKey) {
RLock lock = redissonClient.getLock(lockKey);
lock.unlock();
} @Override
public void lock(String lockKey, int leaseTime) {
RLock lock = redissonClient.getLock(lockKey);
lock.lock(leaseTime, TimeUnit.SECONDS);
} @Override
public void lock(String lockKey, TimeUnit unit ,int timeout) {
RLock lock = redissonClient.getLock(lockKey);
lock.lock(timeout, unit);
} public void setRedissonClient(RedissonClient redissonClient) {
this.redissonClient = redissonClient;
}
}

redis分布式锁帮助类

package com.redis.lock.redisson_springboot;

import java.util.concurrent.TimeUnit;

/**
* redis分布式锁帮助类
*
*/
public class RedissLockUtil {
private static DistributedLocker redissLock; public static void setLocker(DistributedLocker locker) {
redissLock = locker;
} public static void lock(String lockKey) {
redissLock.lock(lockKey);
} public static void unlock(String lockKey) {
redissLock.unlock(lockKey);
} /**
* 带超时的锁
* @param lockKey
* @param timeout 超时时间 单位:秒
*/
public static void lock(String lockKey, int timeout) {
redissLock.lock(lockKey, timeout);
} /**
* 带超时的锁
* @param lockKey
* @param unit 时间单位
* @param timeout 超时时间
*/
public static void lock(String lockKey, TimeUnit unit ,int timeout) {
redissLock.lock(lockKey, unit, timeout);
}
}

SpringBoot的自动配置类

package com.redis.lock.redisson_springboot;

import org.apache.commons.lang3.StringUtils;
import org.redisson.Redisson;
import org.redisson.api.RedissonClient;
import org.redisson.config.Config;
import org.redisson.config.SentinelServersConfig;
import org.redisson.config.SingleServerConfig;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; @Configuration
@ConditionalOnClass(Config.class)
@EnableConfigurationProperties(RedissonProperties.class)
public class RedissonAutoConfiguration { @Autowired
private RedissonProperties redssionProperties; /**
* 哨兵模式自动装配
* @return
*/
@Bean
@ConditionalOnProperty(name="redisson.master-name")
RedissonClient redissonSentinel() {
Config config = new Config();
SentinelServersConfig serverConfig = config.useSentinelServers().addSentinelAddress(redssionProperties.getSentinelAddresses())
.setMasterName(redssionProperties.getMasterName())
.setTimeout(redssionProperties.getTimeout())
.setMasterConnectionPoolSize(redssionProperties.getMasterConnectionPoolSize())
.setSlaveConnectionPoolSize(redssionProperties.getSlaveConnectionPoolSize()); if(StringUtils.isNotBlank(redssionProperties.getPassword())) {
serverConfig.setPassword(redssionProperties.getPassword());
}
return Redisson.create(config);
} /**
* 单机模式自动装配
* @return
*/
@Bean
@ConditionalOnProperty(name="redisson.address")
RedissonClient redissonSingle() {
Config config = new Config();
SingleServerConfig serverConfig = config.useSingleServer()
.setAddress(redssionProperties.getAddress())
.setTimeout(redssionProperties.getTimeout())
.setConnectionPoolSize(redssionProperties.getConnectionPoolSize())
.setConnectionMinimumIdleSize(redssionProperties.getConnectionMinimumIdleSize()); if(StringUtils.isNotBlank(redssionProperties.getPassword())) {
serverConfig.setPassword(redssionProperties.getPassword());
} return Redisson.create(config);
} /**
* 装配locker类,并将实例注入到RedissLockUtil中
* @return
*/
@Bean
DistributedLocker distributedLocker(RedissonClient redissonSingle) {
RedissonDistributedLocker locker = new RedissonDistributedLocker();
locker.setRedissonClient(redissonSingle);
RedissLockUtil.setLocker(locker);
return locker;
} }

Redisson配置类

package com.redis.lock.redisson_springboot;

import org.springframework.boot.context.properties.ConfigurationProperties;

@ConfigurationProperties(prefix = "redisson")
public class RedissonProperties { private int timeout = 3000; private String address; private String password; private int connectionPoolSize = 64; private int connectionMinimumIdleSize=10; private int slaveConnectionPoolSize = 250; private int masterConnectionPoolSize = 250; private String[] sentinelAddresses; private String masterName; public int getTimeout() {
return timeout;
} public void setTimeout(int timeout) {
this.timeout = timeout;
} public int getSlaveConnectionPoolSize() {
return slaveConnectionPoolSize;
} public void setSlaveConnectionPoolSize(int slaveConnectionPoolSize) {
this.slaveConnectionPoolSize = slaveConnectionPoolSize;
} public int getMasterConnectionPoolSize() {
return masterConnectionPoolSize;
} public void setMasterConnectionPoolSize(int masterConnectionPoolSize) {
this.masterConnectionPoolSize = masterConnectionPoolSize;
} public String[] getSentinelAddresses() {
return sentinelAddresses;
} public void setSentinelAddresses(String sentinelAddresses) {
this.sentinelAddresses = sentinelAddresses.split(",");
} public String getMasterName() {
return masterName;
} public void setMasterName(String masterName) {
this.masterName = masterName;
} public String getPassword() {
return password;
} public void setPassword(String password) {
this.password = password;
} public String getAddress() {
return address;
} public void setAddress(String address) {
this.address = address;
} public int getConnectionPoolSize() {
return connectionPoolSize;
} public void setConnectionPoolSize(int connectionPoolSize) {
this.connectionPoolSize = connectionPoolSize;
} public int getConnectionMinimumIdleSize() {
return connectionMinimumIdleSize;
} public void setConnectionMinimumIdleSize(int connectionMinimumIdleSize) {
this.connectionMinimumIdleSize = connectionMinimumIdleSize;
}
}

属性文件配置项application.properties

# redisson lock
#1、单机模式
redisson.address=redis://10.18.75.115:6379
#redisson.password= #2、哨兵模式
redisson.master-name=mymaster
#redisson.password=xxxx
redisson.sentinel-addresses=10.47.91.83:26379,10.47.91.83:26380,10.47.91.83:26381

定义分布式锁接口

  1.  
    package com.redis.lock.redisson_springboot;
  2.  
     
  3.  
    import java.util.concurrent.TimeUnit;
  4.  
     
  5.  
    public interface DistributedLocker {
  6.  
     
  7.  
    void lock(String lockKey);
  8.  
     
  9.  
    void unlock(String lockKey);
  10.  
     
  11.  
    void lock(String lockKey, int timeout);
  12.  
     
  13.  
    void lock(String lockKey, TimeUnit unit ,int timeout);
  14.  
    }

基于Redisson的分布式锁实现

  1.  
    package com.redis.lock.redisson_springboot;
  2.  
     
  3.  
    import org.redisson.api.RLock;
  4.  
    import org.redisson.api.RedissonClient;
  5.  
     
  6.  
    import java.util.concurrent.TimeUnit;
  7.  
     
  8.  
    public class RedissonDistributedLocker implements DistributedLocker {
  9.  
     
  10.  
    private RedissonClient redissonClient;
  11.  
     
  12.  
    @Override
  13.  
    public void lock(String lockKey) {
  14.  
    RLock lock = redissonClient.getLock(lockKey);
  15.  
    lock.lock();
  16.  
    }
  17.  
     
  18.  
    @Override
  19.  
    public void unlock(String lockKey) {
  20.  
    RLock lock = redissonClient.getLock(lockKey);
  21.  
    lock.unlock();
  22.  
    }
  23.  
     
  24.  
    @Override
  25.  
    public void lock(String lockKey, int leaseTime) {
  26.  
    RLock lock = redissonClient.getLock(lockKey);
  27.  
    lock.lock(leaseTime, TimeUnit.SECONDS);
  28.  
    }
  29.  
     
  30.  
    @Override
  31.  
    public void lock(String lockKey, TimeUnit unit ,int timeout) {
  32.  
    RLock lock = redissonClient.getLock(lockKey);
  33.  
    lock.lock(timeout, unit);
  34.  
    }
  35.  
     
  36.  
    public void setRedissonClient(RedissonClient redissonClient) {
  37.  
    this.redissonClient = redissonClient;
  38.  
    }
  39.  
    }

redis分布式锁帮助类

  1.  
    package com.redis.lock.redisson_springboot;
  2.  
     
  3.  
    import java.util.concurrent.TimeUnit;
  4.  
     
  5.  
     
  6.  
    /**
  7.  
    * redis分布式锁帮助类
  8.  
    *
  9.  
    */
  10.  
    public class RedissLockUtil {
  11.  
    private static DistributedLocker redissLock;
  12.  
     
  13.  
    public static void setLocker(DistributedLocker locker) {
  14.  
    redissLock = locker;
  15.  
    }
  16.  
     
  17.  
    public static void lock(String lockKey) {
  18.  
    redissLock.lock(lockKey);
  19.  
    }
  20.  
     
  21.  
    public static void unlock(String lockKey) {
  22.  
    redissLock.unlock(lockKey);
  23.  
    }
  24.  
     
  25.  
    /**
  26.  
    * 带超时的锁
  27.  
    * @param lockKey
  28.  
    * @param timeout 超时时间 单位:秒
  29.  
    */
  30.  
    public static void lock(String lockKey, int timeout) {
  31.  
    redissLock.lock(lockKey, timeout);
  32.  
    }
  33.  
     
  34.  
    /**
  35.  
    * 带超时的锁
  36.  
    * @param lockKey
  37.  
    * @param unit 时间单位
  38.  
    * @param timeout 超时时间
  39.  
    */
  40.  
    public static void lock(String lockKey, TimeUnit unit ,int timeout) {
  41.  
    redissLock.lock(lockKey, unit, timeout);
  42.  
    }
  43.  
    }

SpringBoot的自动配置类

  1.  
    package com.redis.lock.redisson_springboot;
  2.  
     
  3.  
    import org.apache.commons.lang3.StringUtils;
  4.  
    import org.redisson.Redisson;
  5.  
    import org.redisson.api.RedissonClient;
  6.  
    import org.redisson.config.Config;
  7.  
    import org.redisson.config.SentinelServersConfig;
  8.  
    import org.redisson.config.SingleServerConfig;
  9.  
    import org.springframework.beans.factory.annotation.Autowired;
  10.  
    import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
  11.  
    import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
  12.  
    import org.springframework.boot.context.properties.EnableConfigurationProperties;
  13.  
    import org.springframework.context.annotation.Bean;
  14.  
    import org.springframework.context.annotation.Configuration;
  15.  
     
  16.  
     
  17.  
    @Configuration
  18.  
    @ConditionalOnClass(Config.class)
  19.  
    @EnableConfigurationProperties(RedissonProperties.class)
  20.  
    public class RedissonAutoConfiguration {
  21.  
     
  22.  
    @Autowired
  23.  
    private RedissonProperties redssionProperties;
  24.  
     
  25.  
    /**
  26.  
    * 哨兵模式自动装配
  27.  
    * @return
  28.  
    */
  29.  
    @Bean
  30.  
    @ConditionalOnProperty(name="redisson.master-name")
  31.  
    RedissonClient redissonSentinel() {
  32.  
    Config config = new Config();
  33.  
    SentinelServersConfig serverConfig = config.useSentinelServers().addSentinelAddress(redssionProperties.getSentinelAddresses())
  34.  
    .setMasterName(redssionProperties.getMasterName())
  35.  
    .setTimeout(redssionProperties.getTimeout())
  36.  
    .setMasterConnectionPoolSize(redssionProperties.getMasterConnectionPoolSize())
  37.  
    .setSlaveConnectionPoolSize(redssionProperties.getSlaveConnectionPoolSize());
  38.  
     
  39.  
    if(StringUtils.isNotBlank(redssionProperties.getPassword())) {
  40.  
    serverConfig.setPassword(redssionProperties.getPassword());
  41.  
    }
  42.  
    return Redisson.create(config);
  43.  
    }
  44.  
     
  45.  
    /**
  46.  
    * 单机模式自动装配
  47.  
    * @return
  48.  
    */
  49.  
    @Bean
  50.  
    @ConditionalOnProperty(name="redisson.address")
  51.  
    RedissonClient redissonSingle() {
  52.  
    Config config = new Config();
  53.  
    SingleServerConfig serverConfig = config.useSingleServer()
  54.  
    .setAddress(redssionProperties.getAddress())
  55.  
    .setTimeout(redssionProperties.getTimeout())
  56.  
    .setConnectionPoolSize(redssionProperties.getConnectionPoolSize())
  57.  
    .setConnectionMinimumIdleSize(redssionProperties.getConnectionMinimumIdleSize());
  58.  
     
  59.  
    if(StringUtils.isNotBlank(redssionProperties.getPassword())) {
  60.  
    serverConfig.setPassword(redssionProperties.getPassword());
  61.  
    }
  62.  
     
  63.  
    return Redisson.create(config);
  64.  
    }
  65.  
     
  66.  
    /**
  67.  
    * 装配locker类,并将实例注入到RedissLockUtil中
  68.  
    * @return
  69.  
    */
  70.  
    @Bean
  71.  
    DistributedLocker distributedLocker(RedissonClient redissonSingle) {
  72.  
    RedissonDistributedLocker locker = new RedissonDistributedLocker();
  73.  
    locker.setRedissonClient(redissonSingle);
  74.  
    RedissLockUtil.setLocker(locker);
  75.  
    return locker;
  76.  
    }
  77.  
     
  78.  
    }

Redisson配置类

  1.  
    package com.redis.lock.redisson_springboot;
  2.  
     
  3.  
    import org.springframework.boot.context.properties.ConfigurationProperties;
  4.  
     
  5.  
    @ConfigurationProperties(prefix = "redisson")
  6.  
    public class RedissonProperties {
  7.  
     
  8.  
    private int timeout = 3000;
  9.  
     
  10.  
    private String address;
  11.  
     
  12.  
    private String password;
  13.  
     
  14.  
    private int connectionPoolSize = 64;
  15.  
     
  16.  
    private int connectionMinimumIdleSize=10;
  17.  
     
  18.  
    private int slaveConnectionPoolSize = 250;
  19.  
     
  20.  
    private int masterConnectionPoolSize = 250;
  21.  
     
  22.  
    private String[] sentinelAddresses;
  23.  
     
  24.  
    private String masterName;
  25.  
     
  26.  
    public int getTimeout() {
  27.  
    return timeout;
  28.  
    }
  29.  
     
  30.  
    public void setTimeout(int timeout) {
  31.  
    this.timeout = timeout;
  32.  
    }
  33.  
     
  34.  
    public int getSlaveConnectionPoolSize() {
  35.  
    return slaveConnectionPoolSize;
  36.  
    }
  37.  
     
  38.  
    public void setSlaveConnectionPoolSize(int slaveConnectionPoolSize) {
  39.  
    this.slaveConnectionPoolSize = slaveConnectionPoolSize;
  40.  
    }
  41.  
     
  42.  
    public int getMasterConnectionPoolSize() {
  43.  
    return masterConnectionPoolSize;
  44.  
    }
  45.  
     
  46.  
    public void setMasterConnectionPoolSize(int masterConnectionPoolSize) {
  47.  
    this.masterConnectionPoolSize = masterConnectionPoolSize;
  48.  
    }
  49.  
     
  50.  
    public String[] getSentinelAddresses() {
  51.  
    return sentinelAddresses;
  52.  
    }
  53.  
     
  54.  
    public void setSentinelAddresses(String sentinelAddresses) {
  55.  
    this.sentinelAddresses = sentinelAddresses.split(",");
  56.  
    }
  57.  
     
  58.  
    public String getMasterName() {
  59.  
    return masterName;
  60.  
    }
  61.  
     
  62.  
    public void setMasterName(String masterName) {
  63.  
    this.masterName = masterName;
  64.  
    }
  65.  
     
  66.  
    public String getPassword() {
  67.  
    return password;
  68.  
    }
  69.  
     
  70.  
    public void setPassword(String password) {
  71.  
    this.password = password;
  72.  
    }
  73.  
     
  74.  
    public String getAddress() {
  75.  
    return address;
  76.  
    }
  77.  
     
  78.  
    public void setAddress(String address) {
  79.  
    this.address = address;
  80.  
    }
  81.  
     
  82.  
    public int getConnectionPoolSize() {
  83.  
    return connectionPoolSize;
  84.  
    }
  85.  
     
  86.  
    public void setConnectionPoolSize(int connectionPoolSize) {
  87.  
    this.connectionPoolSize = connectionPoolSize;
  88.  
    }
  89.  
     
  90.  
    public int getConnectionMinimumIdleSize() {
  91.  
    return connectionMinimumIdleSize;
  92.  
    }
  93.  
     
  94.  
    public void setConnectionMinimumIdleSize(int connectionMinimumIdleSize) {
  95.  
    this.connectionMinimumIdleSize = connectionMinimumIdleSize;
  96.  
    }
  97.  
    }

属性文件配置项application.properties

  1.  
    # redisson lock
  2.  
    #1、单机模式
  3.  
    redisson.address=redis://10.18.75.115:6379
  4.  
    #redisson.password=
  5.  
     
  6.  
     
  7.  
    #2、哨兵模式
  8.  
    redisson.master-name=mymaster
  9.  
    #redisson.password=xxxx
  10.  
    redisson.sentinel-addresses=10.47.91.83:26379,10.47.91.83:26380,10.47.91.83:26381

基于Redisson+SpringBoot的Redission分布式锁的更多相关文章

  1. SpringBoot基于数据库实现简单的分布式锁

    本文介绍SpringBoot基于数据库实现简单的分布式锁. 1.简介 分布式锁的方式有很多种,通常方案有: 基于mysql数据库 基于redis 基于ZooKeeper 网上的实现方式有很多,本文主要 ...

  2. 基于ZooKeeper的三种分布式锁实现

    [欢迎关注公众号:程序猿讲故事 (codestory),及时接收最新文章] 今天介绍基于ZooKeeper的分布式锁的简单实现,包括阻塞锁和非阻塞锁.同时增加了网上很少介绍的基于节点的非阻塞锁实现,主 ...

  3. SpringBoot集成Redis分布式锁以及Redis缓存

    https://blog.csdn.net/qq_26525215/article/details/79182687 集成Redis 首先在pom.xml中加入需要的redis依赖和缓存依赖 < ...

  4. redission 分布式锁

    https://my.oschina.net/haogrgr/blog/469439   分布式锁和Redisson实现 Aug 20, 2017 CONTENTS 概述 分布式锁特性 Redis实现 ...

  5. 基于Redis实现简单的分布式锁

      在分布式场景下,有很多种情况都需要实现最终一致性.在设计远程上下文的领域事件的时候,为了保证最终一致性,在通过领域事件进行通讯的方式中,可以共享存储(领域模型和消息的持久化数据源),或者做全局XA ...

  6. 【连载】redis库存操作,分布式锁的四种实现方式[三]--基于Redis watch机制实现分布式锁

    一.redis的事务介绍 1. Redis保证一个事务中的所有命令要么都执行,要么都不执行.如果在发送EXEC命令前客户端断线了,则Redis会清空事务队列,事务中的所有命令都不会执行.而一旦客户端发 ...

  7. spring boot 利用redisson实现redis的分布式锁

    原文:http://liaoke0123.iteye.com/blog/2375469 利用redis实现分布式锁,网上搜索的大部分是使用java jedis实现的. redis官方推荐的分布式锁实现 ...

  8. Redisson源码解读-分布式锁

    前言 Redisson是一个在Redis的基础上实现的Java驻内存数据网格(In-Memory Data Grid).Redisson有一样功能是可重入的分布式锁.本文来讨论一下这个功能的特点以及源 ...

  9. 基于redis实现可靠的分布式锁

    什么是锁 今天要谈的是如何在分布式环境下实现一个全局锁,在开始之前先说说非分布式下的锁: 单机 – 单进程程序使用互斥锁mutex,解决多个线程之间的同步问题 单机 – 多进程程序使用信号量sem,解 ...

随机推荐

  1. Spring security 知识笔记【内存角色授权】

    一.原有的配置文件中,增加注解@EnableGlobalMethodSecurity(prePostEnabled = true) 二.原有配置文件中,内存新建账号的时候添加角色 package El ...

  2. gcc编译的时候报错 error trying to exec 'cc1plus': execvp 解决方法

    sudo apt install --reinstall build-essential -y

  3. $ is not defined与SpringMVC访问静态资源

    编写前台Jquery代码时,遇到谷歌浏览器报错:Uncaught ReferenceError: $ is not defined 意味着Jquery库并没有导入到页面.这是有几种情况需要考虑: 1. ...

  4. 计时器StopWatch的几种写法

    下面提供三种计时器的写法供大家参考,大家可以自行选择自己钟爱的使用. 写法一(Spring 包提供的计时器): import java.text.NumberFormat; import java.u ...

  5. Qt Quick 组件与动态对象

    博客24## 一.Components(组件) Component 是由 Qt 框架或开发者封装好的.只暴露了必要接口的 QML 类型,可以重复利用.一个 QML 组件就像一个黑盒子,它通过属性.信号 ...

  6. MySQL事务未提交导致整个表锁死

    问题及说明: 当一个SQL事务执行完了,但未COMMIT,后面的SQL想要执行就是被锁,超时结束:报错信息如下: mysql> ERROR 1205 (HY000): Lock wait tim ...

  7. Flink及Storm、Spark主流流框架比较

    转自:http://www.sohu.com/a/142553677_804130 引言 随着大数据时代的来临,大数据产品层出不穷.我们最近也对一款业内非常火的大数据产品 - Apache Flink ...

  8. windows上redis的安装和配置

    windows上redis的安装和配置 进入到Redis的根目录D:\Programming\Redis\Redis6379\Redis-x64-3.2.100底下操作: 配置文件启动 redis-s ...

  9. SCCM+WSUS的方式分发补丁

    简单来说,System Center Configuration Manager(SCCM/ConfigMgr)由SMS(Systems Management Server)发展而来,其作为一款针对企 ...

  10. 【docker】centos7 上拉取docker镜像,一直拉取不到,报错:Error response from daemon: Get https://registry-1.docker.io/v2/: net/http: request canceled while w

    镜像拉取一直报错: Error response from daemon: Get https://registry-1.docker.io/v2/: net/http: request cancel ...