问题
公司的一个Springboot项目最近上线到生产环境,但是上去之后一直报redis连接超时的问题
2022-05-05 13:15:50 org.springframework.data.redis.RedisSystemException: Unknown redis exception; nested exception is java.lang.reflect.UndeclaredThrowableException
2022-05-05 13:15:50 at org.springframework.data.redis.FallbackExceptionTranslationStrategy.getFallback(FallbackExceptionTranslationStrategy.java:53) ~[spring-data-redis-2.1.18.RELEASE.jar!/:2.1.18.RELEASE]
2022-05-05 13:15:50 at org.springframework.data.redis.FallbackExceptionTranslationStrategy.translate(FallbackExceptionTranslationStrategy.java:43) ~[spring-data-redis-2.1.18.RELEASE.jar!/:2.1.18.RELEASE]
2022-05-05 13:15:50 at org.springframework.data.redis.connection.lettuce.LettuceConnection.convertLettuceAccessException(LettuceConnection.java:268) ~[spring-data-redis-2.1.18.RELEASE.jar!/:2.1.18.RELEASE]
2022-05-05 13:15:50 at org.springframework.data.redis.connection.lettuce.LettuceHashCommands.convertLettuceAccessException(LettuceHashCommands.java:471) ~[spring-data-redis-2.1.18.RELEASE.jar!/:2.1.18.RELEASE]
2022-05-05 13:15:50 at org.springframework.data.redis.connection.lettuce.LettuceHashCommands.hGet(LettuceHashCommands.java:172) ~[spring-data-redis-2.1.18.RELEASE.jar!/:2.1.18.RELEASE]
2022-05-05 13:15:50 at org.springframework.data.redis.connection.DefaultedRedisConnection.hGet(DefaultedRedisConnection.java:926) ~[spring-data-redis-2.1.18.RELEASE.jar!/:2.1.18.RELEASE]
2022-05-05 13:15:50 at org.springframework.data.redis.core.DefaultHashOperations.lambda$get$0(DefaultHashOperations.java:53) ~[spring-data-redis-2.1.18.RELEASE.jar!/:2.1.18.RELEASE]
2022-05-05 13:15:50 at org.springframework.data.redis.core.RedisTemplate.execute(RedisTemplate.java:225) ~[spring-data-redis-2.1.18.RELEASE.jar!/:2.1.18.RELEASE]
2022-05-05 13:15:50 at org.springframework.data.redis.core.RedisTemplate.execute(RedisTemplate.java:185) ~[spring-data-redis-2.1.18.RELEASE.jar!/:2.1.18.RELEASE]
2022-05-05 13:15:50 at org.springframework.data.redis.core.AbstractOperations.execute(AbstractOperations.java:96) ~[spring-data-redis-2.1.18.RELEASE.jar!/:2.1.18.RELEASE]
2022-05-05 13:15:50 at org.springframework.data.redis.core.DefaultHashOperations.get(DefaultHashOperations.java:53) ~[spring-data-redis-2.1.18.RELEASE.jar!/:2.1.18.RELEASE]
...
2022-05-05 13:15:50 Caused by: java.lang.reflect.UndeclaredThrowableException: null
2022-05-05 13:15:50 at com.sun.proxy.$Proxy162.hget(Unknown Source) ~[na:na]
2022-05-05 13:15:50 at org.springframework.data.redis.connection.lettuce.LettuceHashCommands.hGet(LettuceHashCommands.java:170) ~[spring-data-redis-2.1.18.RELEASE.jar!/:2.1.18.RELEASE]
2022-05-05 13:15:50 ... 92 common frames omitted
2022-05-05 13:15:50 Caused by: java.lang.reflect.InvocationTargetException: null
2022-05-05 13:15:50 at sun.reflect.GeneratedMethodAccessor208.invoke(Unknown Source) ~[na:na]
2022-05-05 13:15:50 at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_331]
2022-05-05 13:15:50 at java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_331]
2022-05-05 13:15:50 at com.tencent.tsf.sleuth.instrument.redis.TracingRedisHandler.invoke(TracingRedisHandler.java:55) ~[spring-cloud-tsf-sleuth-1.24.0-Greenwich-RELEASE.jar!/:1.24.0-Greenwich-RELEASE]
2022-05-05 13:15:50 ... 94 common frames omitted
2022-05-05 13:15:50 Caused by: io.lettuce.core.RedisCommandTimeoutException: Command timed out after 1 second(s)
2022-05-05 13:15:50 at io.lettuce.core.ExceptionFactory.createTimeoutException(ExceptionFactory.java:51) ~[lettuce-core-5.1.8.RELEASE.jar!/:na]
2022-05-05 13:15:50 at io.lettuce.core.LettuceFutures.awaitOrCancel(LettuceFutures.java:114) ~[lettuce-core-5.1.8.RELEASE.jar!/:na]
2022-05-05 13:15:50 at io.lettuce.core.FutureSyncInvocationHandler.handleInvocation(FutureSyncInvocationHandler.java:69) ~[lettuce-core-5.1.8.RELEASE.jar!/:na]
2022-05-05 13:15:50 at io.lettuce.core.internal.AbstractInvocationHandler.invoke(AbstractInvocationHandler.java:80) ~[lettuce-core-5.1.8.RELEASE.jar!/:na]
2022-05-05 13:15:50 at com.sun.proxy.$Proxy159.hget(Unknown Source) ~[na:na]
2022-05-05 13:15:50 ... 98 common frames omitted
redis服务端的最大连接数已经设置到足够大,在项目刚启动时是能正常获取到redis连接的,但是当程序闲置几分钟后,后台日志就开始刷redis连接超时的问题
根源
没有配置redis连接池,导致每次需要访问redis时都需要去重新获取redis连接。
解决方案
使用jedis连接池
配置jedis连接池
引入依赖
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
</dependency>
yaml文件增加连接池配置
spring:
redis:
host: 127.0.0.1
port: 6379
password: edith
timeout: 5000
jedis:
pool:
max-active: 20
max-wait: 3000
max-idle: 10
min-idle: 8
Redis配置类
import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.PropertyAccessor;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisPassword;
import org.springframework.data.redis.connection.RedisStandaloneConfiguration;
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import redis.clients.jedis.JedisPoolConfig;
@Slf4j
@Configuration
public class RedisConfig {
@Value("${spring.redis.host}")
private String host;
@Value("${spring.redis.port}")
private Integer port;
@Value("${spring.redis.password}")
private String password;
@Value("${spring.redis.jedis.pool.max-active}")
private Integer maxActive;
@Value("${spring.redis.jedis.pool.max-idle}")
private Integer maxIdle;
@Value("${spring.redis.jedis.pool.max-wait}")
private Long maxWait;
@Value("${spring.redis.jedis.pool.min-idle}")
private Integer minIdle;
@Bean(name = "jedisConnectionFactory")
public JedisConnectionFactory jedisConnectionFactory() {
RedisStandaloneConfiguration jedisConfig = new RedisStandaloneConfiguration();
jedisConfig.setHostName(host);
jedisConfig.setPort(port);
jedisConfig.setPassword(RedisPassword.of(password));
JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();
jedisPoolConfig.setMaxIdle(maxIdle);
jedisPoolConfig.setMaxWaitMillis(maxWait);
jedisPoolConfig.setMaxTotal(maxActive);
jedisPoolConfig.setMinIdle(minIdle);
JedisConnectionFactory jedisConnectionFactory = new JedisConnectionFactory(jedisConfig);
jedisConnectionFactory.setPoolConfig(jedisPoolConfig);
log.info("【redis连接池初始化】>>>>>>>>>>> JedisPool注入成功!!");
log.info("【redis连接池初始化】>>>>>>>>>>> redis地址:{}:{}" , host , port);
return jedisConnectionFactory;
}
@Bean
public RedisTemplate<String,Object> redisTemplate(JedisConnectionFactory jedisConnectionFactory){
RedisTemplate<String, Object> template = new RedisTemplate<>();
template.setConnectionFactory(jedisConnectionFactory);
Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class);
ObjectMapper om = new ObjectMapper();
om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
jackson2JsonRedisSerializer.setObjectMapper(om);
StringRedisSerializer stringRedisSerializer = new StringRedisSerializer();
template.setKeySerializer(stringRedisSerializer);
template.setHashKeySerializer(stringRedisSerializer);
template.setValueSerializer(stringRedisSerializer);
template.setHashValueSerializer(stringRedisSerializer);
template.afterPropertiesSet();
return template;
}
}
写在最后
网上有种说法是
springboot 2.x 默认采用了lettuce作为连接池,但是lettuce是不会进行“心跳”操作的,也就是说,它不会保持连接,导致了连接超时。
笔者使没验证长期空闲lettuce连接超时的问题,有哪位大佬可以讨论一下这个东西?
|