mirror of
https://github.com/jeecgboot/JeecgBoot.git
synced 2026-02-05 10:05:33 +08:00
JeecgBoot 2.4 微服务正式版本发布,基于SpringBoot的低代码平台
This commit is contained in:
@ -0,0 +1,54 @@
|
||||
package org.jeecg.boot.starter.lock.annotation;
|
||||
|
||||
import java.lang.annotation.*;
|
||||
|
||||
/**
|
||||
* Redisson分布式锁注解
|
||||
*
|
||||
* @author zyf
|
||||
* @date 2020-11-11
|
||||
*/
|
||||
@Target({ElementType.TYPE, ElementType.METHOD})
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
@Documented
|
||||
@Inherited
|
||||
public @interface DistributedLock {
|
||||
|
||||
/**
|
||||
* 要锁的参数索引
|
||||
*/
|
||||
int[] fieldIndexs() default {};
|
||||
|
||||
/**
|
||||
* 要锁的参数的属性名
|
||||
*/
|
||||
String[] fieldNames() default {};
|
||||
|
||||
/**
|
||||
* 分布式锁名称
|
||||
*
|
||||
* @return String
|
||||
*/
|
||||
String lockKey() default "";
|
||||
|
||||
/**
|
||||
* 锁超时时间(单位:秒) 如果超过还没有解锁的话,就强制解锁
|
||||
*
|
||||
* @return int
|
||||
*/
|
||||
int expireSeconds() default 10;
|
||||
|
||||
/**
|
||||
* 等待多久(单位:秒)-1 则表示一直等待
|
||||
*
|
||||
* @return int
|
||||
*/
|
||||
int waitTime() default 5;
|
||||
|
||||
/**
|
||||
* 未取到锁时提示信息
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
String failMsg() default "获取锁失败,请稍后重试";
|
||||
}
|
||||
@ -0,0 +1,104 @@
|
||||
package org.jeecg.boot.starter.lock.aspect;
|
||||
|
||||
import lombok.SneakyThrows;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.apache.commons.beanutils.PropertyUtils;
|
||||
import org.apache.commons.lang3.ArrayUtils;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.aspectj.lang.ProceedingJoinPoint;
|
||||
import org.aspectj.lang.annotation.Around;
|
||||
import org.aspectj.lang.annotation.Aspect;
|
||||
import org.jeecg.boot.starter.lock.annotation.DistributedLock;
|
||||
import org.jeecg.boot.starter.lock.client.RedissonLockClient;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
|
||||
/**
|
||||
* 分布式锁解析器
|
||||
*
|
||||
* @author zyf
|
||||
* @date 2020-11-11
|
||||
*/
|
||||
@Slf4j
|
||||
@Aspect
|
||||
@Component
|
||||
public class DistributedLockHandler {
|
||||
|
||||
@Autowired
|
||||
RedissonLockClient redissonLock;
|
||||
|
||||
/**
|
||||
* 切面环绕通知
|
||||
*
|
||||
* @param joinPoint
|
||||
* @param distributedLock
|
||||
* @return Object
|
||||
*/
|
||||
@Around("@annotation(distributedLock)")
|
||||
public Object around(ProceedingJoinPoint joinPoint, DistributedLock distributedLock) {
|
||||
log.info("进入RedisLock环绕通知...");
|
||||
Object obj = null;
|
||||
|
||||
//获取锁名称
|
||||
String lockName = getLockKey(joinPoint, distributedLock);
|
||||
if (StringUtils.isEmpty(lockName)) {
|
||||
return null;
|
||||
}
|
||||
//获取超时时间
|
||||
int expireSeconds = distributedLock.expireSeconds();
|
||||
//等待多久,n秒内获取不到锁,则直接返回
|
||||
int waitTime = distributedLock.waitTime();
|
||||
Boolean success = redissonLock.tryLock(lockName, waitTime, expireSeconds);
|
||||
if (success) {
|
||||
log.info("获取锁成功....");
|
||||
try {
|
||||
obj = joinPoint.proceed();
|
||||
} catch (Throwable throwable) {
|
||||
log.error("获取锁异常", throwable);
|
||||
} finally {
|
||||
//释放锁
|
||||
redissonLock.unlock(lockName);
|
||||
log.info("成功释放锁...");
|
||||
}
|
||||
} else {
|
||||
log.error("获取锁失败", distributedLock.failMsg());
|
||||
}
|
||||
log.info("结束RedisLock环绕通知...");
|
||||
return obj;
|
||||
}
|
||||
|
||||
@SneakyThrows
|
||||
private String getLockKey(ProceedingJoinPoint joinPoint, DistributedLock distributedLock) {
|
||||
String lockKey = distributedLock.lockKey();
|
||||
if (StringUtils.isEmpty(lockKey)) {
|
||||
int[] fieldIndexs = distributedLock.fieldIndexs();
|
||||
String[] fieldNames = distributedLock.fieldNames();
|
||||
//目标方法内的所有参数
|
||||
Object[] params = joinPoint.getArgs();
|
||||
//获取目标包名和类名
|
||||
String declaringTypeName = joinPoint.getSignature().getDeclaringTypeName();
|
||||
//获取目标方法名
|
||||
String methodName = joinPoint.getSignature().getName();
|
||||
// 锁2个及2个以上参数时,fieldNames数量应与fieldIndexs一致
|
||||
if (fieldNames.length > 1 && fieldIndexs.length != fieldNames.length) {
|
||||
log.error("fieldIndexs与fieldNames数量不一致");
|
||||
return null;
|
||||
}
|
||||
// 数组为空代表锁整个方法
|
||||
if (ArrayUtils.isNotEmpty(fieldNames)) {
|
||||
StringBuffer lockParamsBuffer = new StringBuffer();
|
||||
for (int i = 0; i < fieldIndexs.length; i++) {
|
||||
if (fieldNames.length == 0 || fieldNames[i] == null || fieldNames[i].length() == 0) {
|
||||
lockParamsBuffer.append("." + params[fieldIndexs[i]]);
|
||||
} else {
|
||||
Object lockParamValue = PropertyUtils.getSimpleProperty(params[fieldIndexs[i]], fieldNames[i]);
|
||||
lockParamsBuffer.append("." + lockParamValue);
|
||||
}
|
||||
}
|
||||
lockKey = declaringTypeName + "." + methodName + lockParamsBuffer.toString();
|
||||
}
|
||||
}
|
||||
return lockKey;
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,103 @@
|
||||
package org.jeecg.boot.starter.lock.client;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Getter;
|
||||
import lombok.NoArgsConstructor;
|
||||
import lombok.Setter;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.jeecg.boot.starter.lock.core.RedissonManager;
|
||||
import org.redisson.api.RLock;
|
||||
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
/**
|
||||
* 分布式锁实现基于Redisson
|
||||
*
|
||||
* @author zyf
|
||||
* @date 2020-11-11
|
||||
*/
|
||||
@Slf4j
|
||||
@AllArgsConstructor
|
||||
@NoArgsConstructor
|
||||
@Getter
|
||||
@Setter
|
||||
public class RedissonLockClient {
|
||||
|
||||
RedissonManager redissonManager;
|
||||
|
||||
/**
|
||||
* 获取锁
|
||||
*/
|
||||
public RLock getLock(String lockKey) {
|
||||
return redissonManager.getRedisson().getLock(lockKey);
|
||||
}
|
||||
|
||||
/**
|
||||
* 加锁操作
|
||||
*
|
||||
* @return boolean
|
||||
*/
|
||||
public boolean tryLock(String lockName, long expireSeconds) {
|
||||
return tryLock(lockName, 0, expireSeconds);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 加锁操作
|
||||
*
|
||||
* @return boolean
|
||||
*/
|
||||
public boolean tryLock(String lockName, long waitTime, long expireSeconds) {
|
||||
RLock rLock = getLock(lockName);
|
||||
boolean getLock = false;
|
||||
try {
|
||||
getLock = rLock.tryLock(waitTime, expireSeconds, TimeUnit.SECONDS);
|
||||
if (getLock) {
|
||||
log.info("获取锁成功,lockName={}", lockName);
|
||||
} else {
|
||||
log.info("获取锁失败,lockName={}", lockName);
|
||||
}
|
||||
} catch (InterruptedException e) {
|
||||
log.error("获取式锁异常,lockName=" + lockName, e);
|
||||
getLock = false;
|
||||
}
|
||||
return getLock;
|
||||
}
|
||||
|
||||
/**
|
||||
* 锁lockKey
|
||||
*
|
||||
* @param lockKey
|
||||
* @return
|
||||
*/
|
||||
public RLock lock(String lockKey) {
|
||||
RLock lock = getLock(lockKey);
|
||||
lock.lock();
|
||||
return lock;
|
||||
}
|
||||
|
||||
/**
|
||||
* 锁lockKey
|
||||
*
|
||||
* @param lockKey
|
||||
* @param leaseTime
|
||||
* @return
|
||||
*/
|
||||
public RLock lock(String lockKey, long leaseTime) {
|
||||
RLock lock = getLock(lockKey);
|
||||
lock.lock(leaseTime, TimeUnit.SECONDS);
|
||||
return lock;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 解锁
|
||||
*
|
||||
* @param lockName 锁名称
|
||||
*/
|
||||
public void unlock(String lockName) {
|
||||
redissonManager.getRedisson().getLock(lockName).unlock();
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@ -0,0 +1,47 @@
|
||||
package org.jeecg.boot.starter.lock.config;
|
||||
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.jeecg.boot.starter.lock.client.RedissonLockClient;
|
||||
import org.jeecg.boot.starter.lock.core.RedissonManager;
|
||||
import org.jeecg.boot.starter.lock.prop.RedissonProperties;
|
||||
import org.redisson.Redisson;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
|
||||
import org.springframework.boot.context.properties.EnableConfigurationProperties;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.core.annotation.Order;
|
||||
|
||||
|
||||
/**
|
||||
* Redisson自动化配置
|
||||
*
|
||||
* @author zyf
|
||||
* @date 2020-11-11
|
||||
*/
|
||||
@Slf4j
|
||||
@Configuration
|
||||
@ConditionalOnClass(Redisson.class)
|
||||
@EnableConfigurationProperties(RedissonProperties.class)
|
||||
public class RedissonConfiguration {
|
||||
|
||||
|
||||
@Bean
|
||||
@ConditionalOnMissingBean
|
||||
@Order(value = 1)
|
||||
public RedissonManager redissonManager(RedissonProperties redissonProperties) {
|
||||
RedissonManager redissonManager = new RedissonManager(redissonProperties);
|
||||
log.info("RedissonManager初始化完成,当前连接方式:" + redissonProperties.getType() + ",连接地址:" + redissonProperties.getAddress());
|
||||
return redissonManager;
|
||||
}
|
||||
|
||||
@Bean
|
||||
@ConditionalOnMissingBean
|
||||
@Order(value = 2)
|
||||
public RedissonLockClient redissonLock(RedissonManager redissonManager) {
|
||||
RedissonLockClient redissonLock = new RedissonLockClient();
|
||||
redissonLock.setRedissonManager(redissonManager);
|
||||
log.info("RedissonLock初始化完成");
|
||||
return redissonLock;
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,100 @@
|
||||
package org.jeecg.boot.starter.lock.core;
|
||||
|
||||
|
||||
import com.google.common.base.Preconditions;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.jeecg.boot.starter.lock.core.strategy.RedissonConfigStrategy;
|
||||
import org.jeecg.boot.starter.lock.prop.RedissonProperties;
|
||||
import org.jeecg.boot.starter.lock.core.strategy.impl.ClusterRedissonConfigStrategyImpl;
|
||||
import org.jeecg.boot.starter.lock.core.strategy.impl.MasterslaveRedissonConfigStrategyImpl;
|
||||
import org.jeecg.boot.starter.lock.core.strategy.impl.SentinelRedissonConfigStrategyImpl;
|
||||
import org.jeecg.boot.starter.lock.core.strategy.impl.StandaloneRedissonConfigStrategyImpl;
|
||||
import org.jeecg.boot.starter.lock.enums.RedisConnectionType;
|
||||
import org.redisson.Redisson;
|
||||
import org.redisson.config.Config;
|
||||
|
||||
|
||||
/**
|
||||
* Redisson配置管理器,用于初始化的redisson实例
|
||||
*
|
||||
* @author zyf
|
||||
* @date 2020-11-12
|
||||
*/
|
||||
@Slf4j
|
||||
public class RedissonManager {
|
||||
|
||||
private Config config = new Config();
|
||||
|
||||
private Redisson redisson = null;
|
||||
|
||||
public RedissonManager() {
|
||||
}
|
||||
|
||||
public RedissonManager(RedissonProperties redissonProperties) {
|
||||
//装配开关
|
||||
Boolean enabled = redissonProperties.getEnabled();
|
||||
if (enabled) {
|
||||
try {
|
||||
config = RedissonConfigFactory.getInstance().createConfig(redissonProperties);
|
||||
redisson = (Redisson) Redisson.create(config);
|
||||
} catch (Exception e) {
|
||||
log.error("Redisson初始化错误", e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public Redisson getRedisson() {
|
||||
return redisson;
|
||||
}
|
||||
|
||||
/**
|
||||
* Redisson连接方式配置工厂
|
||||
* 双重检查锁
|
||||
*/
|
||||
static class RedissonConfigFactory {
|
||||
|
||||
private RedissonConfigFactory() {
|
||||
}
|
||||
|
||||
private static volatile RedissonConfigFactory factory = null;
|
||||
|
||||
public static RedissonConfigFactory getInstance() {
|
||||
if (factory == null) {
|
||||
synchronized (Object.class) {
|
||||
if (factory == null) {
|
||||
factory = new RedissonConfigFactory();
|
||||
}
|
||||
}
|
||||
}
|
||||
return factory;
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据连接类型創建连接方式的配置
|
||||
*
|
||||
* @param redissonProperties
|
||||
* @return Config
|
||||
*/
|
||||
Config createConfig(RedissonProperties redissonProperties) {
|
||||
Preconditions.checkNotNull(redissonProperties);
|
||||
Preconditions.checkNotNull(redissonProperties.getAddress(), "redis地址未配置");
|
||||
RedisConnectionType connectionType = redissonProperties.getType();
|
||||
// 声明连接方式
|
||||
RedissonConfigStrategy redissonConfigStrategy;
|
||||
if (connectionType.equals(RedisConnectionType.SENTINEL)) {
|
||||
redissonConfigStrategy = new SentinelRedissonConfigStrategyImpl();
|
||||
} else if (connectionType.equals(RedisConnectionType.CLUSTER)) {
|
||||
redissonConfigStrategy = new ClusterRedissonConfigStrategyImpl();
|
||||
} else if (connectionType.equals(RedisConnectionType.MASTERSLAVE)) {
|
||||
redissonConfigStrategy = new MasterslaveRedissonConfigStrategyImpl();
|
||||
} else {
|
||||
redissonConfigStrategy = new StandaloneRedissonConfigStrategyImpl();
|
||||
}
|
||||
Preconditions.checkNotNull(redissonConfigStrategy, "连接方式创建异常");
|
||||
|
||||
return redissonConfigStrategy.createRedissonConfig(redissonProperties);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@ -0,0 +1,21 @@
|
||||
package org.jeecg.boot.starter.lock.core.strategy;
|
||||
|
||||
import org.jeecg.boot.starter.lock.prop.RedissonProperties;
|
||||
import org.redisson.config.Config;
|
||||
|
||||
/**
|
||||
* Redisson配置构建接口
|
||||
*
|
||||
* @author zyf
|
||||
* @date 2020-11-11
|
||||
*/
|
||||
public interface RedissonConfigStrategy {
|
||||
|
||||
/**
|
||||
* 根据不同的Redis配置策略创建对应的Config
|
||||
*
|
||||
* @param redissonProperties
|
||||
* @return Config
|
||||
*/
|
||||
Config createRedissonConfig(RedissonProperties redissonProperties);
|
||||
}
|
||||
@ -0,0 +1,43 @@
|
||||
package org.jeecg.boot.starter.lock.core.strategy.impl;
|
||||
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.jeecg.boot.starter.lock.core.strategy.RedissonConfigStrategy;
|
||||
import org.jeecg.boot.starter.lock.prop.RedissonProperties;
|
||||
import org.jeecg.boot.starter.lock.enums.GlobalConstant;
|
||||
import org.redisson.config.Config;
|
||||
|
||||
|
||||
/**
|
||||
* 集群方式Redisson配置
|
||||
* cluster方式至少6个节点(3主3从)
|
||||
* 配置方式:127.0.0.1:6379,127.0.0.1:6380,127.0.0.1:6381,127.0.0.1:6382,127.0.0.1:6383,127.0.0.1:6384
|
||||
*
|
||||
* @author zyf
|
||||
* @date 2020-11-11
|
||||
*/
|
||||
@Slf4j
|
||||
public class ClusterRedissonConfigStrategyImpl implements RedissonConfigStrategy {
|
||||
|
||||
@Override
|
||||
public Config createRedissonConfig(RedissonProperties redissonProperties) {
|
||||
Config config = new Config();
|
||||
try {
|
||||
String address = redissonProperties.getAddress();
|
||||
String password = redissonProperties.getPassword();
|
||||
String[] addrTokens = address.split(",");
|
||||
// 设置集群(cluster)节点的服务IP和端口
|
||||
for (int i = 0; i < addrTokens.length; i++) {
|
||||
config.useClusterServers().addNodeAddress(GlobalConstant.REDIS_CONNECTION_PREFIX + addrTokens[i]);
|
||||
if (StringUtils.isNotBlank(password)) {
|
||||
config.useClusterServers().setPassword(password);
|
||||
}
|
||||
}
|
||||
log.info("初始化集群方式Config,连接地址:" + address);
|
||||
} catch (Exception e) {
|
||||
log.error("集群Redisson初始化错误", e);
|
||||
e.printStackTrace();
|
||||
}
|
||||
return config;
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,54 @@
|
||||
package org.jeecg.boot.starter.lock.core.strategy.impl;
|
||||
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.jeecg.boot.starter.lock.core.strategy.RedissonConfigStrategy;
|
||||
import org.jeecg.boot.starter.lock.prop.RedissonProperties;
|
||||
import org.jeecg.boot.starter.lock.enums.GlobalConstant;
|
||||
import org.redisson.config.Config;
|
||||
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* 主从方式Redisson配置
|
||||
* <p>配置方式: 127.0.0.1:6379(主),127.0.0.1:6380(子),127.0.0.1:6381(子)</p>
|
||||
*
|
||||
* @author zyf
|
||||
* @date 2020-11-11
|
||||
*/
|
||||
@Slf4j
|
||||
public class MasterslaveRedissonConfigStrategyImpl implements RedissonConfigStrategy {
|
||||
|
||||
@Override
|
||||
public Config createRedissonConfig(RedissonProperties redissonProperties) {
|
||||
Config config = new Config();
|
||||
try {
|
||||
String address = redissonProperties.getAddress();
|
||||
String password = redissonProperties.getPassword();
|
||||
int database = redissonProperties.getDatabase();
|
||||
String[] addrTokens = address.split(",");
|
||||
String masterNodeAddr = addrTokens[0];
|
||||
// 设置主节点ip
|
||||
config.useMasterSlaveServers().setMasterAddress(masterNodeAddr);
|
||||
if (StringUtils.isNotBlank(password)) {
|
||||
config.useMasterSlaveServers().setPassword(password);
|
||||
}
|
||||
config.useMasterSlaveServers().setDatabase(database);
|
||||
// 设置从节点,移除第一个节点,默认第一个为主节点
|
||||
List<String> slaveList = new ArrayList<>();
|
||||
for (String addrToken : addrTokens) {
|
||||
slaveList.add(GlobalConstant.REDIS_CONNECTION_PREFIX + addrToken);
|
||||
}
|
||||
slaveList.remove(0);
|
||||
|
||||
config.useMasterSlaveServers().addSlaveAddress((String[]) slaveList.toArray());
|
||||
log.info("初始化主从方式Config,redisAddress:" + address);
|
||||
} catch (Exception e) {
|
||||
log.error("主从Redisson初始化错误", e);
|
||||
e.printStackTrace();
|
||||
}
|
||||
return config;
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,47 @@
|
||||
package org.jeecg.boot.starter.lock.core.strategy.impl;
|
||||
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.jeecg.boot.starter.lock.core.strategy.RedissonConfigStrategy;
|
||||
import org.jeecg.boot.starter.lock.prop.RedissonProperties;
|
||||
import org.jeecg.boot.starter.lock.enums.GlobalConstant;
|
||||
import org.redisson.config.Config;
|
||||
|
||||
|
||||
/**
|
||||
* 哨兵方式Redis连接配置
|
||||
* 比如sentinel.conf里配置为sentinel monitor my-sentinel-name 127.0.0.1 6379 2,那么这里就配置my-sentinel-name
|
||||
* 配置方式:my-sentinel-name,127.0.0.1:26379,127.0.0.1:26389,127.0.0.1:26399
|
||||
* @author zyf
|
||||
* @date 2020-11-11
|
||||
*/
|
||||
@Slf4j
|
||||
public class SentinelRedissonConfigStrategyImpl implements RedissonConfigStrategy {
|
||||
|
||||
@Override
|
||||
public Config createRedissonConfig(RedissonProperties redissonProperties) {
|
||||
Config config = new Config();
|
||||
try {
|
||||
String address = redissonProperties.getAddress();
|
||||
String password = redissonProperties.getPassword();
|
||||
int database = redissonProperties.getDatabase();
|
||||
String[] addrTokens = address.split(",");
|
||||
String sentinelAliasName = addrTokens[0];
|
||||
// 设置redis配置文件sentinel.conf配置的sentinel别名
|
||||
config.useSentinelServers().setMasterName(sentinelAliasName);
|
||||
config.useSentinelServers().setDatabase(database);
|
||||
if (StringUtils.isNotBlank(password)) {
|
||||
config.useSentinelServers().setPassword(password);
|
||||
}
|
||||
// 设置哨兵节点的服务IP和端口
|
||||
for (int i = 1; i < addrTokens.length; i++) {
|
||||
config.useSentinelServers().addSentinelAddress(GlobalConstant.REDIS_CONNECTION_PREFIX+ addrTokens[i]);
|
||||
}
|
||||
log.info("初始化哨兵方式Config,redisAddress:" + address);
|
||||
} catch (Exception e) {
|
||||
log.error("哨兵Redisson初始化错误", e);
|
||||
e.printStackTrace();
|
||||
}
|
||||
return config;
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,40 @@
|
||||
package org.jeecg.boot.starter.lock.core.strategy.impl;
|
||||
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.jeecg.boot.starter.lock.core.strategy.RedissonConfigStrategy;
|
||||
import org.jeecg.boot.starter.lock.prop.RedissonProperties;
|
||||
import org.jeecg.boot.starter.lock.enums.GlobalConstant;
|
||||
import org.redisson.config.Config;
|
||||
|
||||
|
||||
/**
|
||||
* 单机方式Redisson配置
|
||||
*
|
||||
* @author zyf
|
||||
* @date 2020-11-11
|
||||
*/
|
||||
@Slf4j
|
||||
public class StandaloneRedissonConfigStrategyImpl implements RedissonConfigStrategy {
|
||||
|
||||
@Override
|
||||
public Config createRedissonConfig(RedissonProperties redissonProperties) {
|
||||
Config config = new Config();
|
||||
try {
|
||||
String address = redissonProperties.getAddress();
|
||||
String password = redissonProperties.getPassword();
|
||||
int database = redissonProperties.getDatabase();
|
||||
String redisAddr = GlobalConstant.REDIS_CONNECTION_PREFIX + address;
|
||||
config.useSingleServer().setAddress(redisAddr);
|
||||
config.useSingleServer().setDatabase(database);
|
||||
if (StringUtils.isNotBlank(password)) {
|
||||
config.useSingleServer().setPassword(password);
|
||||
}
|
||||
log.info("初始化Redisson单机配置,连接地址:" + address);
|
||||
} catch (Exception e) {
|
||||
log.error("单机Redisson初始化错误", e);
|
||||
e.printStackTrace();
|
||||
}
|
||||
return config;
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,17 @@
|
||||
package org.jeecg.boot.starter.lock.enums;
|
||||
|
||||
/**
|
||||
* 全局常量枚举
|
||||
*
|
||||
* @author zyf
|
||||
* @date 2020-11-11
|
||||
*/
|
||||
|
||||
public interface GlobalConstant {
|
||||
|
||||
/**
|
||||
* Redis地址连接前缀
|
||||
*/
|
||||
String REDIS_CONNECTION_PREFIX = "redis://";
|
||||
|
||||
}
|
||||
@ -0,0 +1,39 @@
|
||||
package org.jeecg.boot.starter.lock.enums;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Getter;
|
||||
|
||||
/**
|
||||
* Redis连接方式
|
||||
* @author zyf
|
||||
* @date 2020-11-11
|
||||
*/
|
||||
@Getter
|
||||
@AllArgsConstructor
|
||||
public enum RedisConnectionType {
|
||||
/**
|
||||
* 单机部署方式(默认)
|
||||
*/
|
||||
STANDALONE("standalone", "单机部署方式"),
|
||||
/**
|
||||
* 哨兵部署方式
|
||||
*/
|
||||
SENTINEL("sentinel", "哨兵部署方式"),
|
||||
/**
|
||||
* 集群部署方式
|
||||
*/
|
||||
CLUSTER("cluster", "集群方式"),
|
||||
/**
|
||||
* 主从部署方式
|
||||
*/
|
||||
MASTERSLAVE("masterslave", "主从部署方式");
|
||||
|
||||
/**
|
||||
* 编码
|
||||
*/
|
||||
private final String code;
|
||||
/**
|
||||
* 名称
|
||||
*/
|
||||
private final String name;
|
||||
}
|
||||
@ -0,0 +1,39 @@
|
||||
package org.jeecg.boot.starter.lock.prop;
|
||||
|
||||
import lombok.Data;
|
||||
import org.jeecg.boot.starter.lock.enums.RedisConnectionType;
|
||||
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||
|
||||
/**
|
||||
* Redisson配置映射类
|
||||
*
|
||||
* @author zyf
|
||||
* @date 2020-11-11
|
||||
*/
|
||||
@Data
|
||||
@ConfigurationProperties(prefix = "jeecg.redisson")
|
||||
public class RedissonProperties {
|
||||
|
||||
/**
|
||||
* redis主机地址,ip:port,多个用逗号(,)分隔
|
||||
*/
|
||||
private String address;
|
||||
/**
|
||||
* 连接类型
|
||||
*/
|
||||
private RedisConnectionType type;
|
||||
/**
|
||||
* 密码
|
||||
*/
|
||||
private String password;
|
||||
/**
|
||||
* 数据库(默认0)
|
||||
*/
|
||||
private int database;
|
||||
|
||||
/**
|
||||
* 是否装配redisson配置
|
||||
*/
|
||||
private Boolean enabled = true;
|
||||
|
||||
}
|
||||
@ -0,0 +1,4 @@
|
||||
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
|
||||
org.jeecg.boot.starter.lock.config.RedissonConfiguration
|
||||
|
||||
|
||||
Reference in New Issue
Block a user