Spring Boot集成Lettuce连接Azure Redis重连耗时异常问题咨询
现象说明
基于2.3.1-Release版本Spring Boot搭建的应用,集成Lettuce响应式Redis客户端,观测到重连时长异常:
- 应用部署在Azure平台、连接同环境Azure Redis缓存时,重启Redis服务实例后,应用需要超过15分钟才能完成重连
- 同一应用在本地环境运行、连接同一个Azure Redis实例时,Redis重启后应用仅需不到5分钟即可完成重连
目标是通过调整Azure平台或应用侧配置,实现Redis服务恢复可用后,应用可立即触发重连。
相关运行日志
19-10-2020 15:57:33.994 DEBUG r.n.http.server.HttpServerOperations [id: 0x6ef9d608, L:/10.0.0.111:8080 - R:/10.0.0.6:54100] Increasing pending responses, now 1 19-10-2020 15:57:33.994 DEBUG reactor.netty.http.server.HttpServer [id: 0x6ef9d608, L:/10.0.0.111:8080 - R:/10.0.0.6:54100] Handler is being applied: org.springframework.http.server.reactive.ReactorHttpHandlerAdapter@47bcb563 19-10-2020 15:57:33.999 DEBUG r.n.http.server.HttpServerOperations [id: 0x6ef9d608, L:/10.0.0.111:8080 - R:/10.0.0.6:54100] Detected non persistent http connection, preparing to close 19-10-2020 15:57:34.000 DEBUG r.n.http.server.HttpServerOperations [id: 0x6ef9d608, L:/10.0.0.111:8080 - R:/10.0.0.6:54100] Last HTTP response frame 19-10-2020 15:57:34.000 DEBUG r.n.http.server.HttpServerOperations [id: 0x6ef9d608, L:/10.0.0.111:8080 - R:/10.0.0.6:54100] Last HTTP packet was sent, terminating the channel 19-10-2020 15:58:03.994 DEBUG r.n.http.server.HttpServerOperations [id: 0xbfe5c284, L:/10.0.0.111:8080 - R:/10.0.0.6:54626] New http connection, requesting read 19-10-2020 15:58:03.994 DEBUG r.netty.channel.BootstrapHandlers [id: 0xbfe5c284, L:/10.0.0.111:8080 - R:/10.0.0.6:54626] Initialized pipeline DefaultChannelPipeline{(BootstrapHandlers$BootstrapInitializerHandler#0 = reactor.netty.channel.BootstrapHandlers$BootstrapInitializerHandler), (reactor.left.httpCodec = io.netty.handler.codec.http.HttpServerCodec), (reactor.left.httpTrafficHandler = reactor.netty.http.server.HttpTrafficHandler), (reactor.right.reactiveBridge = reactor.netty.channel.ChannelOperationsHandler)} 19-10-2020 15:58:03.994 DEBUG r.n.http.server.HttpServerOperations [id: 0xbfe5c284, L:/10.0.0.111:8080 - R:/10.0.0.6:54626] Increasing pending responses, now 1 19-10-2020 15:58:03.995 DEBUG reactor.netty.http.server.HttpServer [id: 0xbfe5c284, L:/10.0.0.111:8080 - R:/10.0.0.6:54626] Handler is being applied: org.springframework.http.server.reactive.ReactorHttpHandlerAdapter@47bcb563 19-10-2020 15:58:04.003 DEBUG r.n.http.server.HttpServerOperations [id: 0xbfe5c284, L:/10.0.0.111:8080 - R:/10.0.0.6:54626] Detected non persistent http connection, preparing to close 19-10-2020 15:58:04.004 DEBUG r.n.http.server.HttpServerOperations [id: 0xbfe5c284, L:/10.0.0.111:8080 - R:/10.0.0.6:54626] Last HTTP response frame
根因分析与调优方案
两个环境的重连时长差异,核心来自Azure平台网络层的TCP空闲截断规则,与Lettuce默认的连接探测、重连配置不匹配:
- 当Redis服务重启时,Azure平台的网络中转层会直接丢弃原有TCP连接的所有双向流量,但应用侧的空闲连接因为没有持续数据交互,无法第一时间感知连接已经失效,只能等待操作系统TCP栈的默认超时(Linux环境下通常为15分钟)触发,才会判定连接断开、发起重连
- 本地环境没有平台级的空闲TCP连接截断规则,感知连接失效的速度更快,因此重连时长比Azure环境更短。
具体调整方向
- 调整Lettuce的TCP Keepalive参数:修改Netty客户端的Keepalive探测间隔,从默认的2小时压缩到30~60秒,快速识别已经被平台层丢弃的死连接
- 开启Lettuce的定时连接有效性探测:配置30秒间隔的轻量
PING探测,一旦连接返回异常就立刻触发重连逻辑,无需等待TCP层超时 - 调整Lettuce的重连退避策略:把默认的长间隔指数退避调整为初始等待1秒、最大等待5秒的固定退避规则,确保Redis恢复后数秒内就能完成重连
- 检查Azure平台侧配置:确认Azure Redis的VNet集成、网络负载均衡的TCP空闲超时规则(默认5分钟),保持应用侧的Keepalive探测间隔小于平台空闲超时阈值,避免连接未被应用识别就被平台回收。
内容的提问来源于stack exchange,提问作者Rocky4Ever
相关产品推荐
相关产品推荐

