欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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里調(diào)整一下依賴的引用

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

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)) //默認關(guān)閉開啟后時間為30秒
                // 開周期刷新
                .enablePeriodicRefresh(Duration.ofSeconds(20))  // 默認關(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-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)容,更多關(guān)于Redis集群Lettuce主從切換的資料請關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

  • Redis全文搜索教程之創(chuàng)建索引并關(guān)聯(lián)源數(shù)據(jù)的教程

    Redis全文搜索教程之創(chuàng)建索引并關(guān)聯(lián)源數(shù)據(jù)的教程

    RediSearch提供了一種簡單快速的方法對 hash 或者 json 類型數(shù)據(jù)的任何字段建立二級索引,然后就可以對被索引的 hash 或者 json 類型數(shù)據(jù)字段進行搜索和聚合操作,這篇文章主要介紹了Redis全文搜索教程之創(chuàng)建索引并關(guān)聯(lián)源數(shù)據(jù),需要的朋友可以參考下
    2023-12-12
  • 基于Redis 實現(xiàn)網(wǎng)站PV/UV數(shù)據(jù)統(tǒng)計

    基于Redis 實現(xiàn)網(wǎng)站PV/UV數(shù)據(jù)統(tǒng)計

    PV和UV是兩個重要的指標,本文主要介紹了基于Redis 實現(xiàn)網(wǎng)站PV/UV數(shù)據(jù)統(tǒng)計,具有一定的參考價值,感興趣的可以了解一下
    2025-04-04
  • Redis實現(xiàn)短信驗證碼登錄的示例代碼

    Redis實現(xiàn)短信驗證碼登錄的示例代碼

    本文主要介紹了基于Redis如何實現(xiàn)短信驗證碼登錄功能,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2022-06-06
  • 如何自定義redis工具jar包供其他SpringBoot項目直接使用

    如何自定義redis工具jar包供其他SpringBoot項目直接使用

    這篇文章主要介紹了如何自定義redis工具jar包供其他SpringBoot項目直接使用,本文通過實例代碼給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2021-03-03
  • Redis中一些最常見的面試問題總結(jié)

    Redis中一些最常見的面試問題總結(jié)

    Redis在互聯(lián)網(wǎng)技術(shù)存儲方面使用如此廣泛,幾乎所有的后端技術(shù)面試官都要在Redis的使用和原理方面對小伙伴們進行各種刁難。下面這篇文章主要給大家總結(jié)介紹了關(guān)于Redis中一些最常見的面試問題,需要的朋友可以參考下
    2018-09-09
  • Redis緩存工具封裝實現(xiàn)

    Redis緩存工具封裝實現(xiàn)

    本文主要介紹了Redis緩存工具封裝實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2023-01-01
  • CentOS系統(tǒng)安裝Redis及Redis的PHP擴展詳解

    CentOS系統(tǒng)安裝Redis及Redis的PHP擴展詳解

    這篇文章主要介紹了CentOS系統(tǒng)下安裝Redis數(shù)據(jù)的教程,以及詳解了Redis數(shù)據(jù)庫的PHP擴展,文中介紹的很詳細,相信對大家的理解和學習具有一定的參考借鑒價值,有需要的朋友們可以參考借鑒,下面來一起看看吧。
    2016-12-12
  • Redis哨兵模式實現(xiàn)一主二從三哨兵

    Redis哨兵模式實現(xiàn)一主二從三哨兵

    本文主要介紹了Redis哨兵模式實現(xiàn)一主二從三哨兵,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2022-07-07
  • Redis連接錯誤的情況總結(jié)分析

    Redis連接錯誤的情況總結(jié)分析

    這篇文章主要給大家總結(jié)介紹了關(guān)于Redis連接錯誤的相關(guān)資料,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面來一起學習學習吧
    2019-02-02
  • Redis數(shù)據(jù)庫的安裝配置方法

    Redis數(shù)據(jù)庫的安裝配置方法

    redis 是一個高性能的key-value數(shù)據(jù)庫。 redis的出現(xiàn),很大程度補償了memcached這類keyvalue存儲的不足,在部 分場合可以對關(guān)系數(shù)據(jù)庫起到很好的補充作用。它提供了Python,Ruby,Erlang,PHP客戶端,使用很方便
    2014-06-06

最新評論