概述
实际开发中会遇到分布式锁的情况,解决方案有数据库(不推荐)、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;
@Bean
public RedissonClient redissonClient(RedisProperties redisProperties) {
Config config = new Config();
RedisProperties.Sentinel sentinel = redisProperties.getSentinel();
RedisProperties.Cluster redisPropertiesCluster = redisProperties.getCluster();
if (redisPropertiesCluster != null) {
//集群redis
ClusterServersConfig 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())) {
//单点redis
SingleServerConfig 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 {
@Autowired
private 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";
@Autowired
private DistributedRedisLock distributedRedisLock;
// 测试不释放锁
@GetMapping("/testLock")
public void testLock() {
for (int i = 0; i < 5; i++) {
new Thread(() -> {
distributedRedisLock.lock(LOCK);
}).start();
}
}
// 实际业务开发使用分布式锁的方式
@PostMapping
public 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
结果分析
- 调用方法启用5个线程,每个线程都去获取分布式锁,只有一个线程能获取到锁,其他线程均处于阻塞状态
- 因为没有调用释放锁的方法,且在获取锁的lock()方法中设置了锁的最大时间为15秒(防止死锁的发生),所以在15秒后锁自动释放,由其他线程进行竞争这把分布式锁然后执行。
总结
在实际使用redission作为分布式锁时,操作步骤如下:
- 调用distributedRedisLock.lock(String lockName) 获取分布式锁;
- 如果返回true,执行业务逻辑。如果返回false,进行阻塞;
- 当执行完业务逻辑后,调用distributedRedisLock.unlock(String lockName)释放锁。
参考
Redis客户端之Redission
SprinBoot2.2.x 整合 Redission (分布式锁)
Redisson实现分布式锁(1)—原理