In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-02 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)05/31 Report--
This article mainly explains "Springboot2.x integration lettuce connection redis cluster report timeout exception how to solve", interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Next let the editor to take you to learn "Springboot2.x integrated lettuce connection redis cluster report timeout exception how to solve" it!
Background: recently, in the stress test of a newly developed Springboot system, it was found that the redis cluster could access data normally at the beginning of the stress test, but after pausing for a few minutes and then continuing to do the pressure test with jmeter, it was found that redis suddenly began to give an abnormal hint: Command timed out after 6 second (s).
1 Caused by: io.lettuce.core.RedisCommandTimeoutException: Command timed out after 6 second (s) 2 at io.lettuce.core.ExceptionFactory.createTimeoutException (ExceptionFactory.java:51) 3 at io.lettuce.core.LettuceFutures.awaitOrCancel (LettuceFutures.java:114) 4 at io.lettuce.core.cluster.ClusterFutureSyncInvocationHandler.handleInvocation (ClusterFutureSyncInvocationHandler.java:123) 5 at io.lettuce.core.internal.AbstractInvocationHandler.invoke (AbstractInvocationHandler.java:80) 6 at com.sun.proxy. $Proxy134.mget (Unknown Source) 7 at org.springframework.data.redis.connection.lettuce.LettuceStringCommands.mGet (LettuceStringCommands.java:119) 8... 15 common frames omitted
I hastened to check the redis cluster and found that all the nodes in the cluster were normal, and the cpu and memory utilization was less than 20%. Looking at all this, I suddenly fell into a long meditation on what went wrong. Baidu some time, found that many people have had a similar situation, some people say that the timeout timeout setting larger can be solved. I followed this solution and set the timeout timeout to a higher value, but still did not solve the timeout problem.
Where the dependency package for springboot to operate redis is--
1 2 org.springframework.boot 3 spring-boot-starter-data-redis 4
Cluster configuration--
1 redis: 2 timeout: 6000ms 3 cluster: 4 nodes: 5-xxx.xxx.x.xxx:6379 6-xxx.xxx.x.xxx:6379 7-xxx.xxx.x.xxx:6379 8 jedis: 9 pool: 10 max-active: 1000 11 max-idle: 10 12 min-idle: 5 13 max-wait:-1
Click into spring-boot-starter-data-redis and find that it contains lettuce dependencies:
Jedis is used by default for springboot1.x, and lettuce is used by default when it comes to Springboot2.x. We can simply verify that, in the redis driver loading configuration class, output the RedisConnectionFactory information:
1 @ Configuration 2 @ AutoConfigureAfter (RedisAutoConfiguration.class) 3 public class Configuration {4 @ Bean 5 public StringRedisTemplate redisTemplate (RedisConnectionFactory factory) {6 log.info ("Test print driver types:" + factory); 7}
Printout-
Test print driver type: org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory@74ee761e
It can be seen that the lettuce driver connection is used here, so the Command timed out after 6 second (s) problem no longer occurs when it is replaced with the previously used jedis driver connection.
12 org.springframework.boot 3 spring-boot-starter-data-redis 4 5 6 io.lettuce 7 lettuce-core 8 9 10 11 12 redis.clients 13 jedis 14
So the question is, how does Springboot2.x use lettuce by default? you have to study some of the code. We can go to the redis section of the Springboot2.x auto-assembly module, where there is a RedisAutoConfiguration class, which is mainly used to automatically configure the connection redis class for Springboot:
1 @ Configuration (2 proxyBeanMethods = false 3) 4 @ ConditionalOnClass ({RedisOperations.class}) 5 @ EnableConfigurationProperties ({RedisProperties.class}) 6 @ Import ({LettuceConnectionConfiguration.class, JedisConnectionConfiguration.class}) 7 public class RedisAutoConfiguration {8 public RedisAutoConfiguration () {9} 10. Omit 11}
You only need to pay attention to one line of comments here:
1 2 @ Import ({LettuceConnectionConfiguration.class, JedisConnectionConfiguration.class}) 3
This means that both lettuce and jedis drivers can be imported automatically when using spring-boot-starter-data-redis dependencies. In theory, there will not be two drivers at the same time, so it doesn't make much sense, so the order here is very important. Why do you say that?
Go to LettuceConnectionConfiguration.class and JedisConnectionConfiguration.class respectively to show the core code involved in this article:
1 / / LettuceConnectionConfiguration 2 @ ConditionalOnClass ({RedisClient.class}) 3 class LettuceConnectionConfiguration extends RedisConnectionConfiguration {4. Omit 5 @ Bean 6 @ ConditionalOnMissingBean ({RedisConnectionFactory.class}) 7 LettuceConnectionFactory redisConnectionFactory (ObjectProvider builderCustomizers, ClientResources clientResources) throws UnknownHostException {8 LettuceClientConfiguration clientConfig = this.getLettuceClientConfiguration (builderCustomizers, clientResources, this.getProperties (). GetLettuce (). GetPool ()); 9 return this.createLettuceConnectionFactory (clientConfig); 10} 11} 12 / / JedisConnectionConfiguration 13 @ ConditionalOnClass ({GenericObjectPool.class, JedisConnection.class, Jedis.class}) 14 class JedisConnectionConfiguration extends RedisConnectionConfiguration {15. Omit 16 @ Bean 17 @ ConditionalOnMissingBean ({RedisConnectionFactory.class}) 18 JedisConnectionFactory redisConnectionFactory (ObjectProvider builderCustomizers) throws UnknownHostException {19 return this.createJedisConnectionFactory (builderCustomizers); 20} 21} 22
It can be seen that both LettuceConnectionConfiguration.class and JedisConnectionConfiguration.class have the same annotation @ ConditionalOnMissingBean ({RedisConnectionFactory.class}), which means that if the RedisConnectionFactory bean has been registered in the container, other Bean similar to it will no longer be loaded with registration. To put it simply, add @ ConditionalOnMissingBean ({RedisConnectionFactory.class}) annotation to LettuceConnectionConfiguration and JedisConnectionConfiguration respectively. Only one of them can be loaded into the container, and the other will not load registration.
So, the question is, who will be registered first?
This goes back to the sentence mentioned above, the order in @ Import ({LettuceConnectionConfiguration.class, JedisConnectionConfiguration.class}) is critical, and LettuceConnectionConfiguration comes first, which means that LettuceConnectionConfiguration will be registered.
As you can see, Springboot uses lettuce to connect to redis by default.
When we introduce the spring-boot-starter-data-redis dependency package, it is tantamount to introducing the lettuce package, and then we will use the lettuce driver. If you do not want to use the default lettuce driver, you can simply rule out the lettuce dependency.
1 2 org.springframework.boot 3 spring-boot-starter-data-redis 4 5 6 io.lettuce 7 lettuce-core 8 9 10
And then introduce jedis dependency--
1 2 redis.clients 3 jedis 4
In this way, when you import annotations for RedisAutoConfiguration, because no lettuce dependencies are found, the JedisConnectionConfiguration in the second location of the annotation @ Import ({LettuceConnectionConfiguration.class, JedisConnectionConfiguration.class}) is valid and can be registered with the container as the driver of the springboot operation redis.
What's the difference between lettuce and jedis?
Lettuce: the underlying layer is implemented in netty, thread-safe, and there is only one instance by default.
Jedis: can be directly connected to the redis server. With connection pooling, physical connections can be increased.
Find the method that caused the error according to the exception hint, LettuceConverters.toBoolean (this.getConnection (). Zadd (key, score, value) in the following code--
1 public Boolean zAdd (byte [] key, double score, byte [] value) {2 Assert.notNull (key, "Key must not be null!"); 3 Assert.notNull (value, "Value must not be null!"); 4 5 try {6 if (this.isPipelined ()) {7 this.pipeline (this.connection.newLettuceResult (this.getAsyncConnection (). Zadd (key, score, value), LettuceConverters.longToBoolean () 8 return null; 9} else if (this.isQueueing ()) {10 this.transaction (this.connection.newLettuceResult (this.getAsyncConnection (). Zadd (key, score, value), LettuceConverters.longToBoolean ()); 11 return null; 12} else {13 return LettuceConverters.toBoolean (this.getConnection (). Zadd (key, score, value)) 14} 15} catch (Exception var6) {16 throw this.convertLettuceAccessException (var6); 17} 18}
LettuceConverters.toBoolean () converts long to Boolean. Normally, this.getConnection (). Zadd (key, score, value) returns 1 if the addition is successful, so LettuceConverters.toBoolean (1) gets true, otherwise, if the addition fails, it returns 0, that is, LettuceConverters.toBoolean (0). There is a third case, that is, this this.getConnection (). Zadd (key, score, value) method has an exception, under what circumstances will an exception occur? It should be when the connection connection fails.
At this point, I believe that everyone on the "Springboot2.x integrated lettuce connection redis cluster report timeout exception how to solve" have a deeper understanding, might as well to the actual operation of it! Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!
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.