SpringBoot+Redisson分布式锁
SpringBoot+Redisson分布式锁
文章目录
- SpringBoot+Redisson分布式锁
- 1.引入依赖
- 2.编写配置类
- org.redisson.config.Config类是Redisson框架中用于配置Redisson客户端的类。以下是一些常用的配置项:
- org.redisson.config.ClusterServersConfig类是Redisson框架中用于配置Redis集群模式的类。以下是一些常用的配置项:
- 3.编写工具类
- 4.编写使用场景示例
1.引入依赖
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>2.2.6.RELEASE</version><relativePath/> <!-- lookup parent from repository --></parent><groupId>com.kang</groupId><artifactId>redisson</artifactId><version>0.0.1-SNAPSHOT</version><name>redisson-lock-project</name><description>redisson-lock-project</description><properties><java.version>18</java.version></properties><dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter</artifactId></dependency><dependency><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId><optional>true</optional></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope></dependency><dependency><groupId>org.apache.commons</groupId><artifactId>commons-lang3</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-data-redis</artifactId></dependency><dependency><groupId>redis.clients</groupId><artifactId>jedis</artifactId><version>3.6.1</version></dependency><dependency><groupId>org.redisson</groupId><artifactId>redisson-spring-boot-starter</artifactId><version>3.10.7</version></dependency></dependencies><build><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId><configuration><excludes><exclude><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId></exclude></excludes></configuration></plugin></plugins></build></project>
2.编写配置类
package com.kang.redisson.config;import org.apache.commons.lang3.StringUtils;
import org.redisson.Redisson;
import org.redisson.api.RedissonClient;
import org.redisson.config.ClusterServersConfig;
import org.redisson.config.Config;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;/*** @Author Emperor Kang* @ClassName RedissonConfig* @Description RedissonConfig* @Date 2024/1/18 16:19* @Version 1.0* @Motto 让营地比你来时更干净*/
@Configuration
public class RedissonConfig {/*** spring.redis.cluster.nodes=redis://192.168.0.1:6379,redis://192.168.0.2:6379,redis://192.168.0.3:6379,redis://192.168.0.4:6379,redis://192.168.0.5:6379,redis://192.168.0.6:6379*/@Value("${spring.redis.cluster.nodes}")private String redisNodes;@Value("${spring.redis.password}")private String password;/*** 实例化客户端* @return*/@Beanpublic RedissonClient redissonClient(){Config config = new Config();ClusterServersConfig clusterServersConfig = config.useClusterServers().addNodeAddress(redisNodes.split(","));// 密码if(StringUtils.isNoneBlank(password)){clusterServersConfig.setPassword(password);} else {clusterServersConfig.setPassword(password);}return Redisson.create(config);}
}
org.redisson.config.Config类是Redisson框架中用于配置Redisson客户端的类。以下是一些常用的配置项:
- codec(编码):默认值是org.redisson.codec.JsonJacksonCodec,用于定义与Redis交互时使用的编解码器。
- useSingleServer:设置为true时,将使用单节点模式进行连接。
- useMasterSlave:设置为true时,将使用主从节点模式进行连接。
- useSentinel:设置为true时,将使用哨兵模式进行连接。
- useCluster:设置为true时,将使用集群模式进行连接。
- useReplicated:设置为true时,将使用复制模式进行连接。
- address:Redis服务器的地址和端口号。
- database:要连接的Redis数据库的索引号。
- password:连接Redis服务器所需的密码。
- timeout:连接超时时间(毫秒)。
- connectionPoolSize:连接池的大小。
- connectionMinimumIdleSize:连接池中的最小空闲连接数。
- retryAttempts:重试连接的次数。
- retryInterval:重试连接的间隔时间(毫秒)。
- autoRetry:是否自动重试连接。
- ipAddressToConnectTo:要连接的Redis服务器IP地址。
- maxRedirections:重定向的最大次数。
- useConnectionPool:是否使用连接池。
- useReconnection:是否启用自动重新连接。
- useDataSerialization:是否使用数据序列化。
- useKeepAlive:是否使用保持活动连接功能。
- useOffHeapMemory:是否使用OffHeap内存存储数据。
- store:用于存储数据的存储实例或配置。
- springCacheName:与Spring框架集成的缓存名称。
- autoCreateObjectCaches:是否自动创建对象缓存。
- numberOfSlaves:主从模式下的从节点数量。
- masterName:主节点名称。
- slaveNames:从节点名称列表。
- sentinelMasterName:哨兵模式下主节点的名称。
- sentinelNames:哨兵节点的名称列表。
- clusterNodes:集群模式下节点的地址列表。
- replicatedMapName:复制模式下要复制的映射名称。
- replicatedMapKeyField:复制模式下映射的键字段名称。
- replicatedMapValueField:复制模式下映射的值字段名称。
- replicatedMapUseReferenceValues:是否使用引用值作为复制映射的值。
- replicatedMapUseReferenceKeys:是否使用引用键作为复制映射的键。
- clusterNameNodes:集群模式下节点组的名称列表。
- clusterNodeConfigName:集群模式下节点组的配置名称。
- clusterSlaveConfigName:集群模式下从节点组的配置名称。
- clusterSentinelConfigName:集群模式下哨兵节点组的配置名称。
- clusterMapConfigName:集群模式下映射的配置名称。
- clusterReplicatedMapConfigName:集群模式下复制映射的配置名称。
- clusterReplicatedMapKeyField:复制映射的键字段名称。
- clusterReplicatedMapValueField:复制映射的值字段名称。
- clusterReplicatedMapUseReferenceValues:是否使用引用值作为复制映射的值。
- clusterReplicatedMapUseReferenceKeys:是否使用引用键作为复制映射的键。
org.redisson.config.ClusterServersConfig类是Redisson框架中用于配置Redis集群模式的类。以下是一些常用的配置项:
- nodeAddresses:添加节点地址。可以通过host:port的格式来添加Redis集群节点的地址。多个节点可以一次性批量添加。
- scanInterval:集群扫描间隔时间。默认值是1000,表示对Redis集群节点状态扫描的时间间隔。单位是毫秒。
- readMode:读取操作的负载均衡模式。默认值是SLAVE,表示只在从服务节点里读取。
- slots:分片数量。默认值是231,用于指定数据分片过程中的分片数量。
这些配置项可以帮助你更好地管理Redis集群,并确保数据的高可用性和一致性。在Redisson中,可以通过使用ClusterServersConfig类来配置这些参数,以便与Redis集群进行通信和交互。
3.编写工具类
package com.kang.redisson.utils;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;/*** @Author Emperor Kang* @ClassName RedissonUtil* @Description RedissonUtil* @Date 2024/1/18 16:32* @Version 1.0* @Motto 让营地比你来时更干净*/
@Component
@Slf4j
public class RedissonUtil {private static final String LOCK_PREFIX = "lock:";@Autowiredprivate RedissonClient redissonClient;/*** 获取锁* @param lockKey 锁的key* @return*/public RLock getLock(String lockKey){log.info(">>>>>> 获取锁,lockKey:{} <<<<<<",lockKey);return redissonClient.getLock(LOCK_PREFIX + lockKey);}/*** 获取锁* @param rLock 当前锁对象* @param waitTime 等待时间* @param leaseTime 持有锁的时间* @param timeUnit 时间单位* @return true表示获取锁成功,false表示获取锁失败*/public boolean tryLock(RLock rLock, long waitTime, long leaseTime, TimeUnit timeUnit) {log.info(">>>>>> 加锁开始, currentThreadId:{} <<<<<<",Thread.currentThread().getId());try {boolean lockResult = rLock.tryLock(waitTime, leaseTime, timeUnit);log.info(">>>>>> 加锁结束, currentThreadId:{},加锁:{} <<<<<<",Thread.currentThread().getId(),lockResult ? "SUCCESS" : "FAIL");return lockResult;} catch (InterruptedException e) {log.error("加锁出现异常", e);Thread.currentThread().interrupt();return false;}}/*** 加锁* @param rLock 当前锁对象* @param leaseTime 持有锁的时间* @param timeUnit 时间单位* @return true表示获取锁成功,false表示获取锁失败*/public boolean tryLock(RLock rLock, long leaseTime, TimeUnit timeUnit) {log.info(">>>>>> 加锁开始, currentThreadId:{} <<<<<<",Thread.currentThread().getId());try {boolean lockResult = rLock.tryLock(0L, leaseTime, timeUnit);log.info(">>>>>> 加锁结束, currentThreadId:{},加锁:{} <<<<<<",Thread.currentThread().getId(),lockResult ? "SUCCESS" : "FAIL");return lockResult;} catch (InterruptedException e) {log.error("加锁出现异常", e);Thread.currentThread().interrupt();return false;}}/*** 释放锁* @param rLock 当前锁对象* @param lockKey 锁的key*/public void unlock(RLock rLock,String lockKey) {log.info(">>>>>> 释放锁开始, lockKey:{},currentThreadId:{} <<<<<<",lockKey,Thread.currentThread().getId());rLock.unlock();log.info(">>>>>> 释放锁结束, lockKey:{},currentThreadId:{} <<<<<<",lockKey,Thread.currentThread().getId());}
}
4.编写使用场景示例
package com.kang.redisson.service.impl;import com.kang.redisson.service.RedissonService;
import com.kang.redisson.utils.RedissonUtil;
import lombok.extern.slf4j.Slf4j;
import org.redisson.api.RLock;
import org.springframework.beans.factory.annotation.Autowired;import java.util.concurrent.TimeUnit;/*** @Author Emperor Kang* @ClassName RedissonServiceImpl* @Description RedissonServiceImpl* @Date 2024/1/18 17:27* @Version 1.0* @Motto 让营地比你来时更干净*/
@Slf4j
public class RedissonServiceImpl implements RedissonService {@Autowiredprivate RedissonUtil redissonUtil;@Overridepublic Object execute() {String lockKey = "system:lockKey";RLock rLock = redissonUtil.getLock(lockKey);boolean tryLock = redissonUtil.tryLock(rLock,30000, TimeUnit.MILLISECONDS);try {if(tryLock){// TODO 业务逻辑}} catch (Exception e) {log.error("交易异常",e);} finally {// 只释放自己的锁if(rLock.isHeldByCurrentThread()){redissonUtil.unlock(rLock,lockKey);}}return true;}
}