问题描述
使用spring-data-redis后,如果应用长达一段时间没访问,新的请求使用redis时,可能会报错:io.lettuce.core.RedisException: java.io.IOException: 远程主机强迫关闭了一个现有的连接
解决方案
- 修改项目配置
# spring默认使用lettuce,详情参见:LettuceConnectionConfiguration
spring.redis.client-type=lettuce
spring.redis.lettuce.pool.minIdle=1
spring.redis.lettuce.pool.maxIdle=8
# 5分钟执行一次
spring.redis.lettuce.pool.timeBetweenEvictionRuns=PT2M
spring.redis.lettuce.pool.enabled=true
spring.redis.lettuce.shutdownTimeout=PT0.1S
- 修改redis.conf(可能不用改)
tcp-keepalive 60
- 修改LettuceConnectionFactory的shareNativeConnection参数
// 新增一个配置类,在创建RedisTemplate之前,修改LettuceConnectionFactory的shareNativeConnection参数
@Configuration(proxyBeanMethods = false)
@ConditionalOnProperty(name = "spring.redis.client-type", havingValue = "lettuce", matchIfMissing = true)
public class RedisAutoConfiguration {
@Bean
public RedisTemplate<Object, Object> redisTemplate(LettuceConnectionFactory redisConnectionFactory) {
redisConnectionFactory.setShareNativeConnection(false);
RedisTemplate<Object, Object> template = new RedisTemplate<>();
template.setConnectionFactory(redisConnectionFactory);
return template;
}
@Bean
public StringRedisTemplate stringRedisTemplate(LettuceConnectionFactory redisConnectionFactory) {
redisConnectionFactory.setShareNativeConnection(false);
return new StringRedisTemplate(redisConnectionFactory);
}
}
// LettuceConnectionFactory.java中的源码
// 如果不修改sharedConnection为false, lettuce根本没使用自己配置的连接池
protected StatefulRedisConnection<byte[], byte[]> getSharedConnection() {
return shareNativeConnection && !isClusterAware()
? (StatefulRedisConnection) getOrCreateSharedConnection().getConnection()
: null;
}
public RedisConnection getConnection() {
connection = doCreateLettuceConnection(getSharedConnection(), connectionProvider, getTimeout(), getDatabase());
}