【8.1.9】【lock】初始化分布式锁

dev-8.1.9
stylefeng 2024-09-19 21:54:08 +08:00
parent 6c4f952841
commit 3188a40da3
13 changed files with 403 additions and 0 deletions

1
kernel-d-lock/README.md Normal file
View File

@ -0,0 +1 @@
锁模块-提供分布式锁的支持

View File

@ -0,0 +1 @@
锁模块-分布式锁的接口

View File

@ -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>

View File

@ -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();
}

View File

@ -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);
}

View File

@ -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>

View File

@ -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);
}
}

View File

@ -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();
}
}

View File

@ -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>

View File

@ -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);
}
}

View File

@ -0,0 +1,2 @@
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
cn.stylefeng.roses.kernel.lock.starter.ProjectLockCacheAutoConfiguration

31
kernel-d-lock/pom.xml Normal file
View File

@ -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>

View File

@ -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>