最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Redis集群Lettuce主從切換問題解決方案

 更新時間:2023年07月09日 11:42:06   作者:AC編程  
這篇文章主要為大家介紹了Redis集群Lettuce主從切換問題解決方案,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪

一、問題描述

Redis Cluster集群,當master宕機,主從切換,客戶端報錯 timed out

二、原因

SpringBoot2.X版本開始Redis默認的連接池都是采用的Lettuce。當節(jié)點發(fā)生改變后,Letture默認是不會刷新節(jié)點拓撲的。

三、解決方案

3.1 方案一:把lettuce換成jedis

只需要在pom.xml里調整一下依賴的引用

      <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>

3.2 方案二:刷新節(jié)點拓撲視圖

Redis節(jié)點異常,服務端的Redis集群拓撲被刷新了,Java程序沒有獲取到新的拓撲。

Lettuce官方文檔中關于Redis Cluster的相關說明:Lettuce處理Moved和Ask永久重定向,由于命令重定向,你必須刷新節(jié)點拓撲視圖。而自適應拓撲刷新(Adaptive updates)與定時拓撲刷新(Periodic updates)是默認關閉的,可以通過如下代碼打開。

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

修改代碼如下

package com.montnets.common.redis;
import io.lettuce.core.ClientOptions;
import io.lettuce.core.cluster.ClusterClientOptions;
import io.lettuce.core.cluster.ClusterTopologyRefreshOptions;
import org.apache.commons.pool2.impl.GenericObjectPoolConfig;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.data.redis.RedisProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.data.redis.connection.RedisClusterConfiguration;
import org.springframework.data.redis.connection.RedisNode;
import org.springframework.data.redis.connection.RedisPassword;
import org.springframework.data.redis.connection.lettuce.LettuceClientConfiguration;
import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory;
import org.springframework.data.redis.connection.lettuce.LettucePoolingClientConfiguration;
import org.springframework.stereotype.Component;
import java.time.Duration;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
@Component
public class RedisPoolConfig {
    @Autowired
    private RedisProperties redisProperties;
    public GenericObjectPoolConfig<?> genericObjectPoolConfig(RedisProperties.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-Options
        ClientOptions clientOptions = ClusterClientOptions.builder()
                .topologyRefreshOptions(clusterTopologyRefreshOptions)
                .build();
        LettuceClientConfiguration clientConfig = LettucePoolingClientConfiguration.builder()
                .poolConfig(genericObjectPoolConfig(redisProperties.getJedis().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;
    }
}

以上就是Redis集群Lettuce主從切換問題解決方案的詳細內(nèi)容,更多關于Redis集群Lettuce主從切換的資料請關注腳本之家其它相關文章!

相關文章

  • ELK配置轉存redis緩存采集nginx訪問日志的操作方法

    ELK配置轉存redis緩存采集nginx訪問日志的操作方法

    本文介紹了在服務器上部署MySQL及如何啟動MySQL服務,并詳細說明了如何查找安裝軟件的日志文件位置,通過使用rpm命令查詢MySQL服務的日志文件位置,以及通過編輯Logstash配置文件來添加MySQL日志信息,感興趣的朋友一起看看吧
    2024-11-11
  • Redis中有序集合的內(nèi)部實現(xiàn)方式的詳細介紹

    Redis中有序集合的內(nèi)部實現(xiàn)方式的詳細介紹

    本文主要介紹了Redis中有序集合的內(nèi)部實現(xiàn)方式的詳細介紹,文中通過示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2022-03-03
  • Redis RDB快照持久化及寫操作禁止問題排查與解決

    Redis RDB快照持久化及寫操作禁止問題排查與解決

    本文主要介紹了Redis RDB快照持久化及寫操作禁止問題排查與解決,由于?stop-writes-on-bgsave-error?選項處于啟用狀態(tài),所以寫操作被禁止,下面就來介紹一下,感興趣的可以了解一下
    2025-04-04
  • Redis瞬時高并發(fā)秒殺方案總結

    Redis瞬時高并發(fā)秒殺方案總結

    本文講述了Redis瞬時高并發(fā)秒殺方案總結,具有很好的參考價值,感興趣的小伙伴們可以參考一下,具體如下:
    2018-05-05
  • Redis實現(xiàn)短信登錄的企業(yè)實戰(zhàn)

    Redis實現(xiàn)短信登錄的企業(yè)實戰(zhàn)

    本文主要介紹了Redis實現(xiàn)短信登錄的企業(yè)實戰(zhàn),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2022-07-07
  • Redis緩存過期淘汰策略用法解讀

    Redis緩存過期淘汰策略用法解讀

    這篇文章主要介紹了Redis緩存過期淘汰策略用法,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2026-03-03
  • Redis主從復制問題和擴容問題的解決思路

    Redis主從復制問題和擴容問題的解決思路

    這篇文章主要介紹了Redis主從復制問題和擴容問題的解決思路,其中擴容問題的解決思路來自Redis作者,需要的朋友可以參考下
    2014-06-06
  • 詳解Redis開啟遠程登錄連接

    詳解Redis開啟遠程登錄連接

    本篇文章主要介紹了Redis開啟遠程登錄連接,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-05-05
  • Redis集群設置maxmemory參數(shù)的實現(xiàn)

    Redis集群設置maxmemory參數(shù)的實現(xiàn)

    Redis集群中設置內(nèi)存限制需要為每個節(jié)點單獨配置maxmemory參數(shù),本文就來介紹一下Redis集群設置maxmemory參數(shù),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2026-01-01
  • Redis分布式鎖過期時間的設置策略和常見方案

    Redis分布式鎖過期時間的設置策略和常見方案

    分布式鎖過期時間的設置確實是個需要仔細權衡的問題,設置太短,可能業(yè)務還沒執(zhí)行完鎖就釋放了,導致數(shù)據(jù)錯亂,設置太長,萬一客戶端崩潰,其他進程又需要等待很久才能獲取鎖,下面我來為你梳理一下設置策略和常見方案,需要的朋友可以參考下
    2025-09-09

最新評論

温州市| 枣强县| 高邮市| 安康市| 元氏县| 墨竹工卡县| 孟津县| 南城县| 新泰市| 瑞安市| 喀什市| 龙川县| 宿迁市| 阳泉市| 金川县| 临清市| 绥芬河市| 河曲县| 东城区| 惠东县| 沁水县| 云梦县| 五家渠市| 株洲县| 时尚| 台江县| 五华县| 商都县| 广州市| 三门县| 宜宾市| 阳曲县| 临澧县| 六盘水市| 淮阳县| 库尔勒市| 云和县| 五华县| 永寿县| 阿拉善盟| 什邡市|