In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-27 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)05/31 Report--
This article Xiaobian introduces in detail for you "SpringBoot integration redis client timeout how to solve", the content is detailed, the steps are clear, the details are handled properly, I hope this "SpringBoot integration redis client timeout how to solve" article can help you solve doubts, following the editor's ideas slowly in-depth, together to learn new knowledge.
problem
During development, use Lettuce to connect to redis, do not operate after a period of time, and then operate redis, which will report a connection timeout error and can be used again after reconnection.
The reason is that Lettuce adaptive topology refresh (Adaptive updates) and scheduled topology refresh (Periodic updates) are turned off by default, causing problems.
The first solution is:
1. Rewrite the connection factory instance and change its LettuceClientConfiguration to enable topology update
@ Configurationpublic class RedisConfig {@ Autowired private RedisProperties redisProperties; / / this is a fixed template / / define a RedisTemplate @ Bean @ SuppressWarnings ("all") public RedisTemplate redisTemplate (@ Qualifier ("lettuceConnectionFactoryUvPv") RedisConnectionFactory factory) {RedisTemplate template = new RedisTemplate (); template.setConnectionFactory (factory); / / Json serialization configuration Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer (Object.class) ObjectMapper om = new ObjectMapper (); om.setVisibility (PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY); om.activateDefaultTyping (om.getPolymorphicTypeValidator ()); om.enableDefaultTyping (ObjectMapper.DefaultTyping.NON_FINAL); / / solve serialization problems om.configure (DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false); jackson2JsonRedisSerializer.setObjectMapper (om); / / serialization StringRedisSerializer stringRedisSerializer of String = new StringRedisSerializer () / / key uses String serialization method template.setKeySerializer (stringRedisSerializer); / / hash key also uses String serialization method template.setHashKeySerializer (stringRedisSerializer); / / value serialization mode uses jackson template.setValueSerializer (jackson2JsonRedisSerializer); / / hash value serialization mode uses jackson template.setHashValueSerializer (jackson2JsonRedisSerializer); template.afterPropertiesSet (); return template } / * configure Redis connection factory implementation for RedisTemplate * LettuceConnectionFactory implements the RedisConnectionFactory interface * UVPV uses Redis * * @ return to return LettuceConnectionFactory * / @ Bean (destroyMethod = "destroy") / / it should be noted here that when building LettuceConnectionFactory, if you do not use the built-in destroyMethod May cause Redis connections to be destroyed before other Bean is destroyed public LettuceConnectionFactory lettuceConnectionFactoryUvPv () throws Exception {/ / List clusterNodes = redisProperties.getCluster () .getNodes () / / Set 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 ()); / / I use stand-alone redis, and the cluster uses the above annotated code Set nodes = new HashSet (); nodes.add (new RedisNode (redisProperties.getHost (), redisProperties.getPort (); 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)) } / * configuration LettuceClientConfiguration includes thread pool configuration and security item configuration * * @ param genericObjectPoolConfig common-pool2 thread pool * @ return lettuceClientConfiguration * / private LettuceClientConfiguration getLettuceClientConfiguration (GenericObjectPoolConfig genericObjectPoolConfig) {/ * ClusterTopologyRefreshOptions configuration is used to enable adaptive refresh and scheduled refresh. If adaptive refresh is not enabled, changes to the Redis cluster will result in abnormal connection! * / ClusterTopologyRefreshOptions topologyRefreshOptions = ClusterTopologyRefreshOptions.builder () / / enable adaptive refresh / / .enableAdaptiveRefresh trigger (ClusterTopologyRefreshOptions.RefreshTrigger.MOVED_REDIRECT, ClusterTopologyRefreshOptions.RefreshTrigger.PERSISTENT_RECONNECTS) / / enable all adaptive refresh, MOVED,ASK PERSISTENT triggers .enableAllAdaptiveRefresh Triggers () / Adaptive refresh timeout (default is 30 seconds). AdaptiveRefresh TriggersTimeout (Duration.ofSeconds (25)) / / default time after shutdown is 30 seconds / / Open cycle refresh. EnablePeripheral Refresh (Duration.ofSeconds (20)) / / turn it on by default The last time is 60 seconds ClusterTopologyRefreshOptions.DEFAULT_REFRESH_PERIOD 60. EnablePeripheral Refresh (Duration.ofSeconds (2)) = .enablePeripheral Refresh () .refreshPeriod (Duration.ofSeconds (2)) .build () Return LettucePoolingClientConfiguration.builder () .poolConfig (genericObjectPoolConfig) .clientOptions (ClusterClientOptions.builder (). TopologyRefreshOptions (topologyRefreshOptions). Build ()) / / transfer appID to the connection to facilitate viewing / / .clientName (appName + "_ lettuce") .build () in Redis monitoring;}}
2. After SpringBoot2.3.x, you can use the topology refresh with lettuce enabled in the configuration file.
Lettuce: pool: max-active: 20 max-wait:-1ms max-idle: 10 min-idle: 2 cluster: refresh: adaptive: true # automatically refresh once in 20 seconds period: 20 method 2:
Change the way you connect to the redis, using jedis to connect
Org.springframework.boot spring-boot-starter-data-redis io.lettuce lettuce-core redis.clients jedis
Configuration file
Spring: redis: password: xxx host: 172.16.0.x port: 6579 timeout: 5000 jedis: pool: # maximum number of database connections, set 0 to unlimited number of max-active: 8 # maximum waiting connections, set 0 to unlimited max-idle: 8 # maximum connection establishment waiting time. If you exceed this time, you will receive an exception. Set to-1 to indicate no limit. Max-wait:-1ms # the number of minimum waiting connections Let 0 be unlimited min-idle: 0 # 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} read here This article "how to solve the timeout of SpringBoot Integration redis client" has been introduced. If you want to master the knowledge points of this article, you still need to practice and use it yourself. If you want to know more about related articles, welcome to follow the industry information channel.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.