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

如何将/actuator/health的全部健康检查信息发布到/actuator/prometheus?

解决方案

前置注意事项

  • 你当前使用的 Spring Boot 2.1.9 与 Spring Boot Starter Actuator 2.5.1 版本不兼容,两者主版本必须保持一致,否则会出现类不存在、方法签名不匹配等运行时异常,建议先将依赖版本统一为同一系列(如都升级为2.5.x或都降级为2.1.x)。
  • 确保配置文件中已开启对应健康检查和端点暴露:
# 开启数据库健康检查
management.health.db.enabled=true
# 开启LDAP健康检查
management.health.ldap.enabled=true
# 暴露health和prometheus端点
management.endpoints.web.exposure.include=health,prometheus

通用健康指标导出实现

直接创建如下配置类,会自动把所有HealthIndicator的状态(包含应用全局状态、数据库、LDAP等所有已开启的健康检查项)同步到Prometheus指标中:

import io.micrometer.core.instrument.Gauge;
import io.micrometer.core.instrument.MeterRegistry;
import io.micrometer.core.instrument.binder.MeterBinder;
import org.springframework.boot.actuate.health.Health;
import org.springframework.boot.actuate.health.HealthAggregator;
import org.springframework.boot.actuate.health.HealthIndicator;
import org.springframework.boot.actuate.health.Status;
import org.springframework.context.annotation.Configuration;
import java.util.Map;
import java.util.stream.Collectors;

@Configuration
public class HealthMetricsConfiguration implements MeterBinder {
    private final Map<String, HealthIndicator> healthIndicators;
    private final HealthAggregator healthAggregator;

    public HealthMetricsConfiguration(Map<String, HealthIndicator> healthIndicators, 
                                      HealthAggregator healthAggregator) {
        this.healthIndicators = healthIndicators;
        this.healthAggregator = healthAggregator;
    }

    @Override
    public void bindTo(MeterRegistry registry) {
        // 导出所有单个健康检查项的状态(包含数据库、LDAP、磁盘等)
        healthIndicators.forEach((indicatorName, indicator) -> 
            Gauge.builder("health.indicator.status", indicator, this::getIndicatorStatusValue)
                .tag("indicator", indicatorName)
                .description("健康检查项状态:1=正常(UP),0=不可用(DOWN/OUT_OF_SERVICE),-1=状态未知(UNKNOWN)")
                .register(registry)
        );

        // 导出应用全局聚合健康状态
        Gauge.builder("health.global.status", this::getGlobalStatusValue)
            .description("应用全局健康状态:1=正常(UP),0=不可用(DOWN/OUT_OF_SERVICE),-1=状态未知(UNKNOWN)")
            .register(registry);
    }

    private int getIndicatorStatusValue(HealthIndicator indicator) {
        return convertStatusToValue(indicator.health().getStatus());
    }

    private int getGlobalStatusValue() {
        Map<String, Health> healthMap = healthIndicators.entrySet().stream()
                .collect(Collectors.toMap(Map.Entry::getKey, entry -> entry.getValue().health()));
        Status globalStatus = healthAggregator.aggregate(healthMap).getStatus();
        return convertStatusToValue(globalStatus);
    }

    private int convertStatusToValue(Status status) {
        if (Status.UP.equals(status)) {
            return 1;
        } else if (Status.DOWN.equals(status) || Status.OUT_OF_SERVICE.equals(status)) {
            return 0;
        }
        return -1;
    }
}

指标说明

配置生效后,Prometheus端点会新增两类指标:

  • health_indicator_status:带indicator标签的分维度健康指标
    • indicator="db"对应数据库可用性状态
    • indicator="ldap"对应LDAP可用性状态
    • 其他自定义或内置健康检查项都会自动生成对应标签的指标
  • health_global_status:应用全局聚合健康状态,对应/actuator/health返回的顶层status值

内容的提问来源于stack exchange,提问作者Patrik Kukić

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.03 13:45:04