mirror of https://gitee.com/stylefeng/roses
【8.1.9】【lock】初始化分布式锁
parent
6c4f952841
commit
3188a40da3
|
@ -0,0 +1 @@
|
|||
锁模块-提供分布式锁的支持
|
|
@ -0,0 +1 @@
|
|||
锁模块-分布式锁的接口
|
|
@ -0,0 +1,16 @@
|
|||
<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<parent>
|
||||
<groupId>com.gunsdevops</groupId>
|
||||
<artifactId>kernel-d-lock</artifactId>
|
||||
<version>8.1.9</version>
|
||||
<relativePath>../pom.xml</relativePath>
|
||||
</parent>
|
||||
|
||||
<artifactId>lock-api</artifactId>
|
||||
<packaging>jar</packaging>
|
||||
|
||||
</project>
|
|
@ -0,0 +1,33 @@
|
|||
package cn.stylefeng.roses.kernel.lock.api;
|
||||
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
/**
|
||||
* 分布式锁对象接口
|
||||
*
|
||||
* @author yaoliguo
|
||||
* @since 2024-09-12 14:25
|
||||
*/
|
||||
public interface KernelLock {
|
||||
|
||||
/**
|
||||
* 尝试获取定义了获取锁
|
||||
*
|
||||
* @param waitTime 获取锁的最大时间
|
||||
* @param leaseTime 租约时间 单位时间单位
|
||||
* @return true-锁被成功获取,false-锁已经设置
|
||||
* @throws InterruptedException 如果线程被中断
|
||||
* @author yaoliguo
|
||||
* @since 2024-09-12 14:25
|
||||
*/
|
||||
boolean tryLock(long waitTime, long leaseTime, TimeUnit unit) throws InterruptedException;
|
||||
|
||||
/**
|
||||
* 释放锁
|
||||
*
|
||||
* @author yaoliguo
|
||||
* @since 2024-09-12 14:25
|
||||
*/
|
||||
void unlock();
|
||||
|
||||
}
|
|
@ -0,0 +1,23 @@
|
|||
package cn.stylefeng.roses.kernel.lock.api;
|
||||
|
||||
/**
|
||||
* 获取锁的客户端
|
||||
*
|
||||
* @author yaoliguo
|
||||
* @since 2024-09-12 15:45
|
||||
*/
|
||||
public interface KernelLockClient {
|
||||
|
||||
/**
|
||||
* 按名称返回锁实例
|
||||
* <p>
|
||||
* 实现非公平锁定,因此不保证线程的获取顺序
|
||||
*
|
||||
* @param name 名称——对象的名称
|
||||
* @return 锁对象
|
||||
* @author yaoliguo
|
||||
* @since 2024-09-12 15:45
|
||||
*/
|
||||
KernelLock getLock(String name);
|
||||
|
||||
}
|
|
@ -0,0 +1,32 @@
|
|||
<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 http://maven.apache.org/maven-v4_0_0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<parent>
|
||||
<groupId>com.gunsdevops</groupId>
|
||||
<artifactId>kernel-d-lock</artifactId>
|
||||
<version>8.1.9</version>
|
||||
<relativePath>../pom.xml</relativePath>
|
||||
</parent>
|
||||
|
||||
<artifactId>lock-sdk-redisson</artifactId>
|
||||
<name>lock-sdk-redisson</name>
|
||||
<packaging>jar</packaging>
|
||||
|
||||
<dependencies>
|
||||
|
||||
<!--lock模块的api-->
|
||||
<dependency>
|
||||
<groupId>com.gunsdevops</groupId>
|
||||
<artifactId>lock-api</artifactId>
|
||||
<version>${roses.version}</version>
|
||||
</dependency>
|
||||
|
||||
<!--redisson分布式锁-->
|
||||
<dependency>
|
||||
<groupId>org.redisson</groupId>
|
||||
<artifactId>redisson</artifactId>
|
||||
<version>${redisson.version}</version>
|
||||
</dependency>
|
||||
|
||||
</dependencies>
|
||||
</project>
|
|
@ -0,0 +1,28 @@
|
|||
package cn.stylefeng.roses.kernel.lock.redisson;
|
||||
|
||||
import cn.stylefeng.roses.kernel.lock.api.KernelLock;
|
||||
import cn.stylefeng.roses.kernel.lock.api.KernelLockClient;
|
||||
import org.redisson.api.RLock;
|
||||
import org.redisson.api.RedissonClient;
|
||||
|
||||
/**
|
||||
* 分布式锁的redisson的客户端
|
||||
*
|
||||
* @author yaoliguo
|
||||
* @since 2024-09-12 15:52
|
||||
*/
|
||||
public class RedissonKernelClient implements KernelLockClient {
|
||||
|
||||
private final RedissonClient redissonClient;
|
||||
|
||||
public RedissonKernelClient(RedissonClient redissonClient) {
|
||||
this.redissonClient = redissonClient;
|
||||
}
|
||||
|
||||
@Override
|
||||
public KernelLock getLock(String name) {
|
||||
RLock lock = redissonClient.getLock(name);
|
||||
return new RedissonKernelLock(lock);
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,32 @@
|
|||
package cn.stylefeng.roses.kernel.lock.redisson;
|
||||
|
||||
import cn.stylefeng.roses.kernel.lock.api.KernelLock;
|
||||
import org.redisson.api.RLock;
|
||||
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
/**
|
||||
* 基于RedissonLock的实现
|
||||
*
|
||||
* @author yaoliguo
|
||||
* @since 2024-09-12 15:13
|
||||
*/
|
||||
public class RedissonKernelLock implements KernelLock {
|
||||
|
||||
private final RLock lock;
|
||||
|
||||
public RedissonKernelLock(RLock lock) {
|
||||
this.lock = lock;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean tryLock(long waitTime, long leaseTime, TimeUnit unit) throws InterruptedException {
|
||||
return lock.tryLock(waitTime, leaseTime, unit);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void unlock() {
|
||||
lock.unlock();
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,21 @@
|
|||
<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 http://maven.apache.org/maven-v4_0_0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<parent>
|
||||
<groupId>com.gunsdevops</groupId>
|
||||
<artifactId>kernel-d-lock</artifactId>
|
||||
<version>8.1.9</version>
|
||||
<relativePath>../pom.xml</relativePath>
|
||||
</parent>
|
||||
<artifactId>lock-spring-boot-starter-redisson</artifactId>
|
||||
<packaging>jar</packaging>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>com.gunsdevops</groupId>
|
||||
<artifactId>lock-sdk-redisson</artifactId>
|
||||
<version>${roses.version}</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
</project>
|
|
@ -0,0 +1,179 @@
|
|||
package cn.stylefeng.roses.kernel.lock.starter;
|
||||
|
||||
import cn.hutool.core.util.ObjectUtil;
|
||||
import cn.stylefeng.roses.kernel.lock.redisson.RedissonKernelClient;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.redisson.Redisson;
|
||||
import org.redisson.api.RedissonClient;
|
||||
import org.redisson.client.codec.StringCodec;
|
||||
import org.redisson.config.Config;
|
||||
import org.redisson.config.ReadMode;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
/**
|
||||
* 项目锁的redisson配置
|
||||
*
|
||||
* @author yaoliguo
|
||||
* @since 2024/9/19 21:29
|
||||
*/
|
||||
@Configuration
|
||||
@Slf4j
|
||||
public class ProjectLockCacheAutoConfiguration {
|
||||
|
||||
/**
|
||||
* Redis服务器地址
|
||||
*/
|
||||
@Value("${spring.redis.host}")
|
||||
private String host;
|
||||
|
||||
/**
|
||||
* Redis端口
|
||||
*/
|
||||
@Value("${spring.redis.port}")
|
||||
private String port;
|
||||
|
||||
/**
|
||||
* Redis密码
|
||||
*/
|
||||
@Value("${spring.redis.password:}")
|
||||
private String password;
|
||||
|
||||
/**
|
||||
* Sentinel 节点列表
|
||||
*/
|
||||
@Value("${spring.redisson.sentinel.nodes:}")
|
||||
private String sentinel;
|
||||
|
||||
/**
|
||||
* Sentinel 模式中的主节点名称
|
||||
*/
|
||||
@Value("${spring.redisson.sentinel.master:}")
|
||||
private String masterName;
|
||||
|
||||
/**
|
||||
* Redis 集群模式中的节点列表
|
||||
*/
|
||||
@Value("${spring.redisson.cluster:}")
|
||||
private String cluster;
|
||||
|
||||
/**
|
||||
* 超时时间,单位:毫秒
|
||||
*/
|
||||
private final int REDIS_RESPONSE_MILLISECONDS = 2000;
|
||||
|
||||
/**
|
||||
* 扫描间隔,单位:毫秒
|
||||
*/
|
||||
private final int REDIS_CLUSTER_SCAN_INTERVAL = 60000;
|
||||
|
||||
/**
|
||||
* redis地址前缀
|
||||
*/
|
||||
private static final String ADDRESS_PREFIX = "redis://";
|
||||
|
||||
/**
|
||||
* 初始化Redisson客户端
|
||||
*
|
||||
* @author fengshuonan
|
||||
* @since 2024/9/19 21:48
|
||||
*/
|
||||
@Bean
|
||||
public RedissonClient myRedissonClient() {
|
||||
|
||||
// 哨兵模式
|
||||
if (ObjectUtil.isNotEmpty(sentinel)) {
|
||||
log.info("redis is sentinel mode");
|
||||
return redissonSentinel();
|
||||
}
|
||||
|
||||
// 集群模式
|
||||
if (ObjectUtil.isNotEmpty(cluster)) {
|
||||
log.info("redis is cluster mode");
|
||||
return redissonCluster();
|
||||
}
|
||||
|
||||
// 单机模式
|
||||
if (ObjectUtil.isNotEmpty(host)) {
|
||||
log.info("redis is single mode");
|
||||
return redissonSingle();
|
||||
}
|
||||
|
||||
log.error("redisson config can not support this redis mode");
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* 初始化锁获取
|
||||
*
|
||||
* @author fengshuonan
|
||||
* @since 2024/9/19 21:52
|
||||
*/
|
||||
@Bean
|
||||
public RedissonKernelClient RedissonKernelClient(RedissonClient redissonClient) {
|
||||
return new RedissonKernelClient(redissonClient);
|
||||
}
|
||||
|
||||
/**
|
||||
* 初始化Redisson:单机模式
|
||||
*
|
||||
* @author fengshuonan
|
||||
* @since 2024/9/19 21:52
|
||||
*/
|
||||
private RedissonClient redissonSingle() {
|
||||
Config config = new Config();
|
||||
String address = ADDRESS_PREFIX + host + ":" + port;
|
||||
// 设置
|
||||
config.setCodec(new StringCodec())
|
||||
// 这是用的集群server
|
||||
.useSingleServer().setAddress(address).setTimeout(REDIS_RESPONSE_MILLISECONDS).setPassword(password);
|
||||
return Redisson.create(config);
|
||||
}
|
||||
|
||||
/**
|
||||
* 初始化Redisson:哨兵模式
|
||||
*
|
||||
* @author fengshuonan
|
||||
* @since 2024/9/19 21:52
|
||||
*/
|
||||
private RedissonClient redissonSentinel() {
|
||||
|
||||
String[] nodes = sentinel.split(",");
|
||||
// redisson版本是3.5,集群的ip前面要加上“redis://”,不然会报错,3.2版本可不加
|
||||
for (int i = 0; i < nodes.length; i++) {
|
||||
nodes[i] = ADDRESS_PREFIX + nodes[i];
|
||||
}
|
||||
Config config = new Config();
|
||||
// 设置
|
||||
config.setCodec(new StringCodec()).useSentinelServers().setMasterName(masterName).setPassword(password).setTimeout(REDIS_RESPONSE_MILLISECONDS).addSentinelAddress(nodes)
|
||||
// 在Redisson启动期间启用sentinels列表检查,默认为true,这里我们设置为false,不检查
|
||||
.setCheckSentinelsList(false);
|
||||
return Redisson.create(config);
|
||||
}
|
||||
|
||||
/**
|
||||
* 初始化Redisson:集群模式
|
||||
*
|
||||
* @author fengshuonan
|
||||
* @since 2024/9/19 21:52
|
||||
*/
|
||||
private RedissonClient redissonCluster() {
|
||||
String[] nodes = cluster.split(",");
|
||||
// redisson版本是3.5,集群的ip前面要加上“redis://”,不然会报错,3.2版本可不加
|
||||
for (int i = 0; i < nodes.length; i++) {
|
||||
nodes[i] = ADDRESS_PREFIX + nodes[i];
|
||||
}
|
||||
Config config = new Config();
|
||||
// 设置
|
||||
config.setCodec(new StringCodec())
|
||||
// 这是用的集群server
|
||||
.useClusterServers()
|
||||
// 设置集群状态扫描时间
|
||||
.setScanInterval(REDIS_CLUSTER_SCAN_INTERVAL).addNodeAddress(nodes).setPassword(password).setReadMode(ReadMode.MASTER);
|
||||
;
|
||||
return Redisson.create(config);
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,2 @@
|
|||
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
|
||||
cn.stylefeng.roses.kernel.lock.starter.ProjectLockCacheAutoConfiguration
|
|
@ -0,0 +1,31 @@
|
|||
<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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<parent>
|
||||
<groupId>com.javaguns.roses</groupId>
|
||||
<artifactId>roses-kernel</artifactId>
|
||||
<version>8.1.9</version>
|
||||
<relativePath>../pom.xml</relativePath>
|
||||
</parent>
|
||||
|
||||
<groupId>com.gunsdevops</groupId>
|
||||
<artifactId>kernel-d-lock</artifactId>
|
||||
<packaging>pom</packaging>
|
||||
|
||||
<name>kernel-d-lock</name>
|
||||
|
||||
<modules>
|
||||
<module>lock-api</module>
|
||||
<module>lock-sdk-redisson</module>
|
||||
<module>lock-spring-boot-starter-redisson</module>
|
||||
</modules>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>com.javaguns.roses</groupId>
|
||||
<artifactId>kernel-a-rule</artifactId>
|
||||
<version>${roses.version}</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
</project>
|
4
pom.xml
4
pom.xml
|
@ -103,6 +103,9 @@
|
|||
<!--用户收藏模块-->
|
||||
<module>kernel-s-user-favorite</module>
|
||||
|
||||
<!--分布式锁-->
|
||||
<module>kernel-d-lock</module>
|
||||
|
||||
</modules>
|
||||
|
||||
<properties>
|
||||
|
@ -143,6 +146,7 @@
|
|||
<openfeign.version>2.2.6.RELEASE</openfeign.version>
|
||||
<bcprov.version>1.78.1</bcprov.version>
|
||||
<flyway.version>7.1.1</flyway.version>
|
||||
<redisson.version>3.18.0</redisson.version>
|
||||
</properties>
|
||||
|
||||
<dependencyManagement>
|
||||
|
|
Loading…
Reference in New Issue