一直连着没问题,只要间隔一段时间不连,再去连的时候就会出现超时问题,超时几次后就可以连上,再去操作就没问题,但是再次间隔一段时间去连,又会出现超时问题。
所以,问题不是出在代码逻辑上,而是出现在lettuce连接池上面!
第一种. 简单直接 ,换jedis连接池(缺陷:还是没解决lettuce超时问题);
第二种. 找到超时原因 ,解决它;
第一步:添加maven依赖(排除lettuce,添加jedis)
org.springframework.boot
spring-boot-starter-data-redis
io.lettuce
lettuce-core
redis.clients
jedis
第二步:改配置
redis:
open: false # 是否开启redis缓存 true开启 false关闭
database: 0
host: 127.0.0.1
port: 6379
password: # 密码(默认为空)
timeout: 6000ms # 连接超时时长(毫秒)
jedis:
pool:
max-active: 1000 # 连接池最大连接数(使用负值表示没有限制)
max-wait: -1ms # 连接池最大阻塞等待时间(使用负值表示没有限制)
max-idle: 10 # 连接池中的最大空闲连接
min-idle: 5 # 连接池中的最小空闲连接
第三步:修改配置类
@Configuration
public class RedisConfig {
@Autowired
private RedisConnectionFactory factory;
@Bean
public RedisTemplate redisTemplate() {
RedisTemplate redisTemplate = new RedisTemplate<>();
redisTemplate.setKeySerializer(new StringRedisSerializer());
redisTemplate.setHashKeySerializer(new StringRedisSerializer());
redisTemplate.setHashValueSerializer(new StringRedisSerializer());
redisTemplate.setValueSerializer(new StringRedisSerializer());
redisTemplate.setConnectionFactory(factory);
return redisTemplate;
}
@Bean
public HashOperations hashOperations(RedisTemplate redisTemplate) {
return redisTemplate.opsForHash();
}
@Bean
public ValueOperations valueOperations(RedisTemplate redisTemplate) {
return redisTemplate.opsForValue();
}
@Bean
public ListOperations listOperations(RedisTemplate redisTemplate) {
return redisTemplate.opsForList();
}
@Bean
public SetOperations setOperations(RedisTemplate redisTemplate) {
return redisTemplate.opsForSet();
}
@Bean
public ZSetOperations zSetOperations(RedisTemplate redisTemplate) {
return redisTemplate.opsForZSet();
}
}
以上三步就可以了,这里贴出来一个redis工具类,省得来回找了;
@Component
public class RedisUtils {
@Autowired
private RedisTemplate redisTemplate;
@Autowired
private ValueOperations valueOperations;
@Autowired
private HashOperations hashOperations;
@Autowired
private ListOperations listOperations;
@Autowired
private SetOperations setOperations;
@Autowired
private ZSetOperations zSetOperations;
/** 默认过期时长,单位:秒 */
public final static long DEFAULT_EXPIRE = 60 * 60 * 24;
/** 不设置过期时长 */
public final static long NOT_EXPIRE = -1;
private final static Gson gson = new Gson();
public void set(String key, Object value, long expire){
valueOperations.set(key, toJson(value));
if(expire != NOT_EXPIRE){
redisTemplate.expire(key, expire, TimeUnit.SECONDS);
}
}
public void set(String key, Object value){
set(key, value, DEFAULT_EXPIRE);
}
public T get(String key, Class clazz, long expire) {
String value = valueOperations.get(key);
if(expire != NOT_EXPIRE){
redisTemplate.expire(key, expire, TimeUnit.SECONDS);
}
return value == null ? null : fromJson(value, clazz);
}
public T get(String key, Class clazz) {
return get(key, clazz, NOT_EXPIRE);
}
public String get(String key, long expire) {
String value = valueOperations.get(key);
if(expire != NOT_EXPIRE){
redisTemplate.expire(key, expire, TimeUnit.SECONDS);
}
return value;
}
public String get(String key) {
return get(key, NOT_EXPIRE);
}
public void delete(String key) {
redisTemplate.delete(key);
}
/**
* Object转成JSON数据
*/
private String toJson(Object object){
if(object instanceof Integer || object instanceof Long || object instanceof Float ||
object instanceof Double || object instanceof Boolean || object instanceof String){
return String.valueOf(object);
}
return gson.toJson(object);
}
/**
* JSON数据,转成Object
*/
private T fromJson(String json, Class clazz){
return gson.fromJson(json, clazz);
}
}
既然是长时间没有连导致的链接超时,那连一个就好了,就像心跳一样,这样链接池就不会关闭了;
搜来的方案先贴出来:(思路可行,后期校验)
1.定时校验来解决
/**
* 每隔2秒校验异常lettuce连接是否正常,解决长期空闲lettuce连接关闭但是netty不能及时监控到的问题
* @author hujun
*/
@Component
@Slf4j
public class LettuceConnectionValidTask {
@Autowired
private RedisConnectionFactory redisConnectionFactory;
@Scheduled(cron="0/2 * * * * ?")
public void task() {
if(redisConnectionFactory instanceof LettuceConnectionFactory){
LettuceConnectionFactory c=(LettuceConnectionFactory)redisConnectionFactory;
c.validateConnection();
}
}
}
2.开启获取连接的校验
@Component
@Slf4j
public class LettuceConnectionValidConfig implements InitializingBean {
@Autowired
private RedisConnectionFactory redisConnectionFactory;
@Override
public void afterPropertiesSet() throws Exception {
if(redisConnectionFactory instanceof LettuceConnectionFactory){
LettuceConnectionFactory c=(LettuceConnectionFactory)redisConnectionFactory;
c.setValidateConnection(true);
}
}
}