Spring + Prometheus + Grafana:阅读 Prometheus 时出错:发布“http://localhost:9090/api/v1/query”:拨打 tcp 127.0.0.1:9090:连接:连接被拒绝

Ale*_*Pap 2 java spring docker spring-boot grafana

您好,我在 Spring Boot 中有一个应用程序,我正在公开 Prometheus 上的一些指标。我的下一个目标是在 Grafana 上提供这些指标,以获得一些漂亮的仪表板。我在 WSL Ubuntu 上使用 docker,并为 Prometheus 和 Grafana 输入以下命令:

docker run -d --name=prometheus -p 9090:9090 -v /mnt/d/Projects/Msc-Thesis-Project/prometheus.yml:/etc/prometheus/prometheus.yml prom/prometheus --config.file=/etc/prometheus/prometheus.yml
Run Code Online (Sandbox Code Playgroud)
docker run -d --name=grafana -p 3000:3000 grafana/grafana
Run Code Online (Sandbox Code Playgroud)

下面我在浏览器中为您提供了 Prometheus 仪表板,如您所见,一切都已启动并正在运行。我的问题是在 Grafana 配置中,我必须将 Prometheus 配置为数据源。

普罗米修斯仪表板

在 URL 字段中,我提供了 http://localhost:9090 但出现以下错误:

读取 Prometheus 时出错:发布“http://localhost:9090/api/v1/query”:拨打 tcp 127.0.0.1:9090:连接:连接被拒绝

我到处搜索,发现一些不适用于我的解决方法。具体来说,我使用了以下内容:http://host.docker.internal:9090、http://server-ip:9090,当然还有通过 ipconfig 命令 http://<ip_address>:9090 获得的系统 IP 地址。没有任何作用!

我没有使用 docker-compose,而只是使用 prometheus.yml 文件,如下所示。

global:
  scrape_interval:     15s
  evaluation_interval: 15s

scrape_configs:

  - job_name: 'prometheus'
    scrape_interval: 5s
    static_configs:
      - targets: ['localhost:9090']

  - job_name: 'Spring Boot Application input'
    metrics_path: '/actuator/prometheus'
    scrape_interval: 2s
    scheme: http
    static_configs:
      - targets: ['192.168.1.233:8080']
        labels:
          application: "MSc Project Thesis"
Run Code Online (Sandbox Code Playgroud)

你能给我一些建议吗?

Grafana 仪表板

小智 11

您可以使用 dockerspect 命令查找 Prometheus 容器的 IP 地址,然后用它替换 localhost 一词。

  • 哇,我没想到会这样。似乎它之前与 localhost 一起工作过,但现在你的回答对我有帮助。谢谢 (2认同)

小智 5

您只需在 Grafana 的 URL 字段中替换localhost为即可。prometheus

刚才在我的设备上测试了它,并使用localhost我收到了与您相同的错误。当我将 URL 替换为http://prometheus:9090并单击“保存”时,它确实按预期工作。