基于Redisson+SpringBoot的Redission分布式锁
原文: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
定义分布式锁接口
- 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
基于Redisson+SpringBoot的Redission分布式锁的更多相关文章
- SpringBoot基于数据库实现简单的分布式锁
本文介绍SpringBoot基于数据库实现简单的分布式锁. 1.简介 分布式锁的方式有很多种,通常方案有: 基于mysql数据库 基于redis 基于ZooKeeper 网上的实现方式有很多,本文主要 ...
- 基于ZooKeeper的三种分布式锁实现
[欢迎关注公众号:程序猿讲故事 (codestory),及时接收最新文章] 今天介绍基于ZooKeeper的分布式锁的简单实现,包括阻塞锁和非阻塞锁.同时增加了网上很少介绍的基于节点的非阻塞锁实现,主 ...
- SpringBoot集成Redis分布式锁以及Redis缓存
https://blog.csdn.net/qq_26525215/article/details/79182687 集成Redis 首先在pom.xml中加入需要的redis依赖和缓存依赖 < ...
- redission 分布式锁
https://my.oschina.net/haogrgr/blog/469439 分布式锁和Redisson实现 Aug 20, 2017 CONTENTS 概述 分布式锁特性 Redis实现 ...
- 基于Redis实现简单的分布式锁
在分布式场景下,有很多种情况都需要实现最终一致性.在设计远程上下文的领域事件的时候,为了保证最终一致性,在通过领域事件进行通讯的方式中,可以共享存储(领域模型和消息的持久化数据源),或者做全局XA ...
- 【连载】redis库存操作,分布式锁的四种实现方式[三]--基于Redis watch机制实现分布式锁
一.redis的事务介绍 1. Redis保证一个事务中的所有命令要么都执行,要么都不执行.如果在发送EXEC命令前客户端断线了,则Redis会清空事务队列,事务中的所有命令都不会执行.而一旦客户端发 ...
- spring boot 利用redisson实现redis的分布式锁
原文:http://liaoke0123.iteye.com/blog/2375469 利用redis实现分布式锁,网上搜索的大部分是使用java jedis实现的. redis官方推荐的分布式锁实现 ...
- Redisson源码解读-分布式锁
前言 Redisson是一个在Redis的基础上实现的Java驻内存数据网格(In-Memory Data Grid).Redisson有一样功能是可重入的分布式锁.本文来讨论一下这个功能的特点以及源 ...
- 基于redis实现可靠的分布式锁
什么是锁 今天要谈的是如何在分布式环境下实现一个全局锁,在开始之前先说说非分布式下的锁: 单机 – 单进程程序使用互斥锁mutex,解决多个线程之间的同步问题 单机 – 多进程程序使用信号量sem,解 ...
随机推荐
- Spring security 知识笔记【内存角色授权】
一.原有的配置文件中,增加注解@EnableGlobalMethodSecurity(prePostEnabled = true) 二.原有配置文件中,内存新建账号的时候添加角色 package El ...
- gcc编译的时候报错 error trying to exec 'cc1plus': execvp 解决方法
sudo apt install --reinstall build-essential -y
- $ is not defined与SpringMVC访问静态资源
编写前台Jquery代码时,遇到谷歌浏览器报错:Uncaught ReferenceError: $ is not defined 意味着Jquery库并没有导入到页面.这是有几种情况需要考虑: 1. ...
- 计时器StopWatch的几种写法
下面提供三种计时器的写法供大家参考,大家可以自行选择自己钟爱的使用. 写法一(Spring 包提供的计时器): import java.text.NumberFormat; import java.u ...
- Qt Quick 组件与动态对象
博客24## 一.Components(组件) Component 是由 Qt 框架或开发者封装好的.只暴露了必要接口的 QML 类型,可以重复利用.一个 QML 组件就像一个黑盒子,它通过属性.信号 ...
- MySQL事务未提交导致整个表锁死
问题及说明: 当一个SQL事务执行完了,但未COMMIT,后面的SQL想要执行就是被锁,超时结束:报错信息如下: mysql> ERROR 1205 (HY000): Lock wait tim ...
- Flink及Storm、Spark主流流框架比较
转自:http://www.sohu.com/a/142553677_804130 引言 随着大数据时代的来临,大数据产品层出不穷.我们最近也对一款业内非常火的大数据产品 - Apache Flink ...
- windows上redis的安装和配置
windows上redis的安装和配置 进入到Redis的根目录D:\Programming\Redis\Redis6379\Redis-x64-3.2.100底下操作: 配置文件启动 redis-s ...
- SCCM+WSUS的方式分发补丁
简单来说,System Center Configuration Manager(SCCM/ConfigMgr)由SMS(Systems Management Server)发展而来,其作为一款针对企 ...
- 【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 ...