偷偷摘套内射激情视频,久久精品99国产国产精,中文字幕无线乱码人妻,中文在线中文a,性爽19p

Redis Cluster集群,當(dāng)Master宕機(jī),主從切換,客戶端報(bào)錯(cuò) Timed Out

開發(fā) 前端
一個(gè)高并發(fā)系統(tǒng)肯定少不了緩存的身影,為了保證緩存服務(wù)的高可用,我們通常采用 Redis Cluster 集群模式。

大家好,我是Tom哥。

性能不夠,緩存來(lái)湊。

一個(gè)高并發(fā)系統(tǒng)肯定少不了緩存的身影,為了保證緩存服務(wù)的高可用,我們通常采用 Redis Cluster 集群模式。

描述:

集群部署采用了 3主3從 拓?fù)浣Y(jié)構(gòu),數(shù)據(jù)讀寫訪問(wèn)master節(jié)點(diǎn), slave節(jié)點(diǎn)負(fù)責(zé)備份。

隨便登錄一臺(tái) redis 節(jié)點(diǎn),都可以看到集群的slot的槽位分步區(qū)間,以及對(duì)應(yīng)的主從節(jié)點(diǎn)映射關(guān)系。

127.0.0.1:8001> cluster slots
1) 1) (integer) 10923
2) (integer) 16383
3) 1) "127.0.0.1"
2) (integer) 8003
3) "6c574c9d1323c69ebc73a5977bcbd3d4c073a4d4"
4) 1) "127.0.0.1"
2) (integer) 8006
3) "123d0b157078925743ac1deb96be8c3395d7d038"
2) 1) (integer) 0
2) (integer) 5460
3) 1) "127.0.0.1"
2) (integer) 8001
3) "99bc05e81ef0035a4ab2d13cbae2599425b7ed7d"
4) 1) "127.0.0.1"
2) (integer) 8004
3) "402e900ef364ce9382beddf92747cf28e3ea9c2f"
3) 1) (integer) 5461
2) (integer) 10922
3) 1) "127.0.0.1"
2) (integer) 8002
3) "fda6a9e49205a52418c0bca4c66c981066017a3c"
4) 1) "127.0.0.1"
2) (integer) 8005
3) "24a1e23f6cbfb761234970b66043d562e79e3d9c"

人為模擬,master-1 機(jī)器意外宕機(jī)。

docker stop c1dff012392d

此時(shí),Redis Cluster 集群能自動(dòng)感知,并自動(dòng)完成主備切換,對(duì)應(yīng)的slave會(huì)被選舉為新的master節(jié)點(diǎn)。

看下 redis cluster 集群最新的主從關(guān)系。

看似也沒(méi)什么問(wèn)題,一切正常。

此時(shí) Spring Boot 應(yīng)用依然在線服務(wù),當(dāng)我們?cè)賴L試操作緩存時(shí),會(huì)報(bào)錯(cuò)。

問(wèn)題邊界還是非常清晰的。

Redis Cluster 集群已經(jīng)完成了切換。

但是 Spring Boot 客戶端沒(méi)有動(dòng)態(tài)感知到 Redis Cluster 的最新集群信息

原因分析:

SpringBoot 2.X 版本, Redis默認(rèn)的連接池采用 Lettuce。

當(dāng)Redis 集群節(jié)點(diǎn)發(fā)生變化后,Letture默認(rèn)是不會(huì)刷新節(jié)點(diǎn)拓?fù)洹?/p>

解決方案:

將 Letture 二方包仲裁掉。

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
<version>2.3.12.RELEASE</version>
<exclusions>
<exclusion>
<groupId>io.lettuce</groupId>
<artifactId>lettuce-core</artifactId>
</exclusion>
</exclusions>
</dependency>

然后,引入 Jedis 相關(guān)二方包。

<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>3.7.0</version>
</dependency>

編譯代碼,并重新啟動(dòng) SpringBoot 微服務(wù),萬(wàn)事俱備,只欠再次驗(yàn)證。

重新模擬將 127.0.0.1:8001 master 節(jié)點(diǎn)宕機(jī),看看系統(tǒng)的日志。

[2022-03-17 18:03:34:595] - master /127.0.0.1:8001 used as slave
[2022-03-17 18:03:34:596] - slave redis://127.0.0.1:8004 removed for slot ranges: [[0-5460]]
[2022-03-17 18:03:34:611] - 1 connections initialized for /127.0.0.1:8004
[2022-03-17 18:03:34:639] - /127.0.0.1:8001 master and related slaves: [addr=redis://127.0.0.1:8004] removed
[2022-03-17 18:03:34:641] - 24 connections initialized for /127.0.0.1:8004
[2022-03-17 18:03:34:655] - 1 connections initialized for /127.0.0.1:8004
[2022-03-17 18:03:34:678] - master: redis://127.0.0.1:8004 added for slot ranges: [[0-5460]]
[2022-03-17 18:03:34:678] - 24 connections initialized for /127.0.0.1:8004

從打印的日志來(lái)看,客戶端已經(jīng)感知到了主備切換,并與最新的主節(jié)點(diǎn) 127.0.0.1:8004 初始化了 24 個(gè)連接。

然后,回歸業(yè)務(wù)功能,讀寫緩存 數(shù)據(jù)也都是操作最新的主節(jié)點(diǎn)。

還有一種方案:刷新節(jié)點(diǎn)拓?fù)湟晥D。

Lettuce 官方描述:

https://github.com/lettuce-io/lettuce-core/wiki/Redis-Cluster#user-content-refreshing-the-cluster-topology-view。

Lettuce 處理 Moved 和 Ask 永久重定向,由于命令重定向,必須刷新節(jié)點(diǎn)拓?fù)湟晥D。而自適應(yīng)拓?fù)渌⑿?Adaptive updates)與定時(shí)拓?fù)渌⑿?Periodic updates)默認(rèn)關(guān)閉。

解決方案:

  • 調(diào)用 RedisClusterClient.reloadPartitions。
  • 后臺(tái)基于時(shí)間間隔的周期刷新。
  • 后臺(tái)基于持續(xù)的斷開 和 移動(dòng)、重定向 的自適應(yīng)更新。

編寫代碼

@Bean(destroyMethod = "destroy")
public LettuceConnectionFactory lettuceConnectionFactory() {
// 應(yīng)
ClusterTopologyRefreshOptions clusterTopologyRefreshOptions = ClusterTopologyRefreshOptions.builder()
// 應(yīng)。,Redis會(huì)導(dǎo)
.enableAllAdaptiveRefreshTriggers()
// 應(yīng)時(shí)時(shí)(認(rèn)30)
.adaptiveRefreshTriggersTimeout(Duration.ofSeconds(30))
//
.enablePeriodicRefresh(Duration.ofSeconds(20))
.build();
ClientOptions clientOptions = ClusterClientOptions.builder()
.topologyRefreshOptions(clusterTopologyRefreshOptions)
.build();
LettuceClientConfiguration clientConfig = LettucePoolingClientConfiguration.builder()
.poolConfig(genericObjectPoolConfig(redisProperties.getJedis().getPool()))
.clientOptions(clientOptions)
.commandTimeout(redisProperties.getTimeout()) //認(rèn)RedisURI.DEFAULT_TIMEOUT 60
.build();
List<String> clusterNodes = redisProperties.getCluster().getNodes();
Set<RedisNode> nodes = new HashSet<RedisNode>();
clusterNodes.forEach(address -> nodes.add(new RedisNode(address.split(":")[0].trim(), Integer.valueOf(address.split(":")[1]))));
RedisClusterConfiguration clusterConfiguration = new RedisClusterConfiguration();
clusterConfiguration.setClusterNodes(nodes);
clusterConfiguration.setPassword(RedisPassword.of(redisProperties.getPassword()));
clusterConfiguration.setMaxRedirects(redisProperties.getCluster().getMaxRedirects());
LettuceConnectionFactory lettuceConnectionFactory = new LettuceConnectionFactory(clusterConfiguration, clientConfig);
// 個(gè)個(gè),認(rèn)true,false 個(gè)創(chuàng)
// lettuceConnectionFactory.setShareNativeConnection(false);
// , 來(lái)問(wèn)時(shí)
// lettuceConnectionFactory.resetConnection();
return lettuceConnectionFactory;
}
責(zé)任編輯:姜華 來(lái)源: 微觀技術(shù)
相關(guān)推薦

2024-01-17 19:05:44

mget優(yōu)化數(shù)據(jù)庫(kù)

2011-06-08 14:30:54

SkypeWindows微軟

2024-09-11 09:50:22

2023-09-27 06:26:07

2010-12-30 12:13:03

Skype宕機(jī)Windows客戶端漏

2011-08-17 10:10:59

2021-09-22 15:46:29

虛擬桌面瘦客戶端胖客戶端

2023-10-26 07:47:53

Redis哨兵集群

2011-10-26 13:17:05

2011-03-24 13:00:31

配置nagios客戶端

2011-03-02 14:36:24

Filezilla客戶端

2010-12-21 11:03:15

獲取客戶端證書

2010-05-31 10:11:32

瘦客戶端

2024-03-07 16:03:56

RedisDocker

2011-03-21 14:53:36

Nagios監(jiān)控Linux

2011-04-06 14:24:20

Nagios監(jiān)控Linux

2013-05-09 09:33:59

2009-03-04 10:27:50

客戶端組件桌面虛擬化Xendesktop

2013-03-20 11:01:37

Redis客戶端連接

2021-08-01 23:18:21

Redis Golang命令
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)