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

Prometheus配置prometheus.yml采集Spring Actuator指标目标无效求助

Prometheus 采集Spring Actuator指标故障排查方案

1. 优先校验prometheus.yml语法缩进问题

YAML格式对缩进敏感度极高,你提供的配置存在明显的层级缩进错误,会直接导致配置加载失败,无法生效。以下是修正缩进后的合法配置:

# my global config
global:
  scrape_interval: 15s # Set the scrape interval to every 15 seconds. Default is every 1 minute.
  evaluation_interval: 15s # Evaluate rules every 15 seconds. The default is every 1 minute.
  # scrape_timeout is set to the global default (10s).

# Alertmanager configuration
alerting:
  alertmanagers:
  - static_configs:
    - targets:
      # - alertmanager:9093

# Load rules once and periodically evaluate them according to the global 'evaluation_interval'.
rule_files:
  # - 'first_rules.yml'
  # - 'second_rules.yml'

# A scrape configuration containing exactly one endpoint to scrape:
# Here it's Prometheus itself.
scrape_configs:
  # The job name is added as a label `job=<job_name>` to any timeseries scraped from this config.
  - job_name: 'prometheus'
    # metrics_path defaults to '/metrics'
    # scheme defaults to 'http'.
    static_configs:
      - targets: ['localhost:9090']

  - job_name: 'api'
    metrics_path: /actuator/prometheus
    scrape_interval: 5s
    static_configs:
      - targets: ['localhost.tac.com:8080']

配置修改后可以用Prometheus自带的校验工具验证合法性,执行命令:
promtool check config prometheus.yml
校验通过后再重启Prometheus服务生效。

2. 域名解析验证

Prometheus进程和服务虽然部署在同一台远程桌面,但仍需确认进程可正常解析localhost.tac.com:

  • 在远程桌面打开命令行工具,执行ping localhost.tac.com,确认解析地址为127.0.0.1
  • 可临时将targets修改为['127.0.0.1:8080'],排除域名解析导致的连接失败问题

3. 身份认证排查

你提到访问localhost.tac.com需要完成身份认证,浏览器访问时你已手动登录携带了有效认证信息,但Prometheus采集请求默认不会携带认证信息,会直接返回401/403错误:

  • 优先在Spring Boot配置中给/actuator/**端点设置免认证规则,临时关闭认证测试采集是否正常
  • 如果必须保留认证,根据认证类型在api job中补充对应配置:
    若为Basic认证,新增配置:
    basic_auth:
      username: 你的账号
      password: 你的密码
    
    若为Cookie/Token认证,可通过headers参数携带认证信息。

4. 查看Prometheus Target错误日志

访问Prometheus控制台(默认地址http://localhost:9090),进入Status > Targets页面,找到api job对应的采集项,可直接看到具体错误原因:

  • 若为connection refused:检查服务是否正常启动、端口是否正确、服务监听地址是否为0.0.0.0(如果Prometheus为容器部署,需替换targets为宿主机局域网IP,不能使用localhost)
  • 若为404 Not Found:确认metrics_path配置正确,端点路径没有拼写错误
  • 若为401/403 Forbidden:回到步骤3处理认证问题

5. 其他注意事项

targets参数仅支持填写IP:端口格式,不要在targets中拼接指标路径,指标路径需单独通过metrics_path参数配置,你之前尝试将localhost.tac.com:8080/actuator/prometheus填入targets的写法本身是错误的,无法识别。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.09.27 03:45:02