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

如何修改Spring WebFlux中Actuator的health和Prometheus默认端点?

解决方案:适配旧监控工具的Spring WebFlux Actuator端点路径

方案一:直接配置Actuator端点路径(最优)

Spring Boot Actuator支持通过配置直接修改端点访问路径,无需额外编码。在application.yml中添加对应配置即可:

方式1:单独映射指定端点

完全匹配你的需求,将原生Actuator端点映射到监控工具期望的路径:

management:
  endpoints:
    web:
      exposure:
        include: health, info, prometheus  # 保持启用需要的端点
      path-mapping:
        health: /health        # 将/actuator/health映射为/health
        prometheus: /metrics   # 将/actuator/prometheus映射为/metrics
        info: /info            # 将/actuator/info映射为/info

方式2:修改Actuator基础路径为根路径

如果需要所有Actuator端点直接暴露在根路径下,可以用这个配置:

management:
  endpoints:
    web:
      base-path: /  # 把默认的/actuator基础路径改为根路径
      exposure:
        include: health, info, prometheus

配置后原/actuator/health变为/health,原/actuator/prometheus变为/prometheus,若需要/metrics对应prometheus,仍需搭配上面的path-mapping配置。

方案二:修复转发Controller(配置方式不可行时)

你之前的代码报错是因为Rendering是MVC视图渲染组件,@RestController会尝试将其序列化为JSON,但DefaultRendering无Jackson序列化规则,导致异常。且监控不支持重定向,需做内部代理转发,用WebClient实现:

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.reactive.function.client.WebClient;
import reactor.core.publisher.Mono;

@RestController
public class ObservabilityForwardConfig {

    private final WebClient webClient;

    // 注入WebClient,指向当前服务的Actuator端点
    public ObservabilityForwardConfig(WebClient.Builder webClientBuilder) {
        this.webClient = webClientBuilder.baseUrl("http://localhost:8080/actuator").build();
    }

    @GetMapping("/metrics")
    public Mono<String> forwardToMetrics() {
        return webClient.get()
                .uri("/prometheus")
                .retrieve()
                .bodyToMono(String.class);
    }

    @GetMapping("/health")
    public Mono<String> forwardToHealth() {
        return webClient.get()
                .uri("/health")
                .retrieve()
                .bodyToMono(String.class);
    }
}

注意:替换代码中的8080为服务实际端口,也可通过ServerHttpRequest动态获取当前服务地址,避免硬编码。

错误原因说明

你之前的Rendering.redirectTo()返回的DefaultRendering对象,被@RestController视为需序列化的响应体,但该类未定义Jackson序列化规则,导致Jackson无法生成JSON,抛出CodecException。且Rendering本身用于视图渲染场景,不适合WebFlux的RestController使用。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.17 10:25:25