<?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>
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
max-redirects: 3
timeout: 1000
password: 123456
jedis:
pool:
max-idle: 200
min-idle: 100
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
*/
@Bean
public 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);
}
}
这些配置项可以帮助你更好地管理Redis集群,并确保数据的高可用性和一致性。在Redisson中,可以通过使用ClusterServersConfig类来配置这些参数,以便与Redis集群进行通信和交互。
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.Objects;
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:";
@Autowired
private 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());
if(Objects.nonNull(rLock) && rLock.isHeldByCurrentThread()){
rLock.unlock();
}
log.info(">>>>>> 释放锁结束, lockKey:{},currentThreadId:{} <<<<<<",lockKey,Thread.currentThread().getId());
}
}
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 {
@Autowired
private RedissonUtil redissonUtil;
@Override
public Object execute() {
String lockKey = "system:lockKey";
RLock rLock = redissonUtil.getLock(lockKey);
try {
if(redissonUtil.tryLock(rLock,30000, TimeUnit.MILLISECONDS)){
// TODO 业务逻辑
}else{
log.info("获取分布式锁失败");
}
} catch (Exception e) {
log.error("交易异常",e);
} finally {
// 释放自己的锁
redissonUtil.unlock(rLock,lockKey);
}
return true;
}
}
package com.kang.redisson.annotation;
import java.lang.annotation.*;
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface DistributedLock {
/**
* 锁名称
*/
String value();
/**
* 过期时间(单位:秒)
*/
long expire() default 30;
/**
* 等待时间(单位:毫秒)
*/
long waitTime() default 100;
/**
* 锁超时时间(单位:秒)
*/
long lockTimeout() default 10;
/**
* 是否公平锁
*/
boolean fair() default false;
/**
* 是否尝试锁
*/
boolean tryLock() default false;
}
锁名称
*/
String value();
/**
* 过期时间(单位:秒)
*/
long expire() default 30;
/**
* 等待时间(单位:毫秒)
*/
long waitTime() default 100;
/**
* 锁超时时间(单位:秒)
*/
long lockTimeout() default 10;
/**
* 是否公平锁
*/
boolean fair() default false;
/**
* 是否尝试锁
*/
boolean tryLock() default false;
}