springboot整合redis集群master宕机后连接超时
生活随笔
收集整理的這篇文章主要介紹了
springboot整合redis集群master宕机后连接超时
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
前提:
#? ? ? ? 本文是在確保redis集群配置正確的情況下,連接超時的解決方案。
????????項目登錄認(rèn)證使用的是sa-token(這個不重要,主要說的是springboot和redis集群),最近應(yīng)甲方要求,需要做redis集群,在測試主從切換的時候發(fā)現(xiàn),redis的master雖然切換過來了,但是springboot連接redis還是請求的之前宕掉的節(jié)點ip,沒有更新過來。
原因:
????????SpringBoot2.X版本開始Redis默認(rèn)的連接池都是采用的Lettuce。當(dāng)節(jié)點發(fā)生改變后,Letture默認(rèn)是不會刷新節(jié)點拓?fù)涞?#xff0c;需要手動去刷新。
解決方案:
? ? ? ? 方案一、把lettuce換成jedis(我用的這個,親測好使)
?#????????切換jedis有個問題,就是在master宕機(jī),cluster升為master期間,15秒內(nèi)還是會報連接超時,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>方案二:(這個是網(wǎng)上摘得,沒有做測試,各位大佬有時間可以試試)
@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() {//開啟 自適應(yīng)集群拓?fù)渌⑿潞椭芷谕負(fù)渌⑿翪lusterTopologyRefreshOptions clusterTopologyRefreshOptions = ClusterTopologyRefreshOptions.builder()// 開啟全部自適應(yīng)刷新.enableAllAdaptiveRefreshTriggers() // 開啟自適應(yīng)刷新,自適應(yīng)刷新不開啟,Redis集群變更時將會導(dǎo)致連接異常// 自適應(yīng)刷新超時時間(默認(rèn)30秒).adaptiveRefreshTriggersTimeout(Duration.ofSeconds(30)) //默認(rèn)關(guān)閉開啟后時間為30秒// 開周期刷新 .enablePeriodicRefresh(Duration.ofSeconds(20)) // 默認(rèn)關(guān)閉開啟后時間為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()) //默認(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);// lettuceConnectionFactory.setShareNativeConnection(false); //是否允許多個線程操作共用同一個緩存連接,默認(rèn)true,false時每個操作都將開辟新的連接// lettuceConnectionFactory.resetConnection(); // 重置底層共享連接, 在接下來的訪問時初始化return lettuceConnectionFactory;}總結(jié)
以上是生活随笔為你收集整理的springboot整合redis集群master宕机后连接超时的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: docker 二进制安装
- 下一篇: 【无标题】RestHighLevelCl