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

您的位置:首頁(yè)/技術(shù)文章
文章詳情頁(yè)

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

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

參考的博客

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

@Configurationpublic 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序列化方式采用jacksontemplate.setValueSerializer(jackson2JsonRedisSerializer);//hash的value序列化方式采用jacksontemplate.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: 20max-wait: -1msmax-idle: 10min-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)搜索好吧啦網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持好吧啦網(wǎng)!

標(biāo)簽: Spring
相關(guān)文章:
主站蜘蛛池模板: 国产在线一区二区杨幂 | 欧美国产综合日韩一区二区 | 国产在线精品99一卡2卡 | 综合久久综合 | 99久久伊人一区二区yy5099 | 1级毛片 | 欧美+日本+国产+在线观看 | 国产美女在线观看 | 黄网在线观看网址入口 | 中文字幕不卡免费视频 | 黄色大片影院视频免费 | 尤物在线观看网站 | 高清不卡 | 妞干网这里只有精品 | 精品视频一区二区观看 | 青草国产在线视频 | 欧美成人午夜做爰视频在线观看 | 国产女乱淫真高清免费视频 | 黄色篇 | 欧美国产成人免费观看永久视频 | 久久精品美女久久 | 香蕉影视在线观看 | 精品精品精品 | 久热久色 | 在线看a级片 | 亚洲 欧美 国产 中文 | 久久成人综合 | 国产成年女一区二区三区 | yiren22开心综合成人网 | 国产精品永久在线 | 妖精www视频在线观看高清 | 久久综合一区 | 日韩精品亚洲人成在线播放 | v视界成人影院在线视频 | 99视频精品全国免费 | 欧美成人久久 | 久久aa毛片免费播放嗯啊 | 日本三级3本三级带黄 | 精品一区二区在线观看 1080p | 亚洲精品国产一区二区三 | 成年片人免费www |