网站 502错误,网站营销看法,wordpress showposts,南宁网站建设设计制作问题描述
使用spring-data-redis后#xff0c;如果应用长达一段时间没访问#xff0c;新的请求使用redis时#xff0c;可能会报错#xff1a;io.lettuce.core.RedisException: java.io.IOException: 远程主机强迫关闭了一个现有的连接
解决方案
修改项目配置
# spring默…问题描述
使用spring-data-redis后如果应用长达一段时间没访问新的请求使用redis时可能会报错io.lettuce.core.RedisException: java.io.IOException: 远程主机强迫关闭了一个现有的连接
解决方案
修改项目配置
# spring默认使用lettuce详情参见LettuceConnectionConfiguration
spring.redis.client-typelettuce
spring.redis.lettuce.pool.minIdle1
spring.redis.lettuce.pool.maxIdle8
# 5分钟执行一次
spring.redis.lettuce.pool.timeBetweenEvictionRunsPT2M
spring.redis.lettuce.pool.enabledtrue
spring.redis.lettuce.shutdownTimeoutPT0.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 {Beanpublic RedisTemplateObject, Object redisTemplate(LettuceConnectionFactory redisConnectionFactory) {redisConnectionFactory.setShareNativeConnection(false);RedisTemplateObject, Object template new RedisTemplate();template.setConnectionFactory(redisConnectionFactory);return template;}Beanpublic StringRedisTemplate stringRedisTemplate(LettuceConnectionFactory redisConnectionFactory) {redisConnectionFactory.setShareNativeConnection(false);return new StringRedisTemplate(redisConnectionFactory);}
}// LettuceConnectionFactory.java中的源码
// 如果不修改sharedConnection为false, lettuce根本没使用自己配置的连接池
protected StatefulRedisConnectionbyte[], byte[] getSharedConnection() {return shareNativeConnection !isClusterAware()? (StatefulRedisConnection) getOrCreateSharedConnection().getConnection(): null;
}
public RedisConnection getConnection() {connection doCreateLettuceConnection(getSharedConnection(), connectionProvider, getTimeout(), getDatabase());
}