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

關(guān)于SpringBoot整合redis使用Lettuce客戶(hù)端超時(shí)問(wèn)題

 更新時(shí)間:2021年08月04日 11:09:03   作者:shouyaya  
使用到Lettuce連接redis,一段時(shí)間后不操作,再去操作redis,會(huì)報(bào)連接超時(shí)錯(cuò)誤,在其重連后又可使用,糾結(jié)是什么原因?qū)е碌哪?,下面小編給大家?guī)?lái)了SpringBoot整合redis使用Lettuce客戶(hù)端超時(shí)問(wèn)題及解決方案,一起看看吧

參考的博客

問(wèn)題起因

做畢設(shè)的時(shí)候,使用到Lettuce連接redis,一段時(shí)間后不操作,再去操作redis,會(huì)報(bào)連接超時(shí)錯(cuò)誤,在其重連后又可使用。

原因是:Lettuce 自適應(yīng)拓?fù)渌⑿拢ˋdaptive updates)與定時(shí)拓?fù)渌⑿拢≒eriodic updates) 是默認(rèn)關(guān)閉的導(dǎo)致問(wèn)題的出現(xiàn)

解決的方案

1、重寫(xiě)連接工廠實(shí)例,更改其LettuceClientConfiguration 為開(kāi)啟拓?fù)涓?/p>

@Configuration
public class RedisConfig {


    @Autowired
    private RedisProperties redisProperties;

    //這是固定的模板
    //自己定義了一個(gè)RedisTemplate
    @Bean
    @SuppressWarnings("all")
    public RedisTemplate<String, Object> redisTemplate(@Qualifier("lettuceConnectionFactoryUvPv") RedisConnectionFactory factory) {
        RedisTemplate<String, Object> template = new RedisTemplate<>();
        template.setConnectionFactory(factory);

        //Json序列化配置
        Jackson2JsonRedisSerializer<Object> jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer<Object>(Object.class);
        ObjectMapper om = new ObjectMapper();
        om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
        om.activateDefaultTyping(om.getPolymorphicTypeValidator());
        om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
        //解決序列化問(wèn)題
        om.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
        jackson2JsonRedisSerializer.setObjectMapper(om);

        //String的序列化
        StringRedisSerializer stringRedisSerializer = new StringRedisSerializer();

        //key采用String的序列化方式
        template.setKeySerializer(stringRedisSerializer);
        //hash的key也采用String的序列化方式
        template.setHashKeySerializer(stringRedisSerializer);

        //value序列化方式采用jackson
        template.setValueSerializer(jackson2JsonRedisSerializer);

        //hash的value序列化方式采用jackson
        template.setHashValueSerializer(jackson2JsonRedisSerializer);
        template.afterPropertiesSet();

        return template;
    }

    /**
     * 為RedisTemplate配置Redis連接工廠實(shí)現(xiàn)
     * LettuceConnectionFactory實(shí)現(xiàn)了RedisConnectionFactory接口
     * UVPV用Redis
     *
     * @return 返回LettuceConnectionFactory
     */
    @Bean(destroyMethod = "destroy")
    //這里要注意的是,在構(gòu)建LettuceConnectionFactory 時(shí),如果不使用內(nèi)置的destroyMethod,可能會(huì)導(dǎo)致Redis連接早于其它Bean被銷(xiāo)毀
    public LettuceConnectionFactory lettuceConnectionFactoryUvPv() throws Exception {

        List<String> clusterNodes = redisProperties.getCluster().getNodes();
        Set<RedisNode> nodes = new HashSet<>();
        clusterNodes.forEach(address -> nodes.add(new RedisNode(address.split(":")[0].trim(), Integer.parseInt(address.split(":")[1]))));
        RedisClusterConfiguration clusterConfiguration = new RedisClusterConfiguration();
        clusterConfiguration.setClusterNodes(nodes);
        clusterConfiguration.setPassword(RedisPassword.of(redisProperties.getPassword()));
        clusterConfiguration.setMaxRedirects(redisProperties.getCluster().getMaxRedirects());


        RedisStandaloneConfiguration  redisStandaloneConfiguration=new RedisStandaloneConfiguration();
        redisStandaloneConfiguration.setHostName(redisProperties.getHost());
        redisStandaloneConfiguration.setPassword(redisProperties.getPassword());
        redisStandaloneConfiguration.setDatabase(redisProperties.getDatabase());
        redisStandaloneConfiguration.setPort(redisProperties.getPort());

        GenericObjectPoolConfig poolConfig = new GenericObjectPoolConfig();
        poolConfig.setMaxIdle(redisProperties.getLettuce().getPool().getMaxIdle());
        poolConfig.setMinIdle(redisProperties.getLettuce().getPool().getMinIdle());
        poolConfig.setMaxTotal(redisProperties.getLettuce().getPool().getMaxActive());

        return new LettuceConnectionFactory(redisStandaloneConfiguration, getLettuceClientConfiguration(poolConfig));
    }

    /**
     * 配置LettuceClientConfiguration 包括線程池配置和安全項(xiàng)配置
     *
     * @param genericObjectPoolConfig common-pool2線程池
     * @return lettuceClientConfiguration
     */
    private LettuceClientConfiguration getLettuceClientConfiguration(GenericObjectPoolConfig genericObjectPoolConfig) {
        /*
        ClusterTopologyRefreshOptions配置用于開(kāi)啟自適應(yīng)刷新和定時(shí)刷新。如自適應(yīng)刷新不開(kāi)啟,Redis集群變更時(shí)將會(huì)導(dǎo)致連接異常!
         */
        ClusterTopologyRefreshOptions topologyRefreshOptions = ClusterTopologyRefreshOptions.builder()
                //開(kāi)啟自適應(yīng)刷新
                //.enableAdaptiveRefreshTrigger(ClusterTopologyRefreshOptions.RefreshTrigger.MOVED_REDIRECT, ClusterTopologyRefreshOptions.RefreshTrigger.PERSISTENT_RECONNECTS)
                //開(kāi)啟所有自適應(yīng)刷新,MOVED,ASK,PERSISTENT都會(huì)觸發(fā)
                .enableAllAdaptiveRefreshTriggers()
                // 自適應(yīng)刷新超時(shí)時(shí)間(默認(rèn)30秒)
                .adaptiveRefreshTriggersTimeout(Duration.ofSeconds(25)) //默認(rèn)關(guān)閉開(kāi)啟后時(shí)間為30秒
                // 開(kāi)周期刷新
                .enablePeriodicRefresh(Duration.ofSeconds(20))  // 默認(rèn)關(guān)閉開(kāi)啟后時(shí)間為60秒 ClusterTopologyRefreshOptions.DEFAULT_REFRESH_PERIOD 60  .enablePeriodicRefresh(Duration.ofSeconds(2)) = .enablePeriodicRefresh().refreshPeriod(Duration.ofSeconds(2))
                .build();
        return LettucePoolingClientConfiguration.builder()
                .poolConfig(genericObjectPoolConfig)
                .clientOptions(ClusterClientOptions.builder().topologyRefreshOptions(topologyRefreshOptions).build())
                //將appID傳入連接,方便Redis監(jiān)控中查看
                //.clientName(appName + "_lettuce")
                .build();
    }

}

2、SpringBoot2.3.x后,可使用配置文件中開(kāi)啟lettuce的拓?fù)渌⑿?/p>

lettuce:
      pool:
        max-active: 20
        max-wait: -1ms
        max-idle: 10
        min-idle: 2
      cluster:
        refresh:
          adaptive: true
          #20秒自動(dòng)刷新一次
          period: 20

3、更改連接redis的連接方式,使用jedis連接

<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-data-redis</artifactId>
			<exclusions>
				<exclusion>
					<groupId>io.lettuce</groupId>
					<artifactId>lettuce-core</artifactId>
				</exclusion>
			</exclusions>
		</dependency>
		
		<dependency>
			<groupId>redis.clients</groupId>
			<artifactId>jedis</artifactId>
		</dependency>
spring:
  redis:
    jedis:
      pool:
        max-active: ${redis.config.maxTotal:1024}
        max-idle: ${redis.config.maxIdle:50}
        min-idle: ${redis.config.minIdle:1}
        max-wait: ${redis.config.maxWaitMillis:5000}
    #lettuce:
      #pool:
        #max-active: ${redis.config.maxTotal:1024}
        #max-idle: ${redis.config.maxIdle:50}
        #min-idle: ${redis.config.minIdle:1}
        #max-wait: ${redis.config.maxWaitMillis:5000}

到此這篇關(guān)于SpringBoot整合redis使用Lettuce客戶(hù)端超時(shí)問(wèn)題的文章就介紹到這了,更多相關(guān)SpringBoot整合redis內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • java、springboot?接口導(dǎo)出txt方式

    java、springboot?接口導(dǎo)出txt方式

    這篇文章主要介紹了java、springboot?接口導(dǎo)出txt方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-01-01
  • 詳解Java的位操作符

    詳解Java的位操作符

    位操作就是對(duì)這些數(shù)據(jù)進(jìn)行基本的操作。如果基本類(lèi)型是char、byte或者short類(lèi)型的數(shù)值進(jìn)行移位處理,那么會(huì)轉(zhuǎn)化成int類(lèi)型,再進(jìn)行移位的處理
    2017-09-09
  • 解決spring-boot-maven-plugin報(bào)紅的問(wèn)題

    解決spring-boot-maven-plugin報(bào)紅的問(wèn)題

    這篇文章主要給大家介紹一下如何解決spring-boot-maven-plugin報(bào)紅的問(wèn)題,文中通過(guò)圖文講解的非常詳細(xì),具有一定的參考價(jià)值,需要的朋友可以參考下
    2023-08-08
  • 淺談Spring Boot中Redis緩存還能這么用

    淺談Spring Boot中Redis緩存還能這么用

    這篇文章主要介紹了淺談Spring Boot中Redis緩存還能這么用,這種方式是Spring Cache提供的統(tǒng)一接口,實(shí)現(xiàn)既可以是Redis,也可以是Ehcache或者其他支持這種規(guī)范的緩存框架,感興趣的小伙伴們可以參考一下
    2019-06-06
  • Spring中基于xml的聲明式事務(wù)示例詳解

    Spring中基于xml的聲明式事務(wù)示例詳解

    這篇文章主要介紹了Spring之基于xml的聲明式事務(wù),本文通過(guò)圖文實(shí)例代碼相結(jié)合給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2023-09-09
  • 詳解Spring 中 Bean 的生命周期

    詳解Spring 中 Bean 的生命周期

    這篇文章主要介紹了Spring 中 Bean 的生命周期的相關(guān)資料,幫助大家更好的理解和使用spring框架,感興趣的朋友可以了解下。
    2021-01-01
  • spring boot基于DRUID實(shí)現(xiàn)數(shù)據(jù)源監(jiān)控過(guò)程解析

    spring boot基于DRUID實(shí)現(xiàn)數(shù)據(jù)源監(jiān)控過(guò)程解析

    這篇文章主要介紹了spring boot基于DRUID實(shí)現(xiàn)數(shù)據(jù)源監(jiān)控過(guò)程解析,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2019-12-12
  • Java調(diào)用C++程序的實(shí)現(xiàn)方式

    Java調(diào)用C++程序的實(shí)現(xiàn)方式

    這篇文章主要介紹了Java調(diào)用C++程序的實(shí)現(xiàn)方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-02-02
  • 關(guān)于SpringBoot配置文件application.properties的路徑問(wèn)題

    關(guān)于SpringBoot配置文件application.properties的路徑問(wèn)題

    這篇文章主要介紹了關(guān)于SpringBoot配置文件application.properties的路徑問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-08-08
  • Spring Boot整合QueryDSL的實(shí)現(xiàn)示例

    Spring Boot整合QueryDSL的實(shí)現(xiàn)示例

    這篇文章主要介紹了Spring Boot整合QueryDSL的實(shí)現(xiàn)示例,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2020-09-09

最新評(píng)論

繁昌县| 彩票| 星子县| 永泰县| 温宿县| 磴口县| 济宁市| 西乌| 庄浪县| 鄂州市| 来安县| 庐江县| 本溪| 金门县| 皮山县| 栖霞市| 如东县| 鹤壁市| 台中市| 宁陕县| 万宁市| 观塘区| 明光市| 长丰县| 子洲县| 松潘县| 丰台区| 昌乐县| 和平县| 湖北省| 新乡市| 阜新市| 东台市| 太仆寺旗| 庆云县| 铜梁县| 岗巴县| 友谊县| 南和县| 呼玛县| 宽甸|