概述

实际开发中会遇到分布式锁的情况,解决方案有数据库(不推荐)、Redis(Redission 推荐)、zookeeper等。
这里我们介绍redisson方案。
官方解释,什么是redisson?
Redis Java Client with features of In-Memory Data Grid。

什么是redisson

redisson是一个在redis的java客户端,是在Redis基础上实现的数据网格功能。
他不仅提供了一系列的分布式的Java常用对象,还提供了许多分布式服务。
redisson提供的api均以面向对象的操作方式,将key-value封装成我们熟悉的集合或者对象,我们可以通过这些API更方便的操作数据。同时它提供了多个实现了java.util.concurrent接口的集合类,让我们能在线程安全的环境下操作数据。
其中redis的官网也将它纳入推荐使用的工具中。

pom依赖

 <parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>2.6.1</version><relativePath/> <!-- lookup parent from repository --></parent>...<!-- redisson config --><dependency><groupId>org.redisson</groupId><artifactId>redisson-spring-boot-starter</artifactId><version>3.16.6</version></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-data-redis</artifactId></dependency>

redission配置信息

spring.redis.cluster.nodes=${redis.nodes}
spring.redis.database=6
spring.redis.timeout=30000
spring.redis.enableTransactionSupport=false
spring.redis.password=${redis.password}
spring.redis.lettuce.pool.max-wait=30000
spring.redis.lettuce.pool.max-active=100
spring.redis.lettuce.pool.min-idle=50

redisson客户端对象实例化

package com.example.springboot_redis.redis;import lombok.extern.slf4j.Slf4j;
import org.redisson.Redisson;
import org.redisson.api.RedissonClient;
import org.redisson.config.Config;
import org.springframework.boot.autoconfigure.data.redis.RedisProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;/*** Redission配置类*/
@Slf4j
@Configuration
public class RedissionConfig {/*** redisson协议前缀*/private static final String SCHEMA_PREFIX = "redis://";/*** 锁超时时间*/@Value("${spring.redis.lockTimeOut:30000}")private long lockWatchTimeOut;@Beanpublic RedissonClient redissonClient(RedisProperties redisProperties) {Config config = new Config();RedisProperties.Sentinel sentinel = redisProperties.getSentinel();RedisProperties.Cluster redisPropertiesCluster = redisProperties.getCluster();if (redisPropertiesCluster != null) {//集群redisClusterServersConfig clusterServersConfig = config.useClusterServers();for (String cluster : redisPropertiesCluster.getNodes()) {clusterServersConfig.addNodeAddress(SCHEMA_PREFIX + cluster);}if (StringUtils.hasText(redisProperties.getPassword())) {clusterServersConfig.setPassword(redisProperties.getPassword());}clusterServersConfig.setTimeout((int) redisProperties.getTimeout().toMillis());clusterServersConfig.setPingConnectionInterval(30000);} else if (StringUtils.hasText(redisProperties.getHost())) {//单点redisSingleServerConfig singleServerConfig = config.useSingleServer().setAddress(SCHEMA_PREFIX + redisProperties.getHost() + ":" + redisProperties.getPort());if (StringUtils.hasText(redisProperties.getPassword())) {singleServerConfig.setPassword(redisProperties.getPassword());}singleServerConfig.setTimeout((int) redisProperties.getTimeout().toMillis());singleServerConfig.setPingConnectionInterval(30000);singleServerConfig.setDatabase(redisProperties.getDatabase());} else if (sentinel != null) {//哨兵模式SentinelServersConfig sentinelServersConfig = config.useSentinelServers();sentinelServersConfig.setMasterName(sentinel.getMaster());for (String node : sentinel.getNodes()) {sentinelServersConfig.addSentinelAddress(SCHEMA_PREFIX + node);}if (StringUtils.hasText(redisProperties.getPassword())) {sentinelServersConfig.setPassword(redisProperties.getPassword());}sentinelServersConfig.setTimeout((int) redisProperties.getTimeout().toMillis());sentinelServersConfig.setPingConnectionInterval(30000);sentinelServersConfig.setDatabase(redisProperties.getDatabase());}config.setLockWatchdogTimeout(lockWatchTimeOut);return Redisson.create(config);}
}

编写分布式锁工具类

package com.example.springboot_redis.redis;import lombok.extern.slf4j.Slf4j;
import org.redisson.api.RLock;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;import java.util.concurrent.TimeUnit;/*** 分布式Redis锁*/
@Slf4j
@Component
public class DistributedRedisLock {@Autowiredprivate RedissonClient redissonClient;// 加锁public Boolean lock(String lockName) {if (redissonClient == null) {log.info("DistributedRedisLock redissonClient is null");return false;}try {RLock lock = redissonClient.getLock(lockName);// 锁15秒后自动释放,防止死锁lock.lock(15, TimeUnit.SECONDS);log.info("Thread [{}] DistributedRedisLock lock [{}] success", Thread.currentThread().getName(), lockName);// 加锁成功return true;} catch (Exception e) {log.error("DistributedRedisLock lock [{}] Exception:", lockName, e);return false;}}// 释放锁public Boolean unlock(String lockName) {if (redissonClient == null) {log.info("DistributedRedisLock redissonClient is null");return false;}try {RLock lock = redissonClient.getLock(lockName);lock.unlock();log.info("Thread [{}] DistributedRedisLock unlock [{}] success", Thread.currentThread().getName(), lockName);// 释放锁成功return true;} catch (Exception e) {log.error("DistributedRedisLock unlock [{}] Exception:", lockName, e);return false;}}
}

功能测试

package com.example.springboot_redis.contoller;import com.example.springboot_redis.redis.DistributedRedisLock;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;/*** 分布式Redis锁测试controller*/
@Slf4j
@RestController
@RequestMapping("/lock")
public class LockTestController {private final String LOCK = "LOCK";@Autowiredprivate DistributedRedisLock distributedRedisLock;// 测试不释放锁@GetMapping("/testLock")public void testLock() {for (int i = 0; i < 5; i++) {new Thread(() -> {distributedRedisLock.lock(LOCK);}).start();}}// 实际业务开发使用分布式锁的方式@PostMappingpublic void post() {try {if (distributedRedisLock.lock(LOCK)) {// 业务逻辑log.info("开始业务逻辑");} else {// 处理获取锁失败的逻辑log.info("获取锁失败");}} catch (Exception e) {log.error("处理异常:", e);} finally {distributedRedisLock.unlock(LOCK);}}
}

测试结果

2021-12-17 15:03:14.243  INFO 64496 --- [      Thread-21] com.study.practice.utils.DistributeLock  : Thread [Thread-21] DistributedRedisLock lock [LOCK] success
2021-12-17 15:03:29.243  INFO 64496 --- [      Thread-23] com.study.practice.utils.DistributeLock  : Thread [Thread-23] DistributedRedisLock lock [LOCK] success
2021-12-17 15:03:44.246  INFO 64496 --- [      Thread-20] com.study.practice.utils.DistributeLock  : Thread [Thread-20] DistributedRedisLock lock [LOCK] success
2021-12-17 15:03:59.250  INFO 64496 --- [      Thread-19] com.study.practice.utils.DistributeLock  : Thread [Thread-19] DistributedRedisLock lock [LOCK] success
2021-12-17 15:04:14.255  INFO 64496 --- [      Thread-22] com.study.practice.utils.DistributeLock  : Thread [Thread-22] DistributedRedisLock lock [LOCK] success

结果分析

  1. 调用方法启用5个线程,每个线程都去获取分布式锁,只有一个线程能获取到锁,其他线程均处于阻塞状态
  2. 因为没有调用释放锁的方法,且在获取锁的lock()方法中设置了锁的最大时间为15秒(防止死锁的发生),所以在15秒后锁自动释放,由其他线程进行竞争这把分布式锁然后执行。

总结

在实际使用redission作为分布式锁时,操作步骤如下:

  1. 调用distributedRedisLock.lock(String lockName) 获取分布式锁;
  2. 如果返回true,执行业务逻辑。如果返回false,进行阻塞;
  3. 当执行完业务逻辑后,调用distributedRedisLock.unlock(String lockName)释放锁。

参考

SprinBoot2.2.x 整合 Redission (分布式锁)
Redisson实现分布式锁(1)—原理

Redisson与SpringBoot整合相关推荐

  1. springboot整合redisson实现多种分布式锁

    Redisson概述 Redisson是一个在Redis的基础上实现的Java驻内存数据网格(In-Memory Data Grid).它不仅提供了一系列的分布式的Java常用对象,还提供了许多分布式 ...

  2. springboot整合redisson(一)搭建Redisson环境

    一.创建springboot项目 1.通过idea创建springboot项目 2.通过web网站创建springboot项目 创建完之后的项目结构如下: 二.引入redisson依赖 由于我们是sp ...

  3. SpringBoot整合Redisson实现延迟队列

    SpringBoot整合Redisson实现延迟队列 技术选型 引入 Redisson 依赖 配置项 编写工具类 延迟队列执行器 业务消费类枚举 加载消费队列 消费者类 测试类 测试结果 技术选型 关 ...

  4. rabbit和mysql事务_分布式事务原理及SpringBoot整合RabbitMQ实现可靠事件,TCC事务模型及接口幂等性...

    分布式事务 我们知道在单数据库系统中,实现数据的一致性,通过数据库的事务来处理比较简单.在微服务或分布式系统中,各个独立的服务都会有自己的数据库,而不是在同一个数据库中,所以当一组事务(如商品交易中, ...

  5. springboot整合redis实现分布式锁思想

    思路 所有响应获取锁的线程都先尝试往redis中创建一个缓存数据,所有线程的key必须相同.使用的是redis的setnx命令.就只有一个线程能够创建成功,创建成功的线程就成功获取锁. 没有获取锁的线 ...

  6. springboot整合之版本号统一管理

    特别说明:本次项目整合基于idea进行的,如果使用Eclipse可能操作会略有不同,不过总的来说不影响. springboot整合之如何选择版本及项目搭建 springboot整合之版本号统一管理  ...

  7. springboot整合之统一异常处理

    特别说明:本次项目整合基于idea进行的,如果使用Eclipse可能操作会略有不同,不过总的来说不影响. springboot整合之如何选择版本及项目搭建 springboot整合之版本号统一管理  ...

  8. SpringBoot整合Jedis

    我们在使用springboot搭建微服务的时候,在很多时候还是需要redis的高速缓存来缓存一些数据,存储一些高频率访问的数据,如果直接使用redis的话又比较麻烦,在这里,我们使用jedis来实现r ...

  9. 【七】springboot整合redis(超详细)

    springboot篇章整体栏目: [一]springboot整合swagger(超详细 [二]springboot整合swagger(自定义)(超详细) [三]springboot整合token(超 ...

最新文章

  1. git的基本使用-1
  2. Linux上安装php
  3. LeetCode 916. 单词子集(计数)
  4. Java面试知识点:多态、内部类
  5. android nsdservice 类型,Android NSD onServiceFound()没有被调用
  6. 云安全之虚拟机安全监控
  7. Android Debug方法
  8. wuyun知识库目录
  9. IDEA设置背景与字体大小
  10. 杂项-数学软件:MATLAB
  11. 关于 TJA1043 休眠和唤醒一点使用方法
  12. adobe illustrator的格式刷
  13. 文件服务器和ftp服务器的区别
  14. 拼多多按关键字搜索商品 API
  15. 由二叉树构造赫夫曼树
  16. Jenkins流水线配置
  17. JS获取日期(年/月/日/时/分/秒)以及完整格式转化(补0)
  18. 元宇宙为服装设计展示提供数字化社交平台
  19. 安装Node-sass的时候,报ensuring that file exists: C:\Python27\python.exe
  20. 北斗GPS校时服务器(卫星授时)应用于机场网络系统集成

热门文章

  1. 软件性能测试面试题一
  2. [ztjSQL]索引
  3. 备战金三银四,软件测试面试前都要做哪些准备?速看
  4. Unity WebGL发布及Ubuntu Apache服务器部署
  5. Android 启动引导图的实现
  6. SwiftUI 创业之缺乏写App的创意该怎么办
  7. 【Makefile】报错:undefined reference to symbol ‘pthread_spin_init@@GLIBC_2.2.5‘
  8. EditorConfig + ESLint + Prettier 实现代码规范化
  9. 如何使用office的word自动编号(自编)
  10. React+fetch通过修改配置文件解决跨域问题