mirror of
https://github.com/continew-org/continew-starter.git
synced 2025-09-08 16:57:09 +08:00
refactor: 优化部分代码
1.修复 Sonar 扫描问题 2.使用常量替代部分魔法值
This commit is contained in:
@@ -24,6 +24,7 @@ import lombok.RequiredArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.redisson.codec.JsonJacksonCodec;
|
||||
import org.redisson.config.ClusterServersConfig;
|
||||
import org.redisson.config.Config;
|
||||
import org.redisson.config.SentinelServersConfig;
|
||||
import org.redisson.config.SingleServerConfig;
|
||||
import org.redisson.spring.starter.RedissonAutoConfigurationCustomizer;
|
||||
@@ -32,6 +33,7 @@ import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
|
||||
import org.springframework.boot.autoconfigure.data.redis.RedisProperties;
|
||||
import org.springframework.boot.context.properties.EnableConfigurationProperties;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import top.charles7c.continew.starter.core.constant.PropertiesConstants;
|
||||
import top.charles7c.continew.starter.core.constant.StringConstants;
|
||||
|
||||
import java.util.List;
|
||||
@@ -46,7 +48,7 @@ import java.util.List;
|
||||
@Slf4j
|
||||
@AutoConfiguration
|
||||
@RequiredArgsConstructor
|
||||
@ConditionalOnProperty(prefix = "spring.data.redisson", name = "enabled", havingValue = "true")
|
||||
@ConditionalOnProperty(prefix = "spring.data.redisson", name = PropertiesConstants.ENABLED, havingValue = "true")
|
||||
@EnableConfigurationProperties(RedissonProperties.class)
|
||||
public class RedissonAutoConfiguration {
|
||||
|
||||
@@ -60,63 +62,85 @@ public class RedissonAutoConfiguration {
|
||||
RedissonProperties.Mode mode = properties.getMode();
|
||||
String protocol = redisProperties.getSsl().isEnabled() ? "rediss://" : "redis://";
|
||||
switch (mode) {
|
||||
case CLUSTER -> {
|
||||
ClusterServersConfig clusterServersConfig = config.useClusterServers();
|
||||
ClusterServersConfig customClusterServersConfig = properties.getClusterServersConfig();
|
||||
if (null != customClusterServersConfig) {
|
||||
BeanUtil.copyProperties(customClusterServersConfig, clusterServersConfig);
|
||||
clusterServersConfig.setNodeAddresses(customClusterServersConfig.getNodeAddresses());
|
||||
}
|
||||
// 下方配置如果为空,则使用 Redis 的配置
|
||||
if (CollUtil.isEmpty(clusterServersConfig.getNodeAddresses())) {
|
||||
List<String> nodeList = redisProperties.getCluster().getNodes();
|
||||
nodeList.stream().map(node -> protocol + node).forEach(clusterServersConfig::addNodeAddress);
|
||||
}
|
||||
if (StrUtil.isBlank(clusterServersConfig.getPassword())) {
|
||||
clusterServersConfig.setPassword(redisProperties.getPassword());
|
||||
}
|
||||
}
|
||||
case SENTINEL -> {
|
||||
SentinelServersConfig sentinelServersConfig = config.useSentinelServers();
|
||||
SentinelServersConfig customSentinelServersConfig = properties.getSentinelServersConfig();
|
||||
if (null != customSentinelServersConfig) {
|
||||
BeanUtil.copyProperties(customSentinelServersConfig, sentinelServersConfig);
|
||||
sentinelServersConfig.setSentinelAddresses(customSentinelServersConfig.getSentinelAddresses());
|
||||
}
|
||||
// 下方配置如果为空,则使用 Redis 的配置
|
||||
if (CollUtil.isEmpty(sentinelServersConfig.getSentinelAddresses())) {
|
||||
List<String> nodeList = redisProperties.getSentinel().getNodes();
|
||||
nodeList.stream()
|
||||
.map(node -> protocol + node)
|
||||
.forEach(sentinelServersConfig::addSentinelAddress);
|
||||
}
|
||||
if (StrUtil.isBlank(sentinelServersConfig.getPassword())) {
|
||||
sentinelServersConfig.setPassword(redisProperties.getPassword());
|
||||
}
|
||||
if (StrUtil.isBlank(sentinelServersConfig.getMasterName())) {
|
||||
sentinelServersConfig.setMasterName(redisProperties.getSentinel().getMaster());
|
||||
}
|
||||
}
|
||||
default -> {
|
||||
SingleServerConfig singleServerConfig = config.useSingleServer();
|
||||
SingleServerConfig customSingleServerConfig = properties.getSingleServerConfig();
|
||||
if (null != customSingleServerConfig) {
|
||||
BeanUtil.copyProperties(properties.getSingleServerConfig(), singleServerConfig);
|
||||
}
|
||||
// 下方配置如果为空,则使用 Redis 的配置
|
||||
singleServerConfig.setDatabase(redisProperties.getDatabase());
|
||||
if (StrUtil.isBlank(singleServerConfig.getPassword())) {
|
||||
singleServerConfig.setPassword(redisProperties.getPassword());
|
||||
}
|
||||
if (StrUtil.isBlank(singleServerConfig.getAddress())) {
|
||||
singleServerConfig.setAddress(protocol + redisProperties
|
||||
.getHost() + StringConstants.COLON + redisProperties.getPort());
|
||||
}
|
||||
}
|
||||
case CLUSTER -> this.buildClusterModeConfig(config, protocol);
|
||||
case SENTINEL -> this.buildSentinelModeConfig(config, protocol);
|
||||
default -> this.buildSingleModeConfig(config, protocol);
|
||||
}
|
||||
// Jackson 处理
|
||||
config.setCodec(new JsonJacksonCodec(objectMapper));
|
||||
log.debug("[ContiNew Starter] - Auto Configuration 'Redisson' completed initialization.");
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* 构建集群模式配置
|
||||
*
|
||||
* @param config 配置
|
||||
* @param protocol 协议
|
||||
*/
|
||||
private void buildClusterModeConfig(Config config, String protocol) {
|
||||
ClusterServersConfig clusterServersConfig = config.useClusterServers();
|
||||
ClusterServersConfig customClusterServersConfig = properties.getClusterServersConfig();
|
||||
if (null != customClusterServersConfig) {
|
||||
BeanUtil.copyProperties(customClusterServersConfig, clusterServersConfig);
|
||||
clusterServersConfig.setNodeAddresses(customClusterServersConfig.getNodeAddresses());
|
||||
}
|
||||
// 下方配置如果为空,则使用 Redis 的配置
|
||||
if (CollUtil.isEmpty(clusterServersConfig.getNodeAddresses())) {
|
||||
List<String> nodeList = redisProperties.getCluster().getNodes();
|
||||
nodeList.stream().map(node -> protocol + node).forEach(clusterServersConfig::addNodeAddress);
|
||||
}
|
||||
if (StrUtil.isBlank(clusterServersConfig.getPassword())) {
|
||||
clusterServersConfig.setPassword(redisProperties.getPassword());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 构建哨兵模式配置
|
||||
*
|
||||
* @param config 配置
|
||||
* @param protocol 协议
|
||||
*/
|
||||
private void buildSentinelModeConfig(Config config, String protocol) {
|
||||
SentinelServersConfig sentinelServersConfig = config.useSentinelServers();
|
||||
SentinelServersConfig customSentinelServersConfig = properties.getSentinelServersConfig();
|
||||
if (null != customSentinelServersConfig) {
|
||||
BeanUtil.copyProperties(customSentinelServersConfig, sentinelServersConfig);
|
||||
sentinelServersConfig.setSentinelAddresses(customSentinelServersConfig.getSentinelAddresses());
|
||||
}
|
||||
// 下方配置如果为空,则使用 Redis 的配置
|
||||
if (CollUtil.isEmpty(sentinelServersConfig.getSentinelAddresses())) {
|
||||
List<String> nodeList = redisProperties.getSentinel().getNodes();
|
||||
nodeList.stream().map(node -> protocol + node).forEach(sentinelServersConfig::addSentinelAddress);
|
||||
}
|
||||
if (StrUtil.isBlank(sentinelServersConfig.getPassword())) {
|
||||
sentinelServersConfig.setPassword(redisProperties.getPassword());
|
||||
}
|
||||
if (StrUtil.isBlank(sentinelServersConfig.getMasterName())) {
|
||||
sentinelServersConfig.setMasterName(redisProperties.getSentinel().getMaster());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 构建单机模式配置
|
||||
*
|
||||
* @param config 配置
|
||||
* @param protocol 协议
|
||||
*/
|
||||
private void buildSingleModeConfig(Config config, String protocol) {
|
||||
SingleServerConfig singleServerConfig = config.useSingleServer();
|
||||
SingleServerConfig customSingleServerConfig = properties.getSingleServerConfig();
|
||||
if (null != customSingleServerConfig) {
|
||||
BeanUtil.copyProperties(properties.getSingleServerConfig(), singleServerConfig);
|
||||
}
|
||||
// 下方配置如果为空,则使用 Redis 的配置
|
||||
singleServerConfig.setDatabase(redisProperties.getDatabase());
|
||||
if (StrUtil.isBlank(singleServerConfig.getPassword())) {
|
||||
singleServerConfig.setPassword(redisProperties.getPassword());
|
||||
}
|
||||
if (StrUtil.isBlank(singleServerConfig.getAddress())) {
|
||||
singleServerConfig.setAddress(protocol + redisProperties.getHost() + StringConstants.COLON + redisProperties
|
||||
.getPort());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user