原创

Spring-Redisson-解决redisson长时间未使用导致掉线后连接超时-pingConnectionInterval

redission版本:
<version>3.12.5</version>


import org.redisson.Redisson;
import org.redisson.api.RedissonClient;
import org.redisson.client.codec.StringCodec;
import org.redisson.config.Config;
import org.redisson.config.TransportMode;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;

/**
* 尝试解决redisson长时间未使用导致掉线后再次连接超时
*
* 关注要不要排除 RedissonAutoConfiguration
*
* [org/springframework/boot/autoconfigure/cache/RedisCacheConfiguration.class]:
* Unsatisfied dependency expressed through method 'cacheManager' parameter 4;
* nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException:
* Error creating bean with name 'redissonConnectionFactory'
* defined in class path resource [org/redisson/spring/starter/RedissonAutoConfiguration.class]:
* Unsatisfied dependency expressed through method 'redissonConnectionFactory' parameter 0;
* nested exception is org.springframework.beans.factory.NoUniqueBeanDefinitionException:
* No qualifying bean of type 'org.redisson.api.RedissonClient' available:
* expected single matching bean but found 2: redissonClient,singleRedisson
*
* https://www.freesion.com/article/2025821993/ (https://github.com/redisson/redisson/tree/master/redisson-spring-boot-starter)
* https://www.cnblogs.com/east7/p/16255253.html
* http://events.jianshu.io/p/84522c2736a2
* https://www.cnblogs.com/achengmu/p/14894493.html
*
* 验证代码:
* @Autowired
* private StringRedisTemplate stringRedisTemplate;
*
* String redisKey = "test#redissonInitByCode";
* stringRedisTemplate.opsForValue().set(redisKey, "redisson初始化Value");
* String redisValue = stringRedisTemplate.opsForValue().get(redisKey);
* log.info("代码初始化redisson成功,取值:{},如果要关闭代码初始化,配置:enable.redissonInitByCode=false",redisValue);
*
*/
@Component
@ConditionalOnProperty(name = "enable.redissonInitByCode", havingValue = "true", matchIfMissing = true)
public class RedissonClientConfig {

@Value("${spring.redis.host}")
private String host;
@Value("${spring.redis.port}")
private String port;
@Value("${spring.redis.password}")
private String password;
@Value("${spring.redis.pingConnectionInterval:3000}")
private int pingConnectionInterval;

// 下面两个只能用一个
// @Bean
// public RedissonClient redissonClient() {
// Config config = getConfig();
// return Redisson.create(config);
// }

@Bean(destroyMethod = "shutdown")
public RedissonClient singleRedisson() {
// 创建配置
Config config = getConfig();
return Redisson.create(config);
}


private Config getConfig(){
// 创建配置
Config config = new Config();
config.setCodec(StringCodec.INSTANCE);
config.setTransportMode(TransportMode.NIO);
config.useSingleServer()
.setAddress("redis://" + host + ":" + port)
.setPassword(password)
.setPingConnectionInterval(pingConnectionInterval);
return config;
}
}
正文到此结束
本文目录