Spring-Redisson-解决redisson长时间未使用导致掉线后连接超时-pingConnectionInterval
redission版本:
<version>3.12.5</version>
--------------------------------------------------------------------------------
package cn.jiangjiesheng.eduinp.config;
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;
}
//Redis server response timeout (3000 ms) occured after 3 retry attempts.
// Increase nettyThreads and/or timeout settings.
// Try to define pingConnectionInterval setting. Command: (PSETEX),
// params: [inp:memauthtoken:526f2598-ef90-47b5-ac0f-59bdd4085c98, 43200000,
// PooledUnsafeDirectByteBuf(ridx: 0, widx: 190, cap: 256)], channel: [id: 0xcb3cffbe, L:/10.130.9.8:39096 - R:/10.81.47.139:6379]","result":null,"trace":"[org.redisson.command.RedisExecutor$3.run(RedisExecutor.java:336),
// io.netty.util.HashedWheelTimer$HashedWheelTimeout.expire(HashedWheelTimer.java:669),
// io.netty.util.HashedWheelTimer$HashedWheelBucket.expireTimeouts(HashedWheelTimer.java:744),
// io.netty.util.HashedWheelTimer$Worker.run(HashedWheelTimer.java:469),
// io.netty.util.concurrent.FastThreadLocalRunnable.run(FastThreadLocalRunnable.java:30),
// java.lang.Thread.run(Thread.java:750)]
// RedisResponseTimeoutException:
// Redis server response timeout (3000 ms) occured after 3 retry attempts. lncrease nettyThreads and/or timeoutsettings.
// Try to define pingConnectionlnterval setting. Command:(EXISTS), params: [[105, 110, 112, 58, 108, 101, 106, 105, 110, 98, ...l,
// channel [id: 0x38ec1048, L:/172.17.0.23:58162 - R:redis/10.32.21.33:6379]
//
// [orc.redisson.spring.data.connection.RedissonExceptionConverter.convert(RedissonExceptionConverterjava:48).
// orgedisson.spring.data.connection.RedissonExceptionConverter.convert(RedissonExceptionConverter;java:35).
// org springframework.data.redis.PassThroughExceptionTranslationStrategy.translate(PassThroughExceptionTranslationStrategyjava:44)
// orgredisson.spring.data.connection.RedissonConnection.transform(RedissonConnection.java:216)
// orgredisson.spring.data.connection.RedissonConnectionFuture(RedissonConnection.java:211)
// orgedisson.spring.data.connection.RedissorRedissonConnectioniava:372)
// oroedisson.sina.data.connectionRedissononnectionedissonconnection,iava:?53)
// orgedisson.sring.data.connection.Redisson(on.existsRedissonConnection.iava:225)
// org.springiramework.data.redis.connection.DefaultstrincRedisConnectionexists(DefaultstrinaRedisConnection.
// java:336)*org.springframework.data.redis.coreRedisTemplate.lamboa$hasKey$6(RedisTemplate.java:768)
// org.springframework.data.redis.coreRedisTemplate.execute(RedisTemplate.java:222)
// org.springframework.data.redis.coreRedisTemplate.execue(RedisTemplate.java:189)
// org.springframework.data.redis.coreRedisTemplate.hasKe(RedisTemplate.java:768)
// cn.jiangjiesheng.eduinp.service.org.OrgService.saveUserldToRedis(OrgService.java:185)
// cn.jiangjiesheng.eduinp.service.enterprise.EduinpEnterpriseService.createlnpAdminUser(EduinpEnterpriseService,ava:394)
//
}
- 本文标签: Spring Spring Boot
- 本文链接: https://code.jiangjiesheng.cn/article/199
- 版权声明: 本文由小江同学原创发布,转载请先联系本站长,谢谢。