You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Spring Boot 3 Actuator Info端点随机返回GOAWAY问题排查求助

原因分析
  • HTTP/2连接回收机制触发:Spring Boot 3默认启用HTTP/2,服务器会对空闲过久的连接发送GOAWAY帧回收资源。你每30秒的调用间隔可能刚好触达服务器的空闲超时阈值,WebClient默认的连接池复用逻辑没及时检测到连接已被标记关闭,导致复用该连接时收到GOAWAY响应。
  • WebClient与RestTemplate的连接管理差异:RestTemplate默认使用HTTP/1.1,连接逻辑偏向简单复用或短连接;而WebClient基于Reactor Netty,HTTP/2下的多路复用连接管理更复杂,默认配置未适配这种高频低负载的定时调用场景。
  • 服务器端主动终止连接:服务器可能因负载波动、连接数限制等运维策略主动发送GOAWAY,通知客户端停止复用当前连接,但WebClient默认未处理这类通知,直接导致请求失败。
可行修复建议

1. 适配WebClient连接池的HTTP/2配置

自定义Reactor Netty的HttpClient,调整连接空闲时间,让客户端在服务器回收连接前主动处理连接生命周期:

@Bean
public WebClient webClient() {
    HttpClient httpClient = HttpClient.create()
            .http2()
            .keepAlive(true)
            .option(ChannelOption.CONNECT_TIMEOUT_MILLIS, 5000)
            .doOnConnected(conn -> conn
                    .addHandlerLast(new ReadTimeoutHandler(10))
                    .addHandlerLast(new WriteTimeoutHandler(10))
                    .option(ChannelOption.SO_KEEPALIVE, true)
                    .attr(AttributeKey.valueOf("maxIdleTime"), Duration.ofSeconds(25))); // 设置为短于服务器空闲超时时间

    return WebClient.builder()
            .clientConnector(new ReactorClientHttpConnector(httpClient))
            .build();
}

2. 添加GOAWAY异常的重试逻辑

在WebClient调用中加入针对性重试,自动处理HTTP/2连接相关异常:

@Scheduled(fixedRate = 30000, initialDelay = 60000)
public void pingInfoEndpoint() {
    String someResponse = webClient
            .get()
            .uri("https://my-endpoint.com/actuator/info")
            .retrieve()
            .bodyToMono(String.class)
            .retryWhen(Retry.backoff(3, Duration.ofMillis(500))
                    .filter(throwable -> throwable instanceof Http2Exception || 
                            (throwable instanceof IOException && throwable.getMessage().contains("GOAWAY"))))
            .block();
    // 处理响应逻辑
}

3. 调整服务器端HTTP/2配置

如果被调用的服务也是你的Spring Boot 3应用,可修改配置延长连接空闲时间:

server.http2.connection-timeout=35s # 设置长于客户端30秒的调用间隔
server.http2.max-concurrent-streams=100 # 根据实际并发需求调整

4. 配置HTTP版本自动降级

让WebClient优先使用HTTP/2,遇到GOAWAY等异常时自动降级到HTTP/1.1,无需全局强制HTTP/1.1:

@Bean
public WebClient webClient() {
    HttpClient httpClient = HttpClient.create()
            .protocol(HttpProtocol.H2, HttpProtocol.HTTP1_1) // 优先HTTP/2,失败自动降级
            .option(ChannelOption.CONNECT_TIMEOUT_MILLIS, 5000);

    return WebClient.builder()
            .clientConnector(new ReactorClientHttpConnector(httpClient))
            .build();
}

内容的提问来源于stack exchange,提问作者canpan14

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.25 20:20:55