亚洲精品久久久中文字幕-亚洲精品久久片久久-亚洲精品久久青草-亚洲精品久久婷婷爱久久婷婷-亚洲精品久久午夜香蕉

您的位置:首頁技術文章
文章詳情頁

關于SpringBoot整合redis使用Lettuce客戶端超時問題

瀏覽:23日期:2023-02-19 15:13:04

參考的博客

問題起因

做畢設的時候,使用到Lettuce連接redis,一段時間后不操作,再去操作redis,會報連接超時錯誤,在其重連后又可使用。

原因是:Lettuce 自適應拓撲刷新(Adaptive updates)與定時拓撲刷新(Periodic updates) 是默認關閉的導致問題的出現

解決的方案

1、重寫連接工廠實例,更改其LettuceClientConfiguration 為開啟拓撲更新

@Configurationpublic class RedisConfig { @Autowired private RedisProperties redisProperties; //這是固定的模板 //自己定義了一個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);//解決序列化問題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序列化方式采用jacksontemplate.setValueSerializer(jackson2JsonRedisSerializer);//hash的value序列化方式采用jacksontemplate.setHashValueSerializer(jackson2JsonRedisSerializer);template.afterPropertiesSet();return template; } /** * 為RedisTemplate配置Redis連接工廠實現 * LettuceConnectionFactory實現了RedisConnectionFactory接口 * UVPV用Redis * * @return 返回LettuceConnectionFactory */ @Bean(destroyMethod = 'destroy') //這里要注意的是,在構建LettuceConnectionFactory 時,如果不使用內置的destroyMethod,可能會導致Redis連接早于其它Bean被銷毀 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 包括線程池配置和安全項配置 * * @param genericObjectPoolConfig common-pool2線程池 * @return lettuceClientConfiguration */ private LettuceClientConfiguration getLettuceClientConfiguration(GenericObjectPoolConfig genericObjectPoolConfig) {/*ClusterTopologyRefreshOptions配置用于開啟自適應刷新和定時刷新。如自適應刷新不開啟,Redis集群變更時將會導致連接異常! */ClusterTopologyRefreshOptions topologyRefreshOptions = ClusterTopologyRefreshOptions.builder()//開啟自適應刷新//.enableAdaptiveRefreshTrigger(ClusterTopologyRefreshOptions.RefreshTrigger.MOVED_REDIRECT, ClusterTopologyRefreshOptions.RefreshTrigger.PERSISTENT_RECONNECTS)//開啟所有自適應刷新,MOVED,ASK,PERSISTENT都會觸發.enableAllAdaptiveRefreshTriggers()// 自適應刷新超時時間(默認30秒).adaptiveRefreshTriggersTimeout(Duration.ofSeconds(25)) //默認關閉開啟后時間為30秒// 開周期刷新.enablePeriodicRefresh(Duration.ofSeconds(20)) // 默認關閉開啟后時間為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監控中查看//.clientName(appName + '_lettuce').build(); }}

2、SpringBoot2.3.x后,可使用配置文件中開啟lettuce的拓撲刷新

lettuce: pool:max-active: 20max-wait: -1msmax-idle: 10min-idle: 2 cluster:refresh: adaptive: true #20秒自動刷新一次 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}

到此這篇關于SpringBoot整合redis使用Lettuce客戶端超時問題的文章就介紹到這了,更多相關SpringBoot整合redis內容請搜索好吧啦網以前的文章或繼續瀏覽下面的相關文章希望大家以后多多支持好吧啦網!

標簽: Spring
相關文章:
主站蜘蛛池模板: 一本之道无吗一二三区 | 国产成人综合久久亚洲精品 | 无遮挡一级毛片私人影院 | 日韩一级影院 | 国产成人亚洲精品老王 | 免费国产在线观看不卡 | 99久久综合狠狠综合久久男同 | 亚洲三级精品 | 一国产一级淫片a免费播放口 | 日韩免费毛片全部不收费 | 欧美乱妇欲仙欲死视频免费 | 未满十八18周岁禁止免费国产 | 国产精品1区 2区 3区 | 黄网在线观看 | 日韩黄色在线视频 | 香蕉亚洲精品一区二区 | 国产国语毛片 | 久草毛片 | 黑人解禁hd在线观看 | 一级毛片免费在线 | 九九在线观看免费视频 | 日本久久中文字幕精品 | 亚洲一区二区欧美日韩 | 亚洲综合久久久久久888 | 成人在线观看视频免费 | 欧美日韩大尺码免费专区 | 色多多在深夜释放自己黄 | 国产一级特黄aa大片免费 | 久久久久国产精品 | 中文字幕一区二区三区不卡 | 国产一区二区三区在线观看影院 | 亚洲人成在线免费观看 | 成人性视频免费网站 | 黄色的视频在线观看 | 精品国产午夜久久久久九九 | 三级免费网址 | 超级97碰碰碰碰久久久久最新 | 久爱www免费人成福利播放 | 国产一级做a爰片在线 | 亚洲码欧美码一区二区三区 | 亚洲综合激情另类图片专区 |