From acb88a6b2a5ca56e0de457ab141053eab865f9c2 Mon Sep 17 00:00:00 2001 From: paulGao Date: Fri, 11 Feb 2022 14:54:56 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96redisson=E9=85=8D=E7=BD=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- buyer-api/src/main/resources/redisson.yaml | 21 -------- common-api/src/main/resources/redisson.yaml | 21 -------- consumer/src/main/resources/redisson.yaml | 21 -------- .../lili/cache/config/redis/RedisConfig.java | 50 +++++++++++++++++-- manager-api/src/main/resources/redisson.yaml | 21 -------- seller-api/src/main/resources/redisson.yaml | 21 -------- 6 files changed, 45 insertions(+), 110 deletions(-) delete mode 100644 buyer-api/src/main/resources/redisson.yaml delete mode 100644 common-api/src/main/resources/redisson.yaml delete mode 100644 consumer/src/main/resources/redisson.yaml delete mode 100644 manager-api/src/main/resources/redisson.yaml delete mode 100644 seller-api/src/main/resources/redisson.yaml diff --git a/buyer-api/src/main/resources/redisson.yaml b/buyer-api/src/main/resources/redisson.yaml deleted file mode 100644 index ac8dfc66..00000000 --- a/buyer-api/src/main/resources/redisson.yaml +++ /dev/null @@ -1,21 +0,0 @@ ---- -singleServerConfig: - idleConnectionTimeout: 10000 - connectTimeout: 10000 - timeout: 3000 - retryAttempts: 3 - retryInterval: 1500 - password: lilishop - subscriptionsPerConnection: 5 - clientName: null - address: "redis://127.0.0.1:6379" - subscriptionConnectionMinimumIdleSize: 1 - subscriptionConnectionPoolSize: 50 - connectionMinimumIdleSize: 24 - connectionPoolSize: 64 - database: 0 - dnsMonitoringInterval: 5000 -threads: 16 -nettyThreads: 32 -codec: ! {} -transportMode: "NIO" \ No newline at end of file diff --git a/common-api/src/main/resources/redisson.yaml b/common-api/src/main/resources/redisson.yaml deleted file mode 100644 index ac8dfc66..00000000 --- a/common-api/src/main/resources/redisson.yaml +++ /dev/null @@ -1,21 +0,0 @@ ---- -singleServerConfig: - idleConnectionTimeout: 10000 - connectTimeout: 10000 - timeout: 3000 - retryAttempts: 3 - retryInterval: 1500 - password: lilishop - subscriptionsPerConnection: 5 - clientName: null - address: "redis://127.0.0.1:6379" - subscriptionConnectionMinimumIdleSize: 1 - subscriptionConnectionPoolSize: 50 - connectionMinimumIdleSize: 24 - connectionPoolSize: 64 - database: 0 - dnsMonitoringInterval: 5000 -threads: 16 -nettyThreads: 32 -codec: ! {} -transportMode: "NIO" \ No newline at end of file diff --git a/consumer/src/main/resources/redisson.yaml b/consumer/src/main/resources/redisson.yaml deleted file mode 100644 index ac8dfc66..00000000 --- a/consumer/src/main/resources/redisson.yaml +++ /dev/null @@ -1,21 +0,0 @@ ---- -singleServerConfig: - idleConnectionTimeout: 10000 - connectTimeout: 10000 - timeout: 3000 - retryAttempts: 3 - retryInterval: 1500 - password: lilishop - subscriptionsPerConnection: 5 - clientName: null - address: "redis://127.0.0.1:6379" - subscriptionConnectionMinimumIdleSize: 1 - subscriptionConnectionPoolSize: 50 - connectionMinimumIdleSize: 24 - connectionPoolSize: 64 - database: 0 - dnsMonitoringInterval: 5000 -threads: 16 -nettyThreads: 32 -codec: ! {} -transportMode: "NIO" \ No newline at end of file diff --git a/framework/src/main/java/cn/lili/cache/config/redis/RedisConfig.java b/framework/src/main/java/cn/lili/cache/config/redis/RedisConfig.java index dbb8ad39..659c1ece 100644 --- a/framework/src/main/java/cn/lili/cache/config/redis/RedisConfig.java +++ b/framework/src/main/java/cn/lili/cache/config/redis/RedisConfig.java @@ -1,12 +1,17 @@ package cn.lili.cache.config.redis; +import cn.hutool.core.text.CharSequenceUtil; import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.parser.ParserConfig; import lombok.extern.slf4j.Slf4j; import org.apache.commons.codec.digest.DigestUtils; import org.redisson.Redisson; import org.redisson.api.RedissonClient; +import org.redisson.config.ClusterServersConfig; import org.redisson.config.Config; +import org.redisson.config.SentinelServersConfig; +import org.redisson.config.SingleServerConfig; +import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; @@ -20,7 +25,6 @@ import org.springframework.cache.interceptor.KeyGenerator; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Primary; -import org.springframework.core.io.ClassPathResource; import org.springframework.data.redis.cache.RedisCacheConfiguration; import org.springframework.data.redis.cache.RedisCacheManager; import org.springframework.data.redis.cache.RedisCacheWriter; @@ -31,9 +35,10 @@ import org.springframework.data.redis.core.RedisTemplate; import org.springframework.data.redis.serializer.RedisSerializationContext; import org.springframework.data.redis.serializer.StringRedisSerializer; -import java.io.IOException; import java.time.Duration; +import java.util.ArrayList; import java.util.HashMap; +import java.util.List; import java.util.Map; /** @@ -54,6 +59,9 @@ public class RedisConfig extends CachingConfigurerSupport { @Value("${lili.cache.timeout:7200}") private Integer timeout; + @Autowired + private RedisProperties redisProperties; + /** * 当有多个管理器的时候,必须使用该注解在一个管理器上注释:表示该管理器为默认的管理器 @@ -105,9 +113,41 @@ public class RedisConfig extends CachingConfigurerSupport { } @Bean(destroyMethod = "shutdown") - public RedissonClient redisson() throws IOException { - return Redisson.create( - Config.fromYAML(new ClassPathResource("redisson.yaml").getInputStream())); + public RedissonClient redisson() { + Config config = new Config(); + + if (redisProperties.getSentinel() != null && !redisProperties.getSentinel().getNodes().isEmpty()) { + // 哨兵模式 + SentinelServersConfig sentinelServersConfig = config.useSentinelServers(); + sentinelServersConfig.setMasterName(redisProperties.getSentinel().getMaster()); + List sentinelAddress = new ArrayList<>(); + for (String node : redisProperties.getCluster().getNodes()) { + sentinelAddress.add("redis://" + node); + } + sentinelServersConfig.setSentinelAddresses(sentinelAddress); + if (CharSequenceUtil.isNotEmpty(redisProperties.getSentinel().getPassword())) { + sentinelServersConfig.setSentinelPassword(redisProperties.getSentinel().getPassword()); + } + } else if (redisProperties.getCluster() != null && !redisProperties.getCluster().getNodes().isEmpty()) { + // 集群模式 + ClusterServersConfig clusterServersConfig = config.useClusterServers(); + List clusterNodes = new ArrayList<>(); + for (String node : redisProperties.getCluster().getNodes()) { + clusterNodes.add("redis://" + node); + } + clusterServersConfig.setNodeAddresses(clusterNodes); + if (CharSequenceUtil.isNotEmpty(redisProperties.getPassword())) { + clusterServersConfig.setPassword(redisProperties.getPassword()); + } + } else { + SingleServerConfig singleServerConfig = config.useSingleServer(); + singleServerConfig.setAddress("redis://" + redisProperties.getHost() + ":" + redisProperties.getPort()); + if (CharSequenceUtil.isNotEmpty(redisProperties.getPassword())) { + singleServerConfig.setPassword(redisProperties.getPassword()); + } + } + + return Redisson.create(config); } /** diff --git a/manager-api/src/main/resources/redisson.yaml b/manager-api/src/main/resources/redisson.yaml deleted file mode 100644 index ac8dfc66..00000000 --- a/manager-api/src/main/resources/redisson.yaml +++ /dev/null @@ -1,21 +0,0 @@ ---- -singleServerConfig: - idleConnectionTimeout: 10000 - connectTimeout: 10000 - timeout: 3000 - retryAttempts: 3 - retryInterval: 1500 - password: lilishop - subscriptionsPerConnection: 5 - clientName: null - address: "redis://127.0.0.1:6379" - subscriptionConnectionMinimumIdleSize: 1 - subscriptionConnectionPoolSize: 50 - connectionMinimumIdleSize: 24 - connectionPoolSize: 64 - database: 0 - dnsMonitoringInterval: 5000 -threads: 16 -nettyThreads: 32 -codec: ! {} -transportMode: "NIO" \ No newline at end of file diff --git a/seller-api/src/main/resources/redisson.yaml b/seller-api/src/main/resources/redisson.yaml deleted file mode 100644 index ac8dfc66..00000000 --- a/seller-api/src/main/resources/redisson.yaml +++ /dev/null @@ -1,21 +0,0 @@ ---- -singleServerConfig: - idleConnectionTimeout: 10000 - connectTimeout: 10000 - timeout: 3000 - retryAttempts: 3 - retryInterval: 1500 - password: lilishop - subscriptionsPerConnection: 5 - clientName: null - address: "redis://127.0.0.1:6379" - subscriptionConnectionMinimumIdleSize: 1 - subscriptionConnectionPoolSize: 50 - connectionMinimumIdleSize: 24 - connectionPoolSize: 64 - database: 0 - dnsMonitoringInterval: 5000 -threads: 16 -nettyThreads: 32 -codec: ! {} -transportMode: "NIO" \ No newline at end of file