【问题标题】:Configuring Prometheus for Spring Boot health check monitoring为 Spring Boot 健康检查监控配置 Prometheus
【发布时间】:2026-01-25 20:05:01
【问题描述】:

我们很少有实现健康检查的 Spring Boot 应用程序。根据@Thiru 的suggestion,这些检查的响应已更改为 JSON 格式。我现在得到以下回复:

Prometheus 服务器正在 Ubuntu 实例上运行。必须监控的 Spring Boot 服务正在 Windows Server 2016 上运行。在看到 this 的帖子后,我在 Windows Server 上安装了 blackbox-exporter(版本 0.12.0.windows-amd64)。

blackbox.yml 的客户端(IP:172.16.x.yz)的客户端(Windows 服务器)上完成了以下更改:

modules:
  http_2xx:
    prober: http
    http:
  http_post_2xx:
    prober: http
    timeout: 5s
    http:
      method: POST
      headers:
        Content-Type: application/json
      body: '{"status": "UP"}'
  ...
  ...

在Prometheus服务器上,prometheus.yml的内容如下:

...
...
scrape_configs:
  - job_name: 'prometheus'
    static_configs:
    - targets: ['localhost:9090']

  - job_name: 'blackbox'
    metrics_path: /probe
    params:
      #module: [http_2xx]  # Look for a HTTP 200 response.
      module: [http_post_2xx]  # Look for a HTTP 200 response.
    static_configs:
      - targets:
        - http://172.16.x.yz:6300/serviceA/actuator/health
        - http://172.16.x.yz:6340/serviceB/actuator/health
    relabel_configs:
      - source_labels: [__address__]
        target_label: __param_target
      - source_labels: [__param_target]
        target_label: instance
      - target_label: __address__
        replacement: 172.16.x.yz:9115  # The blackbox exporter's real hostname:port.

在客户端和服务器上进行上述更改后,当我重新启动 blackbox-exporterprometheus 时,我看到 Prometheus 总是State 显示为 UP,即使这两个服务出口商正在监控下降。似乎 Prometheus 显示的是blackbox-exporter 的状态,而不是服务的状态。有什么建议我可以解决这个问题吗?

【问题讨论】:

    标签: spring-boot prometheus


    【解决方案1】:

    我建议使用某种有助于将 spring 健康导出为 prometheus 导出器的工具。基本上,导出器有助于使用 jsonpath 将 json 数据从 http url 转换为 prometheus 指标:

    【讨论】:

    • 感谢您的回复。请检查我帖子中的更新部分
    • 你是否在 spring boot 中启用了执行器?
    • 它有 JSON 格式的健康端点。上面提到的两种解决方案都只适用于 JSON 输出
    • 两种情况下都会生成警报。要制作 prometheus-spring-boot-exporter / prometheus-jsonpath-exporter,您必须在应用程序中使用执行器。请按照 (this)[spring.io/guides/gs/actuator-service/] 链接中的步骤操作。这将为您提供/health 端点,prometheus-spring-boot-exporter 会从中删除指标。
    • 感谢您的意见。会尝试修复它
    【解决方案2】:

    在我看来,prometheus 目标实际上表明抓取目标处于向上或向下状态。这与健康检查端点状态无关。您只能在 probe_success 指标中检查 healthcheck 端点状态(1 为 up,0 为 down)。您可以查看以下屏幕截图。 对不起我的英语不好。

    【讨论】: