Spring Cloud Gateway Retry过滤器配置后仍不生效问题排查
Spring Cloud Gateway Retry过滤器不生效问题排查
核心问题分析
当目标服务(8081端口)停止时,网关实际触发的异常/返回状态码和你配置的重试触发条件不匹配,导致重试逻辑未执行,具体原因如下:
异常类型不匹配:
Spring Cloud Gateway默认使用Netty作为底层HTTP客户端,连接已停止的服务时,抛出的异常是io.netty.channel.AbstractChannel$AnnotatedConnectException,而非你配置的java.net.ConnectException。原生ConnectException仅在使用JDK自带HTTP客户端时才会触发。状态码覆盖不全:
服务停止时,网关通常返回503 SERVICE_UNAVAILABLE(服务不可用)或504 GATEWAY_TIMEOUT(网关超时),而非你配置的500 INTERNAL_SERVER_ERROR,当前statuses配置无法覆盖这些场景。
修正后的配置示例
spring: cloud: gateway: httpclient: connect-timeout: 2000 # 缩短连接超时,快速触发异常 read-timeout: 5000 routes: - id: retry_test uri: http://localhost:8081 predicates: - Path=/location filters: - name: Retry args: retries: 3 statuses: INTERNAL_SERVER_ERROR, SERVICE_UNAVAILABLE, GATEWAY_TIMEOUT methods: GET exceptions: java.net.ConnectException, io.netty.channel.AbstractChannel$AnnotatedConnectException backoff: # 可选:添加退避策略,避免频繁重试 firstBackoff: 100ms maxBackoff: 500ms factor: 2 basedOnPreviousValue: false
额外排查点
- 检查过滤器执行顺序:如果有CircuitBreaker等过滤器在Retry之前执行,断路器提前触发短路会直接阻断重试逻辑。
- 确认Spring Cloud Gateway版本:不同版本的Retry过滤器参数存在细微差异,旧版本对异常类型的匹配规则更严格。
内容的提问来源于stack exchange,提问作者tin_tin
相关产品推荐
相关产品推荐

