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

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

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

一、問題描述

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

二、原因

SpringBoot2.X版本開始Redis默認(rèn)的連接池都是采用的Lettuce。當(dāng)節(jié)點(diǎn)發(fā)生改變后,Letture默認(rèn)是不會(huì)刷新節(jié)點(diǎn)拓?fù)涞摹?/p>

三、解決方案

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é)點(diǎn)拓?fù)湟晥D

Redis節(jié)點(diǎn)異常,服務(wù)端的Redis集群拓?fù)浔凰⑿铝耍琂ava程序沒有獲取到新的拓?fù)洹?/p>

Lettuce官方文檔中關(guān)于Redis Cluster的相關(guān)說(shuō)明:Lettuce處理Moved和Ask永久重定向,由于命令重定向,你必須刷新節(jié)點(diǎn)拓?fù)湟晥D。而自適應(yīng)拓?fù)渌⑿拢ˋdaptive updates)與定時(shí)拓?fù)渌⑿拢≒eriodic updates)是默認(rèn)關(guān)閉的,可以通過(guò)如下代碼打開。

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() {
        //開啟 自適應(yīng)集群拓?fù)渌⑿潞椭芷谕負(fù)渌⑿?
        ClusterTopologyRefreshOptions clusterTopologyRefreshOptions =  ClusterTopologyRefreshOptions.builder()
                // 開啟全部自適應(yīng)刷新
                .enableAllAdaptiveRefreshTriggers() // 開啟自適應(yīng)刷新,自適應(yīng)刷新不開啟,Redis集群變更時(shí)將會(huì)導(dǎo)致連接異常
                // 自適應(yīng)刷新超時(shí)時(shí)間(默認(rèn)30秒)
                .adaptiveRefreshTriggersTimeout(Duration.ofSeconds(30)) //默認(rèn)關(guān)閉開啟后時(shí)間為30秒
                // 開周期刷新
                .enablePeriodicRefresh(Duration.ofSeconds(20))  // 默認(rèn)關(guān)閉開啟后時(shí)間為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()) //默認(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); //是否允許多個(gè)線程操作共用同一個(gè)緩存連接,默認(rèn)true,false時(shí)每個(gè)操作都將開辟新的連接
        // lettuceConnectionFactory.resetConnection(); // 重置底層共享連接, 在接下來(lái)的訪問時(shí)初始化
        return lettuceConnectionFactory;
    }
}

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

相關(guān)文章

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

    ELK配置轉(zhuǎn)存redis緩存采集nginx訪問日志的操作方法

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

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

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

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

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

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

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

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

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

    Redis緩存過(guò)期淘汰策略用法解讀

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

    Redis主從復(fù)制問題和擴(kuò)容問題的解決思路

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

    詳解Redis開啟遠(yuǎn)程登錄連接

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

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

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

    Redis分布式鎖過(guò)期時(shí)間的設(shè)置策略和常見方案

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

最新評(píng)論

四平市| 澄城县| 米林县| 邵武市| 武宁县| 永康市| 巧家县| 安丘市| 嘉义县| 汝州市| 外汇| 陈巴尔虎旗| 玉屏| 牡丹江市| 高清| 灌阳县| 绩溪县| 武夷山市| 屯留县| 襄城县| 曲松县| 富川| 三明市| 多伦县| 乡城县| 罗甸县| 双桥区| 涟源市| 张家川| 台东县| 阿合奇县| 吉木乃县| 太白县| 曲靖市| 博客| 进贤县| 永州市| 稷山县| 台山市| 法库县| 黔江区|