From 5285735c7a5ce268628e00d134e61ce720213a5f Mon Sep 17 00:00:00 2001
From: 疯狂的狮子li <15040126243@163.com>
Date: 星期二, 14 九月 2021 16:57:07 +0800
Subject: [PATCH] add 增加 redis redisson 集群配置

---
 ruoyi-admin/src/main/resources/application-prod.yml                                         |   63 ++++++++++++
 ruoyi-framework/src/main/java/com/ruoyi/framework/config/RedisConfig.java                   |   64 +++++++++---
 ruoyi-framework/src/main/java/com/ruoyi/framework/config/properties/RedissonProperties.java |  103 ++++++++++++++++++++
 ruoyi-admin/src/main/resources/application-dev.yml                                          |   63 ++++++++++++
 4 files changed, 274 insertions(+), 19 deletions(-)

diff --git a/ruoyi-admin/src/main/resources/application-dev.yml b/ruoyi-admin/src/main/resources/application-dev.yml
index 7804994..eb6962f 100644
--- a/ruoyi-admin/src/main/resources/application-dev.yml
+++ b/ruoyi-admin/src/main/resources/application-dev.yml
@@ -70,7 +70,7 @@
           config:
             multi-statement-allow: true
 
---- # redis 閰嶇疆
+--- # redis 鍗曟満閰嶇疆(鍗曟満涓庨泦缇ゅ彧鑳藉紑鍚竴涓彟涓�涓渶瑕佹敞閲婃帀)
 spring:
   redis:
     # 鍦板潃
@@ -118,6 +118,67 @@
     # DNS鐩戞祴鏃堕棿闂撮殧锛屽崟浣嶏細姣
     dnsMonitoringInterval: 5000
 
+#--- # redis 闆嗙兢閰嶇疆(鍗曟満涓庨泦缇ゅ彧鑳藉紑鍚竴涓彟涓�涓渶瑕佹敞閲婃帀)
+#spring:
+#  redis:
+#    cluster:
+#      nodes:
+#        - 192.168.0.100:6379
+#        - 192.168.0.101:6379
+#        - 192.168.0.102:6379
+#    # 瀵嗙爜
+#    password:
+#    # 杩炴帴瓒呮椂鏃堕棿
+#    timeout: 10s
+#    # 鏄惁寮�鍚痵sl
+#    ssl: false
+#
+#redisson:
+#  # 绾跨▼姹犳暟閲�
+#  threads: 16
+#  # Netty绾跨▼姹犳暟閲�
+#  nettyThreads: 32
+#  # 浼犺緭妯″紡
+#  transportMode: "NIO"
+#  # 闆嗙兢閰嶇疆
+#  clusterServersConfig:
+#    # 瀹㈡埛绔悕绉�
+#    clientName: ${ruoyi.name}
+#    # master鏈�灏忕┖闂茶繛鎺ユ暟
+#    masterConnectionMinimumIdleSize: 32
+#    # master杩炴帴姹犲ぇ灏�
+#    masterConnectionPoolSize: 64
+#    # slave鏈�灏忕┖闂茶繛鎺ユ暟
+#    slaveConnectionMinimumIdleSize: 32
+#    # slave杩炴帴姹犲ぇ灏�
+#    slaveConnectionPoolSize: 64
+#    # 杩炴帴绌洪棽瓒呮椂锛屽崟浣嶏細姣
+#    idleConnectionTimeout: 10000
+#    # ping杩炴帴闂撮殧
+#    pingConnectionInterval: 1000
+#    # 鍛戒护绛夊緟瓒呮椂锛屽崟浣嶏細姣
+#    timeout: 3000
+#    # 濡傛灉灏濊瘯鍦ㄦ闄愬埗涔嬪唴鍙戦�佹垚鍔燂紝鍒欏紑濮嬪惎鐢� timeout 璁℃椂銆�
+#    retryAttempts: 3
+#    # 鍛戒护閲嶈瘯鍙戦�佹椂闂撮棿闅旓紝鍗曚綅锛氭绉�
+#    retryInterval: 1500
+#    # 浠庡彲鐢ㄦ湇鍔″櫒鐨勫唴閮ㄥ垪琛ㄤ腑鎺掗櫎 Redis Slave 閲嶆柊杩炴帴灏濊瘯鐨勯棿闅斻��
+#    failedSlaveReconnectionInterval: 3000
+#    # 鍙戝竷鍜岃闃呰繛鎺ユ睜鏈�灏忕┖闂茶繛鎺ユ暟
+#    subscriptionConnectionMinimumIdleSize: 1
+#    # 鍙戝竷鍜岃闃呰繛鎺ユ睜澶у皬
+#    subscriptionConnectionPoolSize: 50
+#    # 鍗曚釜杩炴帴鏈�澶ц闃呮暟閲�
+#    subscriptionsPerConnection: 5
+#    # 鎵弿闂撮殧
+#    scanInterval: 1000
+#    # DNS鐩戞祴鏃堕棿闂撮殧锛屽崟浣嶏細姣
+#    dnsMonitoringInterval: 5000
+#    # 璇诲彇妯″紡
+#    readMode: "SLAVE"
+#    # 璁㈤槄妯″紡
+#    subscriptionMode: "MASTER"
+
 --- # 鐩戞帶閰嶇疆
 spring:
   boot:
diff --git a/ruoyi-admin/src/main/resources/application-prod.yml b/ruoyi-admin/src/main/resources/application-prod.yml
index 446d54d..13a2da8 100644
--- a/ruoyi-admin/src/main/resources/application-prod.yml
+++ b/ruoyi-admin/src/main/resources/application-prod.yml
@@ -70,7 +70,7 @@
           config:
             multi-statement-allow: true
 
---- # redis 閰嶇疆
+--- # redis 鍗曟満閰嶇疆(鍗曟満涓庨泦缇ゅ彧鑳藉紑鍚竴涓彟涓�涓渶瑕佹敞閲婃帀)
 spring:
   redis:
     # 鍦板潃
@@ -118,6 +118,67 @@
     # DNS鐩戞祴鏃堕棿闂撮殧锛屽崟浣嶏細姣
     dnsMonitoringInterval: 5000
 
+#--- # redis 闆嗙兢閰嶇疆(鍗曟満涓庨泦缇ゅ彧鑳藉紑鍚竴涓彟涓�涓渶瑕佹敞閲婃帀)
+#spring:
+#  redis:
+#    cluster:
+#      nodes:
+#        - 192.168.0.100:6379
+#        - 192.168.0.101:6379
+#        - 192.168.0.102:6379
+#    # 瀵嗙爜
+#    password:
+#    # 杩炴帴瓒呮椂鏃堕棿
+#    timeout: 10s
+#    # 鏄惁寮�鍚痵sl
+#    ssl: false
+#
+#redisson:
+#  # 绾跨▼姹犳暟閲�
+#  threads: 16
+#  # Netty绾跨▼姹犳暟閲�
+#  nettyThreads: 32
+#  # 浼犺緭妯″紡
+#  transportMode: "NIO"
+#  # 闆嗙兢閰嶇疆
+#  clusterServersConfig:
+#    # 瀹㈡埛绔悕绉�
+#    clientName: ${ruoyi.name}
+#    # master鏈�灏忕┖闂茶繛鎺ユ暟
+#    masterConnectionMinimumIdleSize: 32
+#    # master杩炴帴姹犲ぇ灏�
+#    masterConnectionPoolSize: 64
+#    # slave鏈�灏忕┖闂茶繛鎺ユ暟
+#    slaveConnectionMinimumIdleSize: 32
+#    # slave杩炴帴姹犲ぇ灏�
+#    slaveConnectionPoolSize: 64
+#    # 杩炴帴绌洪棽瓒呮椂锛屽崟浣嶏細姣
+#    idleConnectionTimeout: 10000
+#    # ping杩炴帴闂撮殧
+#    pingConnectionInterval: 1000
+#    # 鍛戒护绛夊緟瓒呮椂锛屽崟浣嶏細姣
+#    timeout: 3000
+#    # 濡傛灉灏濊瘯鍦ㄦ闄愬埗涔嬪唴鍙戦�佹垚鍔燂紝鍒欏紑濮嬪惎鐢� timeout 璁℃椂銆�
+#    retryAttempts: 3
+#    # 鍛戒护閲嶈瘯鍙戦�佹椂闂撮棿闅旓紝鍗曚綅锛氭绉�
+#    retryInterval: 1500
+#    # 浠庡彲鐢ㄦ湇鍔″櫒鐨勫唴閮ㄥ垪琛ㄤ腑鎺掗櫎 Redis Slave 閲嶆柊杩炴帴灏濊瘯鐨勯棿闅斻��
+#    failedSlaveReconnectionInterval: 3000
+#    # 鍙戝竷鍜岃闃呰繛鎺ユ睜鏈�灏忕┖闂茶繛鎺ユ暟
+#    subscriptionConnectionMinimumIdleSize: 1
+#    # 鍙戝竷鍜岃闃呰繛鎺ユ睜澶у皬
+#    subscriptionConnectionPoolSize: 50
+#    # 鍗曚釜杩炴帴鏈�澶ц闃呮暟閲�
+#    subscriptionsPerConnection: 5
+#    # 鎵弿闂撮殧
+#    scanInterval: 1000
+#    # DNS鐩戞祴鏃堕棿闂撮殧锛屽崟浣嶏細姣
+#    dnsMonitoringInterval: 5000
+#    # 璇诲彇妯″紡
+#    readMode: "SLAVE"
+#    # 璁㈤槄妯″紡
+#    subscriptionMode: "MASTER"
+
 --- # 鐩戞帶閰嶇疆
 spring:
   boot:
diff --git a/ruoyi-framework/src/main/java/com/ruoyi/framework/config/RedisConfig.java b/ruoyi-framework/src/main/java/com/ruoyi/framework/config/RedisConfig.java
index 867cd40..59f9f97 100644
--- a/ruoyi-framework/src/main/java/com/ruoyi/framework/config/RedisConfig.java
+++ b/ruoyi-framework/src/main/java/com/ruoyi/framework/config/RedisConfig.java
@@ -1,5 +1,6 @@
 package com.ruoyi.framework.config;
 
+import cn.hutool.core.util.ObjectUtil;
 import cn.hutool.core.util.StrUtil;
 import com.ruoyi.common.utils.StringUtils;
 import com.ruoyi.framework.config.properties.RedissonProperties;
@@ -58,23 +59,52 @@
 			.setTransportMode(redissonProperties.getTransportMode());
 
 		RedissonProperties.SingleServerConfig singleServerConfig = redissonProperties.getSingleServerConfig();
-		// 浣跨敤鍗曟満妯″紡
-		config.useSingleServer()
-			.setAddress(prefix + redisProperties.getHost() + ":" + redisProperties.getPort())
-			.setConnectTimeout(((Long) redisProperties.getTimeout().toMillis()).intValue())
-			.setDatabase(redisProperties.getDatabase())
-			.setPassword(StringUtils.isNotBlank(redisProperties.getPassword()) ? redisProperties.getPassword() : null)
-			.setTimeout(singleServerConfig.getTimeout())
-			.setRetryAttempts(singleServerConfig.getRetryAttempts())
-			.setRetryInterval(singleServerConfig.getRetryInterval())
-			.setSubscriptionsPerConnection(singleServerConfig.getSubscriptionsPerConnection())
-			.setClientName(singleServerConfig.getClientName())
-			.setIdleConnectionTimeout(singleServerConfig.getIdleConnectionTimeout())
-			.setSubscriptionConnectionMinimumIdleSize(singleServerConfig.getSubscriptionConnectionMinimumIdleSize())
-			.setSubscriptionConnectionPoolSize(singleServerConfig.getSubscriptionConnectionPoolSize())
-			.setConnectionMinimumIdleSize(singleServerConfig.getConnectionMinimumIdleSize())
-			.setConnectionPoolSize(singleServerConfig.getConnectionPoolSize())
-			.setDnsMonitoringInterval(singleServerConfig.getDnsMonitoringInterval());
+		if (ObjectUtil.isNotNull(singleServerConfig)) {
+			// 浣跨敤鍗曟満妯″紡
+			config.useSingleServer()
+					.setAddress(prefix + redisProperties.getHost() + ":" + redisProperties.getPort())
+					.setConnectTimeout(((Long) redisProperties.getTimeout().toMillis()).intValue())
+					.setDatabase(redisProperties.getDatabase())
+					.setPassword(StringUtils.isNotBlank(redisProperties.getPassword()) ? redisProperties.getPassword() : null)
+					.setTimeout(singleServerConfig.getTimeout())
+					.setRetryAttempts(singleServerConfig.getRetryAttempts())
+					.setRetryInterval(singleServerConfig.getRetryInterval())
+					.setSubscriptionsPerConnection(singleServerConfig.getSubscriptionsPerConnection())
+					.setClientName(singleServerConfig.getClientName())
+					.setIdleConnectionTimeout(singleServerConfig.getIdleConnectionTimeout())
+					.setSubscriptionConnectionMinimumIdleSize(singleServerConfig.getSubscriptionConnectionMinimumIdleSize())
+					.setSubscriptionConnectionPoolSize(singleServerConfig.getSubscriptionConnectionPoolSize())
+					.setConnectionMinimumIdleSize(singleServerConfig.getConnectionMinimumIdleSize())
+					.setConnectionPoolSize(singleServerConfig.getConnectionPoolSize())
+					.setDnsMonitoringInterval(singleServerConfig.getDnsMonitoringInterval());
+		}
+
+		RedissonProperties.ClusterServersConfig clusterServersConfig = redissonProperties.getClusterServersConfig();
+		if (ObjectUtil.isNotNull(clusterServersConfig)) {
+			// 浣跨敤闆嗙兢妯″紡
+			config.useClusterServers()
+					.setConnectTimeout(((Long) redisProperties.getTimeout().toMillis()).intValue())
+					.setPassword(StringUtils.isNotBlank(redisProperties.getPassword()) ? redisProperties.getPassword() : null)
+					.setTimeout(clusterServersConfig.getTimeout())
+					.setRetryAttempts(clusterServersConfig.getRetryAttempts())
+					.setRetryInterval(clusterServersConfig.getRetryInterval())
+					.setSubscriptionsPerConnection(clusterServersConfig.getSubscriptionsPerConnection())
+					.setClientName(clusterServersConfig.getClientName())
+					.setIdleConnectionTimeout(clusterServersConfig.getIdleConnectionTimeout())
+					.setPingConnectionInterval(clusterServersConfig.getPingConnectionInterval())
+					.setSubscriptionConnectionMinimumIdleSize(clusterServersConfig.getSubscriptionConnectionMinimumIdleSize())
+					.setSubscriptionConnectionPoolSize(clusterServersConfig.getSubscriptionConnectionPoolSize())
+					.setMasterConnectionMinimumIdleSize(clusterServersConfig.getMasterConnectionMinimumIdleSize())
+					.setMasterConnectionPoolSize(clusterServersConfig.getMasterConnectionPoolSize())
+					.setSlaveConnectionMinimumIdleSize(clusterServersConfig.getSlaveConnectionMinimumIdleSize())
+					.setSlaveConnectionPoolSize(clusterServersConfig.getSlaveConnectionPoolSize())
+					.setDnsMonitoringInterval(clusterServersConfig.getDnsMonitoringInterval())
+					.setFailedSlaveReconnectionInterval(clusterServersConfig.getFailedSlaveReconnectionInterval())
+					.setScanInterval(clusterServersConfig.getScanInterval())
+					.setReadMode(clusterServersConfig.getReadMode())
+					.setSubscriptionMode(clusterServersConfig.getSubscriptionMode())
+					.setNodeAddresses(redisProperties.getCluster().getNodes());
+		}
 		RedissonClient redissonClient = Redisson.create(config);
 		log.info("鍒濆鍖� redis 閰嶇疆");
 		return redissonClient;
diff --git a/ruoyi-framework/src/main/java/com/ruoyi/framework/config/properties/RedissonProperties.java b/ruoyi-framework/src/main/java/com/ruoyi/framework/config/properties/RedissonProperties.java
index b6e289d..4516da2 100644
--- a/ruoyi-framework/src/main/java/com/ruoyi/framework/config/properties/RedissonProperties.java
+++ b/ruoyi-framework/src/main/java/com/ruoyi/framework/config/properties/RedissonProperties.java
@@ -2,6 +2,8 @@
 
 import lombok.Data;
 import lombok.NoArgsConstructor;
+import org.redisson.config.ReadMode;
+import org.redisson.config.SubscriptionMode;
 import org.redisson.config.TransportMode;
 import org.springframework.boot.context.properties.ConfigurationProperties;
 import org.springframework.stereotype.Component;
@@ -37,6 +39,11 @@
 	 * 鍗曟満鏈嶅姟閰嶇疆
 	 */
 	private SingleServerConfig singleServerConfig;
+
+	/**
+	 * 闆嗙兢鏈嶅姟閰嶇疆
+	 */
+	private ClusterServersConfig clusterServersConfig;
 
 	/**
 	 * 缂撳瓨缁�
@@ -106,6 +113,102 @@
 
 	@Data
 	@NoArgsConstructor
+	public static class ClusterServersConfig {
+
+		/**
+		 * 瀹㈡埛绔悕绉�
+		 */
+		private String clientName;
+
+		/**
+		 * master鏈�灏忕┖闂茶繛鎺ユ暟
+		 */
+		private int masterConnectionMinimumIdleSize;
+
+		/**
+		 * master杩炴帴姹犲ぇ灏�
+		 */
+		private int masterConnectionPoolSize;
+
+		/**
+		 * slave鏈�灏忕┖闂茶繛鎺ユ暟
+		 */
+		private int slaveConnectionMinimumIdleSize;
+
+		/**
+		 * slave杩炴帴姹犲ぇ灏�
+		 */
+		private int slaveConnectionPoolSize;
+
+		/**
+		 * 杩炴帴绌洪棽瓒呮椂锛屽崟浣嶏細姣
+		 */
+		private int idleConnectionTimeout;
+
+		/**
+		 * ping瓒呮椂
+		 */
+		private int pingConnectionInterval;
+
+		/**
+		 * 鍛戒护绛夊緟瓒呮椂锛屽崟浣嶏細姣
+		 */
+		private int timeout;
+
+		/**
+		 * 濡傛灉灏濊瘯鍦ㄦ闄愬埗涔嬪唴鍙戦�佹垚鍔燂紝鍒欏紑濮嬪惎鐢� timeout 璁℃椂銆�
+		 */
+		private int retryAttempts;
+
+		/**
+		 * 鍛戒护閲嶈瘯鍙戦�佹椂闂撮棿闅旓紝鍗曚綅锛氭绉�
+		 */
+		private int retryInterval;
+
+		/**
+		 * 閿欒閲嶈瘯娆℃暟
+		 */
+		private int failedSlaveReconnectionInterval;
+
+		/**
+		 * 鍙戝竷鍜岃闃呰繛鎺ユ睜鏈�灏忕┖闂茶繛鎺ユ暟
+		 */
+		private int subscriptionConnectionMinimumIdleSize;
+
+		/**
+		 * 鍙戝竷鍜岃闃呰繛鎺ユ睜澶у皬
+		 */
+		private int subscriptionConnectionPoolSize;
+
+		/**
+		 * 鍗曚釜杩炴帴鏈�澶ц闃呮暟閲�
+		 */
+		private int subscriptionsPerConnection;
+
+		/**
+		 * 鎵弿闂撮殧
+		 */
+		private int scanInterval;
+
+		/**
+		 * DNS鐩戞祴鏃堕棿闂撮殧锛屽崟浣嶏細姣
+		 */
+		private int dnsMonitoringInterval;
+
+		/**
+		 * 璇诲彇妯″紡
+		 */
+		private ReadMode readMode;
+
+		/**
+		 * 璁㈤槄妯″紡
+		 */
+		private SubscriptionMode subscriptionMode;
+
+	}
+
+	@Data
+	@NoArgsConstructor
 	public static class CacheGroup {
 
 		/**

--
Gitblit v1.9.3