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

Spring Webflux WebClient搭配Jetty HttpClient时指标丢失解决方案咨询

问题:Spring Webflux WebClient(Jetty HttpClient)指标采集失败

需求目标

使用Spring Webflux WebClient时,基于Jetty HttpClient(而非Netty)获取相关指标。

已尝试配置

Java配置

@Bean
@Primary
public WebClient getWebClient(final HttpClient httpClient) {
    return WebClient.builder()
            .baseUrl("http://some-host:8080/api/path")
            .defaultHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE)
            .clientConnector(new JettyClientHttpConnector(httpClient))
            .build();
}

@Bean
public HttpClient getHttpClient(final MeterRegistry registry) {
    final var sslContextFactory = new SslContextFactory.Client();
    sslContextFactory.setSslContext(getCustomSSLContext());
    final var clientConnector = new ClientConnector();
    clientConnector.setSslContextFactory(sslContextFactory);
    final var httpClient = new HttpClient(new HttpClientTransportDynamic(clientConnector));
    return httpClient;
}

Maven依赖

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-webflux</artifactId>
</dependency>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
    <groupId>io.micrometer</groupId>
    <artifactId>micrometer-registry-elastic</artifactId>
</dependency>
<dependency>
    <groupId>io.micrometer</groupId>
    <artifactId>micrometer-tracing-bridge-otel</artifactId>
</dependency>
<dependency>
    <groupId>io.opentelemetry</groupId>
    <artifactId>opentelemetry-exporter-otlp</artifactId>
</dependency>
<dependency>
    <groupId>org.eclipse.jetty</groupId>
    <artifactId>jetty-reactive-httpclient</artifactId>
</dependency>

当前问题

未获取到任何Webflux WebClient相关指标,也无Jetty相关指标。改用Netty作为客户端连接器时,可正常采集到reactor_netty相关指标。


解决方案

1. 添加Jetty Micrometer支持依赖

Jetty HttpClient的指标采集需要额外的jetty-micrometer模块,在Maven依赖中添加:

<dependency>
    <groupId>org.eclipse.jetty</groupId>
    <artifactId>jetty-micrometer</artifactId>
</dependency>

2. 配置Jetty HttpClient绑定指标注册表

修改getHttpClient方法,将MeterRegistry绑定到Jetty客户端,启用指标采集:

@Bean
public HttpClient getHttpClient(final MeterRegistry registry) {
    final var sslContextFactory = new SslContextFactory.Client();
    sslContextFactory.setSslContext(getCustomSSLContext());
    final var clientConnector = new ClientConnector();
    clientConnector.setSslContextFactory(sslContextFactory);
    
    final var httpClient = new HttpClient(new HttpClientTransportDynamic(clientConnector));
    // 绑定MeterRegistry到Jetty客户端,启用指标
    httpClient.addBean(new MeterRegistryListener(registry));
    httpClient.setMetricRegistry(registry);
    
    return httpClient;
}

3. 启用WebClient的观测支持

Spring Webflux的WebClient指标依赖观测框架,需要在构建WebClient时注入ObservationRegistry:

@Bean
@Primary
public WebClient getWebClient(final HttpClient httpClient, final ObservationRegistry observationRegistry) {
    return WebClient.builder()
            .baseUrl("http://some-host:8080/api/path")
            .defaultHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE)
            .clientConnector(new JettyClientHttpConnector(httpClient))
            // 启用观测,生成WebClient专属指标
            .observationRegistry(observationRegistry)
            .build();
}

4. 确认Actuator端点配置

在配置文件(如application.yml)中确保启用指标端点暴露:

management:
  endpoints:
    web:
      exposure:
        include: metrics,health
  metrics:
    tags:
      application: your-app-name # 可选:添加全局应用标签,方便指标区分

验证指标

启动应用后,访问/actuator/metrics端点,可查看以下核心指标:

  • jetty.client.requests.active:当前活跃的Jetty客户端请求数
  • jetty.client.requests.total:Jetty客户端处理的总请求数
  • webclient.requests:WebClient请求指标,包含状态码、目标URI等标签维度

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.22 10:15:00