【问题标题】:Prometheus UI, Grafana settings for spring boot applicationPrometheus UI,Spring Boot 应用程序的 Grafana 设置
【发布时间】:2026-02-14 09:05:02
【问题描述】:

我使用的是 spring boot 2.0 并在 POM 中添加了以下依赖项

     <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-actuator</artifactId>
    </dependency>

    <dependency>
        <groupId>io.micrometer</groupId>
        <artifactId>micrometer-registry-prometheus</artifactId>
    </dependency>

我的 application.yml 看起来像这样

management.endpoints.web.base-path = /manage management.endpoints.web.exposure.include = "*" endpoints.prometheus.enabled = true

当我在

访问 Prometheus 时

本地主机/管理/普罗米修斯

我可以看到所有指标。

接下来,我的目标是在 Prometheus UI 中查看上述指标。为此,我在我的 POM 中添加了以下依赖项

   <dependency>  
        <groupId>io.prometheus</groupId>
        <artifactId>simpleclient_spring_boot</artifactId>
        <version>${prometheus.version}</version>
    </dependency>

    <dependency>
        <groupId>io.prometheus</groupId>
        <artifactId>simpleclient_hotspot</artifactId>
        <version>${prometheus.version}</version>
    </dependency>

    <dependency>
        <groupId>io.prometheus</groupId>
        <artifactId>simpleclient_servlet</artifactId>
        <version>${prometheus.version}</version>
    </dependency>

在 Prometheus UI 中查看指标的下一步是什么,最终目标是将 Prometheus 集成到 Grafana。

PS:我在 google 上进行了搜索,并尝试添加 prometheus.yml 并添加诸如 @EnablePrometheusEndpoint 之类的注释,因为所有文章都是旧的,所以没有任何效果。

编辑:如果 spring boot jar 托管在不同的主机(Azure/AWS)并且 prometheus 服务器在不同的主机中,如何配置 prometheus.yml(metrics_path,targets)。

【问题讨论】:

    标签: spring spring-boot prometheus spring-boot-actuator micrometer


    【解决方案1】:

    如果您使用的是 Spring Boot 2 和 micrometer,则不需要添加额外的依赖项,它们会在您添加 micrometer-registry-prometheus 时导入。如果您能够在 localhost/manage/prometheus 上看到指标,那么您在 spring-boot 端的配置就可以了。无需再进行任何配置。

    要在 Prometheus 中查看您需要的指标:

    1. 安装普罗米修斯服务器https://prometheus.io/docs/prometheus/latest/installation/
    2. 配置 Prometheus 以从您的服务器抓取(拉取)指标。为此,您需要通过添加新作业来修改 prometheus.yml 文件(更改 yml 文件后不要忘记重新启动 Prometheus)
    scrape_configs: - 工作名称:'mySpringBoot' metrics_path: '/manage/prometheus' 静态配置: - 目标:['springBootHost:springBootPort']
    1. 配置完成后,进入 Prometheus UI,检查目标是 UP - http://localhost:9090/targets(假设 Prometheus 在 localhost 上运行)
    2. 如果您没有看到您的目标或者您将其视为DOWN,则说明存在配置或网络问题。

    以下步骤很简单,其他地方有很多文档:

    1. 下一步是安装 Grafana
    2. 现在将 Prometheus 配置为 Grafana 中的数据源
    3. 开始绘制您的指标。

    【讨论】:

    • 感谢 Yarix 的清晰解释。现在我有另一个问题。我的微服务托管在 Azure 中。所以我配置了metrics_path:“mywebapp.azure.com/prometheus”,scheme:“https”,targets:['localhost:9090']。当我访问localhost:9090/targets 时,我的工作失败了,因为我看到的最终 URL 是 localhost:9090https://mywebapp.azure.com/prometheus,这是错误的,如何解决这个问题?微服务和普罗米修斯需要在同一主机上吗?
    • metrics_path 是错误的,你应该只写 '/manage/prometheus' 或 '/prometheus' 取决于你如何配置 spring-boot。普罗米修斯服务器在哪里运行?也在 Azure 中?在应用程序的同一台机器上?目标应该是 ['yourSpringBootServer:port']
    • Prometheus 服务器正在我的本地运行。 /manage/prometheus 的 metrics_path 不起作用,因为主机不是我的本地主机。
    • 它不起作用,因为您的目标错误。目标应该是 mywebapp.azure.com。另外,请确保您可以从本地计算机访问 mywebapp.azure.com/manage/prometheus。
    • 它没有工作它说 mywebapp.azure.com 不是有效的主机名
    【解决方案2】:

    Spring boot api 应用在 windows 上配置 prometheus 和 grafana。

    1. 创建spring boot应用并在pom.xml中添加依赖-
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-actuator</artifactId>
    </dependency>
    <dependency>
     <groupId>io.micrometer</groupId>
     <artifactId>micrometer-core</artifactId>
    </dependency>
    <dependency>
      <groupId>io.micrometer</groupId>
      <artifactId>micrometer-registry-prometheus</artifactId>
    </dependency>
    

    如果它正在运行,请转到您的浏览器并输入“http://localhost:8080/actuator/prometheus”,您将获得所有指标。

    1. 下载并安装 Prometheus Server https://prometheus.io/download/ 并解压 zip 并运行 prometheus exe。

    2. 为此,您需要通过添加新作业来修改 prometheus.yml 文件(更改 yml 文件后不要忘记重新启动 Prometheus)

    注意:使用 prometheus 配置 spring 应用程序。

    scrape_configs: - job_name: 'SpringBootApplicationName 指标路径:'执行器-普罗米修斯' 静态配置: - 目标:['IPADDRESS:springBootApplicationPort]
    1. 配置完成后,进入 Prometheus UI,检查目标是 UP - http://localhost:9090/targets(假设 Prometheus 在 localhost 上运行)

    2. 转到“http://localhost:8080/actuator/prometheus”并选择一个指标并粘贴 - http://localhost:9090/graph 并单击执行。例如,您可以选择一个 CPU 和 HTTP 指标,例如 - ‘http_server_request_seconds_max’

    以下步骤很简单,其他地方有很多文档:

    1. 下一步是从 -https://grafana.com/grafana/download?platform=windows 安装 Grafana 并解压缩 zip 并运行 grafana-server.exe 并检查您的浏览器“http://localhost:3000
    2. 现在以默认用户名“admin”和密码“admin”登录,然后转到设置选择 deshboard 并创建数据源并选择 prometheus 并输入 prometheus 的 url,例如 -''http://ipaddress:pord of prometheus。
    3. 然后转到图形并单击编辑并选择数据源并键入您的指标查询,例如“http_server_request_seconds_max” 9.开始绘制您的指标。

    【讨论】: