日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

springboot整合redis集群master宕机后连接超时

發布時間:2025/3/19 编程问答 31 豆豆
生活随笔 收集整理的這篇文章主要介紹了 springboot整合redis集群master宕机后连接超时 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

前提:

#? ? ? ? 本文是在確保redis集群配置正確的情況下,連接超時的解決方案。

????????項目登錄認證使用的是sa-token(這個不重要,主要說的是springboot和redis集群),最近應甲方要求,需要做redis集群,在測試主從切換的時候發現,redis的master雖然切換過來了,但是springboot連接redis還是請求的之前宕掉的節點ip,沒有更新過來。

原因:

????????SpringBoot2.X版本開始Redis默認的連接池都是采用的Lettuce。當節點發生改變后,Letture默認是不會刷新節點拓撲的,需要手動去刷新。

解決方案:

? ? ? ? 方案一、把lettuce換成jedis(我用的這個,親測好使)

?#????????切換jedis有個問題,就是在master宕機,cluster升為master期間,15秒內還是會報連接超時,15秒后項目正常了,項目流量大的慎用。

<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-data-redis</artifactId><version>2.1.5.RELEASE</version><!-- 不用lettuce ,用jedis --><exclusions><exclusion><groupId>io.lettuce</groupId><artifactId>lettuce-core</artifactId></exclusion></exclusions></dependency><dependency><groupId>redis.clients</groupId><artifactId>jedis</artifactId><version>3.1.0-m4</version></dependency>

方案二:(這個是網上摘得,沒有做測試,各位大佬有時間可以試試)

@Autowiredprivate RedisProperties redisProperties;@Beanpublic GenericObjectPoolConfig<?> genericObjectPoolConfig(Pool properties) {GenericObjectPoolConfig<?> config = new GenericObjectPoolConfig<>();config.setMaxTotal(properties.getMaxActive());config.setMaxIdle(properties.getMaxIdle());config.setMinIdle(properties.getMinIdle());if (properties.getTimeBetweenEvictionRuns() != null) {config.setTimeBetweenEvictionRunsMillis(properties.getTimeBetweenEvictionRuns().toMillis());}if (properties.getMaxWait() != null) {config.setMaxWaitMillis(properties.getMaxWait().toMillis());}return config;}@Bean(destroyMethod = "destroy")public LettuceConnectionFactory lettuceConnectionFactory() {//開啟 自適應集群拓撲刷新和周期拓撲刷新ClusterTopologyRefreshOptions clusterTopologyRefreshOptions = ClusterTopologyRefreshOptions.builder()// 開啟全部自適應刷新.enableAllAdaptiveRefreshTriggers() // 開啟自適應刷新,自適應刷新不開啟,Redis集群變更時將會導致連接異常// 自適應刷新超時時間(默認30秒).adaptiveRefreshTriggersTimeout(Duration.ofSeconds(30)) //默認關閉開啟后時間為30秒// 開周期刷新 .enablePeriodicRefresh(Duration.ofSeconds(20)) // 默認關閉開啟后時間為60秒 ClusterTopologyRefreshOptions.DEFAULT_REFRESH_PERIOD 60 .enablePeriodicRefresh(Duration.ofSeconds(2)) = .enablePeriodicRefresh().refreshPeriod(Duration.ofSeconds(2)).build();// https://github.com/lettuce-io/lettuce-core/wiki/Client-OptionsClientOptions clientOptions = ClusterClientOptions.builder().topologyRefreshOptions(clusterTopologyRefreshOptions).build();LettuceClientConfiguration clientConfig = LettucePoolingClientConfiguration.builder().poolConfig(genericObjectPoolConfig(redisProperties.getLettuce().getPool()))//.readFrom(ReadFrom.MASTER_PREFERRED).clientOptions(clientOptions).commandTimeout(redisProperties.getTimeout()) //默認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);// lettuceConnectionFactory.setShareNativeConnection(false); //是否允許多個線程操作共用同一個緩存連接,默認true,false時每個操作都將開辟新的連接// lettuceConnectionFactory.resetConnection(); // 重置底層共享連接, 在接下來的訪問時初始化return lettuceConnectionFactory;}

總結

以上是生活随笔為你收集整理的springboot整合redis集群master宕机后连接超时的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。