解決Springboot集成Redis集群配置公網(wǎng)IP連接報(bào)私網(wǎng)IP連接失敗問(wèn)題
1、問(wèn)題:
在Springboot 集成 Redis集群配置公網(wǎng)IP連接報(bào)私網(wǎng)IP連接失敗,一直報(bào)私有IP連接失敗
14 14:57:49.180 WARN 22012 --- [ioEventLoop-6-4] i.l.c.c.topology.ClusterTopologyRefresh : Unable to connect to [192.168.0.19:6384]: connection timed out: /192.168.0.19:6384 2020-08-14 14:57:49.180 WARN 22012 --- [ioEventLoop-6-3] i.l.c.c.topology.ClusterTopologyRefresh : Unable to connect to [192.168.0.19:6383]: connection timed out: /192.168.0.19:6383 2020-08-14 14:57:49.182 WARN 22012 --- [ioEventLoop-6-2] i.l.c.c.topology.ClusterTopologyRefresh : Unable to connect to [192.168.0.19:6382]: connection timed out: /192.168.0.19:6382 2020-08-14 14:57:49.182 WARN 22012 --- [ioEventLoop-6-1] i.l.c.c.topology.ClusterTopologyRefresh : Unable to connect to [192.168.0.19:6381]: connection timed out: /192.168.0.19:6381 2020-08-14 14:57:49.190 WARN 22012 --- [ioEventLoop-6-1] i.l.c.c.topology.ClusterTopologyRefresh : Unable to connect to [192.168.0.19:6385]: connection timed out: /192.168.0.19:6385 2020-08-14 14:57:49.191 WARN 22012 --- [ioEventLoop-6-2] i.l.c.c.topology.ClusterTopologyRefresh : Unable to connect to [192.168.0.19:6386]: connection timed out: /192.168.0.19:6386 2020-08-14 14:57:59.389 WARN 22012 --- [ioEventLoop-6-3] i.l.core.cluster.RedisClusterClient : connection timed out: /192.168.0.19:6382 2020-08-14 14:58:09.391 WARN 22012 --- [ioEventLoop-6-4] i.l.core.cluster.RedisClusterClient : connection timed out: /192.168.0.19:6381 2020-08-14 14:58:19.393 WARN 22012 --- [ioEventLoop-6-1] i.l.core.cluster.RedisClusterClient : connection timed out: /192.168.0.19:6383 2020-08-14 14:58:29.396 WARN 22012 --- [ioEventLoop-6-2] i.l.core.cluster.RedisClusterClient : connection timed out: /192.168.0.19:6384 2020-08-14 14:58:39.399 WARN 22012 --- [ioEventLoop-6-3] i.l.core.cluster.RedisClusterClient : connection timed out: /192.168.0.19:6386 2020-08-14 14:58:49.402 WARN 22012 --- [ioEventLoop-6-4] i.l.core.cluster.RedisClusterClient : connection timed out: /192.168.0.19:6385
2、配置文件
創(chuàng)建6個(gè)配置文件:redis-6381.conf,redis-6382.conf,redis-6383.conf,redis-6384.conf,redis-6385.conf,
redis-6386.conf。配置文件內(nèi)容如下:
# 配置文件進(jìn)行了精簡(jiǎn),完整配置可自行和官方提供的完整conf文件進(jìn)行對(duì)照。端口號(hào)自行對(duì)應(yīng)修改 #默認(rèn)是 protected-mode yes,即開啟保護(hù)模式, no=關(guān)閉 #在redis的配置文件中會(huì)遇到protected-mode,它直譯為保護(hù)模式。 #如果設(shè)置為yes,那么只允許我們?cè)诒緳C(jī)的回環(huán)連接,其他機(jī)器無(wú)法連接。 #線上Redis服務(wù),為了安全,我們建議將protected-mode設(shè)置為yes。 #protected-mode設(shè)置為yes的情況下,為了我們的應(yīng)用服務(wù)可以正常訪問(wèn)Redis,我們需要設(shè)置Redis的bind參數(shù)或者密碼參數(shù)#requirepass。 protected-mode yes #端口號(hào) port 6381 # IP綁定,redis不建議對(duì)公網(wǎng)開放,這里綁定了服務(wù)器私網(wǎng)IP及環(huán)回地址 bind 192.168.0.19 127.0.0.1 requirepass 123456 # redis數(shù)據(jù)文件存放的目錄 dir /redis/workdata # 日志文件 logfile "/redis/logs/cluster-node-6381.log" # 開啟AOF appendonly yes #后臺(tái)啟動(dòng) daemonize yes # 開啟集群 cluster-enabled yes # 集群持久化配置文件,內(nèi)容包含其它節(jié)點(diǎn)的狀態(tài),持久化變量等,會(huì)自動(dòng)生成在上面配置的dir目錄下 cluster-config-file cluster-node-6381.conf # 集群節(jié)點(diǎn)不可用的最大時(shí)間(毫秒),如果主節(jié)點(diǎn)在指定時(shí)間內(nèi)不可達(dá),那么會(huì)進(jìn)行故障轉(zhuǎn)移 cluster-node-timeout 5000
3、springboot集成redis集群有以下配置,二選一:
1:代碼配置
@Configuration public class RedisClusterConfig { @Bean public RedisConnectionFactory redisConnectionFactory() { // 客戶端讀寫分離配置 LettuceClientConfiguration clientConfig = LettuceClientConfiguration.builder() .readFrom(ReadFrom.REPLICA_PREFERRED) .build(); RedisClusterConfiguration redisClusterConfiguration = new RedisClusterConfiguration(Arrays.asList( "42.192.119.238:6381", "42.192.119.238:6382", "42.192.119.238:6383", "42.192.119.238:6384", "42.192.119.238:6385", "42.192.119.238:6386")); return new LettuceConnectionFactory(redisClusterConfiguration, clientConfig); } }
2:yml 配置
#集群模式 spring: redis: cluster: max-redirects: 3 nodes: - 42.192.119.238:6381 - 42.192.119.238:6382 - 42.192.119.238:6383 - 42.192.119.238:6384 - 42.192.119.238:6385 - 42.192.119.238:6386 database: 0 # host: 42.192.119.238 # port: 6380 password: timeout: 5000s #連接超時(shí)時(shí)長(zhǎng) # 連接池最大連接數(shù)(使用負(fù)值表示沒有限制) jedis: pool: max-active: 8 #連接池最大連接數(shù)量,負(fù)數(shù)表示無(wú)限,默認(rèn)為8 max-idle: 8 #連接池最大空閑數(shù)量,默認(rèn)8 min-idle: 0 #連接池最小空閑數(shù)量,默認(rèn)0
4、解決鏈接報(bào)錯(cuò)問(wèn)題
讓Redis暴露公網(wǎng)IP其實(shí)在redis.conf配置文件里是能找到的,這里我們可以手動(dòng)指定Redis的公網(wǎng)IP、端口以及總線端口(默認(rèn)服務(wù)端口加10000)。
手動(dòng)指定了公網(wǎng)ip后,Redis集群中的節(jié)點(diǎn)會(huì)通過(guò)公網(wǎng)IP進(jìn)行通信,也就是外網(wǎng)訪問(wèn)。因此相關(guān)的總線端口,如下面的16381等總線端口必須在云服務(wù)器中的安全組中放開
# 配置文件進(jìn)行了精簡(jiǎn),完整配置可自行和官方提供的完整conf文件進(jìn)行對(duì)照。端口號(hào)自行對(duì)應(yīng)修改 #默認(rèn)是 protected-mode yes,即開啟保護(hù)模式, no=關(guān)閉 #在redis的配置文件中會(huì)遇到protected-mode,它直譯為保護(hù)模式。 #如果設(shè)置為yes,那么只允許我們?cè)诒緳C(jī)的回環(huán)連接,其他機(jī)器無(wú)法連接。 #線上Redis服務(wù),為了安全,我們建議將protected-mode設(shè)置為yes。 #protected-mode設(shè)置為yes的情況下,為了我們的應(yīng)用服務(wù)可以正常訪問(wèn)Redis,我們需要設(shè)置Redis的bind參數(shù)或者密碼參數(shù)#requirepass。 protected-mode yes #端口號(hào) port 6381 # IP綁定,redis不建議對(duì)公網(wǎng)開放,這里綁定了服務(wù)器私網(wǎng)IP及環(huán)回地址 bind 192.168.0.19 127.0.0.1 requirepass 123456 # redis數(shù)據(jù)文件存放的目錄 dir /redis/workdata # 日志文件 logfile "/redis/logs/cluster-node-6381.log" # 開啟AOF appendonly yes #后臺(tái)啟動(dòng) daemonize yes # 開啟集群 cluster-enabled yes # 集群持久化配置文件,內(nèi)容包含其它節(jié)點(diǎn)的狀態(tài),持久化變量等,會(huì)自動(dòng)生成在上面配置的dir目錄下 cluster-config-file cluster-node-6381.conf # 集群節(jié)點(diǎn)不可用的最大時(shí)間(毫秒),如果主節(jié)點(diǎn)在指定時(shí)間內(nèi)不可達(dá),那么會(huì)進(jìn)行故障轉(zhuǎn)移 cluster-node-timeout 5000 # 云服務(wù)器上部署需指定公網(wǎng)ip cluster-announce-ip 42.192.119.238 # Redis總線端口,用于與其它節(jié)點(diǎn)通信 cluster-announce-bus-port 16381
根據(jù)以上配置修改每一個(gè)redis節(jié)點(diǎn)的配置,注意端口不能相同
在src 目錄下執(zhí)行命令:
./redis-cli -c -p 6381 -h 192.168.0.19 -a 123456 cluster nodes
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe. 6297ab04ff4bbfcad778b80315619defc2f6e513 42.192.119.238:6382@16382 master - 0 1696924087000 2 connected 5461-10922 19d3955d4bacd04892c13e7a05e13c7744085896 42.192.119.238:6381@16381 myself,master - 0 1696924087000 1 connected 0-5460 3a21d6c05255229741593340a781affbdcad6236 42.192.119.238:6385@16385 slave df0858e942b03f5b3c848d1acb2a4fde1f70e290 0 1696924088000 3 connected 546c8528a07abee29a1e383a3130d4f306447f0e 42.192.119.238:6386@16386 slave 19d3955d4bacd04892c13e7a05e13c7744085896 0 1696924086000 1 connected 56b730c5631515b2359bbf9b6d4306460da8502c 42.192.119.238:6384@16384 slave 6297ab04ff4bbfcad778b80315619defc2f6e513 0 1696924088460 2 connected df0858e942b03f5b3c848d1acb2a4fde1f70e290 42.192.119.238:6383@16383 master - 0 1696924089469 3 connected 10923-16383
可以發(fā)現(xiàn),各節(jié)點(diǎn)暴露的IP全是公網(wǎng)IP了。
5、故障轉(zhuǎn)移期間Lettuce客戶端連接問(wèn)題
解決方式:
1、yml指定使用jedis
2、代碼配置
1)、更換Redis客戶端
@Configuration public class RedisClusterConfig { @Bean public RedisConnectionFactory redisConnectionFactory() { RedisClusterConfiguration redisClusterConfiguration = new RedisClusterConfiguration(Arrays.asList( "42.192.119.238:6381", "42.192.119.238:6382", "42.192.119.238:6383", "42.192.119.238:6384", "42.192.119.238:6385", "42.192.119.238:6386")); return new JedisConnectionFactory(redisClusterConfiguration); } }
參考鏈接:
https://github.com/lettuce-io/lettuce-core/wiki/Redis-Cluster
https://github.com/lettuce-io/lettuce-core/wiki/Client-options#cluster-specific-options
2)、更改實(shí)現(xiàn)配置
@Configuration public class RedisClusterConfig { @Bean public RedisConnectionFactory redisConnectionFactory() { // 開啟自適應(yīng)集群拓?fù)渌⑿潞椭芷谕負(fù)渌⑿?,不開啟相應(yīng)槽位主節(jié)點(diǎn)掛掉會(huì)出現(xiàn)服務(wù)不可用,直到掛掉節(jié)點(diǎn)重新恢復(fù) ClusterTopologyRefreshOptions clusterTopologyRefreshOptions = ClusterTopologyRefreshOptions.builder() .enableAllAdaptiveRefreshTriggers() // 開啟自適應(yīng)刷新,自適應(yīng)刷新不開啟,Redis集群變更時(shí)將會(huì)導(dǎo)致連接異常 .adaptiveRefreshTriggersTimeout(Duration.ofSeconds(30)) //自適應(yīng)刷新超時(shí)時(shí)間(默認(rèn)30秒),默認(rèn)關(guān)閉開啟后時(shí)間為30秒 .enablePeriodicRefresh(Duration.ofSeconds(20)) // 默認(rèn)關(guān)閉開啟后時(shí)間為60秒 .build(); ClientOptions clientOptions = ClusterClientOptions.builder() .topologyRefreshOptions(clusterTopologyRefreshOptions) .build(); // 客戶端讀寫分離配置 LettuceClientConfiguration clientConfig = LettuceClientConfiguration.builder() .clientOptions(clientOptions) .build(); RedisClusterConfiguration redisClusterConfiguration = new RedisClusterConfiguration(Arrays.asList( "42.192.119.238:6381", "42.192.119.238:6382", "42.192.119.238:6383", "42.192.119.238:6384", "42.192.119.238:6385", "42.192.119.238:6386")); return new LettuceConnectionFactory(redisClusterConfiguration, clientConfig); } }
以上就是解決Springboot集成Redis集群配置公網(wǎng)IP連接報(bào)私網(wǎng)IP連接失敗問(wèn)題的詳細(xì)內(nèi)容,更多關(guān)于Springboot私網(wǎng)IP連接失敗的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
java中Scanner.next()和Scanner.nextLine的區(qū)別圖文詳解
使用java語(yǔ)言編程,最常用的輸入就是使用Scanner了,它的構(gòu)造很簡(jiǎn)單,這篇文章主要給大家介紹了關(guān)于java中Scanner.next()和Scanner.nextLine區(qū)別的相關(guān)資料,需要的朋友可以參考下2024-02-02教您如何3分鐘快速搞定EasyExcel導(dǎo)入與導(dǎo)出功能
對(duì)于EasyExcel庫(kù),我們可以使用它來(lái)實(shí)現(xiàn)數(shù)據(jù)的導(dǎo)入和導(dǎo)出,下面這篇文章主要給大家介紹了關(guān)于如何3分鐘快速搞定EasyExcel導(dǎo)入與導(dǎo)出功能的相關(guān)資料,文中通過(guò)代碼介紹的非常詳細(xì),需要的朋友可以參考下2024-01-01Java 實(shí)戰(zhàn)項(xiàng)目之家居購(gòu)物商城系統(tǒng)詳解流程
讀萬(wàn)卷書不如行萬(wàn)里路,只學(xué)書上的理論是遠(yuǎn)遠(yuǎn)不夠的,只有在實(shí)戰(zhàn)中才能獲得能力的提升,本篇文章手把手帶你用Java實(shí)現(xiàn)一個(gè)家居購(gòu)物商城系統(tǒng),大家可以在過(guò)程中查缺補(bǔ)漏,提升水平2021-11-11

基于Jasypt對(duì)SpringBoot配置文件加密

詳解關(guān)于mybatis-plus中Service和Mapper的分析

細(xì)談java同步之JMM(Java Memory Model)