redis长时间未请求,无法自动重连,报异常org.springframework.data.redis.RedisSystemException
disconnectedBehavior(ClientOptions.DisconnectedBehavior.REJECT_COMMANDS)// 断开连接时拒绝命令。.pingBeforeActivateConnection(true)// 连接前PING测试。.shutdownTimeout(Duration.ofSeconds(2))// 增加关闭超时时间。.commandTimeout(
org.springframework.data.redis.RedisSystemException: Redis exception; nested exception is io.lettuce.core.RedisException: io.netty.channel.unix.Errors$NativeIoException: readAddress(..) failed: Connection re
set by peer
at org.springframework.data.redis.connection.lettuce.LettuceExceptionConverter.convert(LettuceExceptionConverter.java:74)
添加配置
spring:
redis:
lettuce:
pool:
max-active: 8
max-idle: 8
min-idle: 2 # 保持最小空闲连接数
max-wait: 1000ms
time-between-eviction-runs: 30000 # 空闲连接检测周期(毫秒)
redisconfig配置增加
@Bean public LettuceConnectionFactory redisConnectionFactory() { RedisStandaloneConfiguration serverConfig = new RedisStandaloneConfiguration(host, port); serverConfig.setPassword(password); LettuceClientConfiguration clientConfig = LettuceClientConfiguration.builder() .commandTimeout(Duration.ofSeconds(5)) // 增加命令超时时间 .shutdownTimeout(Duration.ofSeconds(2)) // 增加关闭超时时间 .clientOptions(ClientOptions.builder() .autoReconnect(true) .disconnectedBehavior(ClientOptions.DisconnectedBehavior.REJECT_COMMANDS) // 断开连接时拒绝命令 .pingBeforeActivateConnection(true) // 连接前PING测试 .build()) .build(); LettuceConnectionFactory factory = new LettuceConnectionFactory(serverConfig, clientConfig); factory.setValidateConnection(true); // 在工厂级别设置连接验证 return factory; }

魔乐社区(Modelers.cn) 是一个中立、公益的人工智能社区,提供人工智能工具、模型、数据的托管、展示与应用协同服务,为人工智能开发及爱好者搭建开放的学习交流平台。社区通过理事会方式运作,由全产业链共同建设、共同运营、共同享有,推动国产AI生态繁荣发展。
更多推荐
所有评论(0)