【问题标题】:Spring Boot 2 - Actuator Metrics Endpoint not workingSpring Boot 2 - 执行器指标端点不起作用
【发布时间】:2018-07-08 06:50:20
【问题描述】:

在我设置的 Spring Boot App (2.0.0.M7) application.properties 中

management.endpoint.metrics.enabled=true

但是,当我击中时

localhost:8080/actuator/metrics 

我得到 404。

解决办法是什么?

【问题讨论】:

    标签: spring-boot spring-boot-actuator


    【解决方案1】:

    我想用更多信息来增强 OP 的答案,因为我在最终偶然发现这个解决方案之前有点挣扎,而且似乎对 Spring Boot 2 对执行器行为的更改有很多困惑

    没有改变的地方

    您需要包含对 spring-boot-starter-actuator 的依赖项

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

    如果你想通过HTTP访问执行器端点,你还需要添加一个依赖到spring-boot-starter-web

    所以你的 pom 依赖项如下所示

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

    Spring Boot 2 中引入的更改

    1. /health/metrics 等端点不再在默认根上下文中可用。从现在开始,它们可以在 http://{host}:{port}/actuator。 此外,您的应用程序的所有其他端点是否以其他一些上下文(例如/hello)开头并不重要——执行器在/actuator 上可用,而不在/hello/actuator 上可用。

    2. 来自/actuator 端点的响应默认启用HATEOAS。在 Spring Boot 2 之前,这种情况只有 if HATEOAS is on the classpath and explicitly enabled in application.yml

    3. 要通过 HTTP 使执行器端点可用,它需要同时启用和公开

      默认情况下:

      • 只有 /health/info 端点被公开,无论您的应用程序中是否存在和配置 Spring Security。

      • 除了/shutdown 之外的所有端点都已启用(尽管只有/health/info 被公开)

    4. 如果您想公开所有端点(并不总是一个好主意),您可以通过将management.endpoints.web.exposure.include=* 添加到application.properties 来实现。如果您使用 yml-configurations,请不要忘记引用通配符。

    5. 不推荐使用以endpoints.xyz 开头的旧属性,取而代之的是以management.xyz 开头的属性

    有关完整文档,请参阅 official docmigration guide

    【讨论】:

    • 从spring-boot2开始,现在是management.endpoints.web.exposure.include=*
    • yaml配置需要引用通配符,否则解析失败:management.endpoints.web.exposure.include: '*'
    • @Timi 我同意,答案已修改
    • 如果默认情况下已删除或禁用了这么多,我想知道执行器的用途是什么。我的项目中有执行器,唯一有用的是健康状况,甚至这是值得怀疑的。有什么东西在运行或没有运行,如果停止,那么心跳就会消失。 info url 创建一个空的 json。那么为什么要使用它呢?
    • 没有什么被删除。出于某种充分的原因,默认情况下会禁用敏感端点
    【解决方案2】:

    将以下行添加到您的 application.properties 文件中:

    management.endpoints.web.exposure.include=metrics

    就是这样。

    【讨论】:

    • 另外management.endpoints.web.exposure.include=* 为我工作
    【解决方案3】:

    对我有用的是以下(以 YAML 格式)与 Spring Boot 2 版本一起使用:

    management:
      endpoints:
        web:
          exposure:
            include: info, health, metrics
      metrics:
        export:
          atlas:
            enabled: false
    

    也可以找到具体的文档here

    【讨论】:

    • 当我使用它时,/metrics 端点工作,但只显示键,而不是值。怎么了???
    • 是的,这就是它的工作原理,然后您选择要显示的指标,而不是将所有指标显示在凌乱的单个页面中,这对于 prometheus 或 atlas 等其他应用程序读取指标也很有用
    【解决方案4】:

    您需要在application.properties 文件中添加以下道具。在添加以下道具之前,我遇到了同样的问题。

    management.endpoints.beans.enabled=false
    management.endpoints.web.exposure.include=*
    

    【讨论】:

      【解决方案5】:

      “*”在YAML中有特殊含义,所以如果要包含(或排除)所有端点,请务必加上引号,如下例所示:

      management:
        endpoints:
          web:
            exposure:
              include: "*"
      

      【讨论】:

        【解决方案6】:

        从 Spring Boot 1.5.15 升级到 2.1.4 时遇到同样的问题

        需要修改我pom.xml中Spring Boot执行器的原始依赖来自:

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

        到:

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

        注意artifactId 中添加了单词starter

        【讨论】:

        • 嗨!感谢分享! enabledexposed 有什么区别?谢谢!
        • 这很让人恼火,因为任何一个看起来都很好——但第一个默默地失败了。
        【解决方案7】:

        根据micrometer docs .Spring Boot 2.0.x 通过 Spring Boot Actuator 支持开箱即用的千分尺。
        默认情况下禁用端点 metric,这与 Spring Boot 2 的试金石测试一致,即任何可能暴露应用程序敏感数据的端点都应默认禁用。可以通过设置启用:

        management.endpoints.web.exposure.include:指标

        导航到/actuator/metrics 会显示可用计量器名称的列表。

        要访问它们,请使用以下内容: http://localhost:8080/actuator/metrics/jvm.memory.used

        【讨论】:

        • 只有这个对我有用 application.properties 通过指定 management.endpoints.web.exposure.include=metrics
        【解决方案8】:

        好的,我找到了解决方案。我在 application.properties 中添加了另一行

        management.endpoints.web.expose=*
        

        但是,保护执行器的 endoints 很重要

        在这里阅读: https://docs.spring.io/spring-boot/docs/current/reference/html/production-ready-monitoring.html

        【讨论】:

          【解决方案9】:

          以下配置对我有用

          server.servlet.context-path=/travel management.endpoints.web.exposure.include=*

          然后你需要添加上下文路径: http://localhost:8080/travel/actuator/metrics/

          【讨论】:

            【解决方案10】:
            management:
              endpoints:
                web:
                  base-path: "/"
                  exposure:
                    include: '*'
            

            它应该像那样工作。 * 表示暴露所有端点

            【讨论】:

              【解决方案11】:

              正如@senseiwu 提到的,与以前的版本不同,spring boot 2 中的 Actuator 禁用了大多数端点。 我们是否要启用所有这些,我们可以设置

              management.endpoints.web.exposure.include=* 
              

              或者,我们可以列出应该启用的端点。

              您可以轻松使用 hal-browser 这是一个有用的 UI,通过添加以下依赖项映射到“/”路径:

              <dependency>
                          <groupId>org.springframework.boot</groupId>
                          <artifactId>spring-boot-starter-actuator</artifactId>
              </dependency>
              <dependency>
                          <groupId>org.springframework.data</groupId>
                          <artifactId>spring-data-rest-hal-browser</artifactId>
              </dependency>
              

              在 hal 浏览器中,您需要输入 /actuator 才能查看所有端点。 它已经在 Spring Boot 2.3.0.M2 中进行了测试,并且运行良好。 您可以通过以下链接了解更多信息:

              Spring REST and HAL Browser

              Spring Boot Actuator

              【讨论】:

                【解决方案12】:

                在 application.properties 中添加以下属性为我解决了问题:

                management.health.defaults.enabled=false
                

                【讨论】:

                  【解决方案13】:

                  将千分尺的完整配置放在这里。以下一个对我来说工作正常。我将它用于 ELK 堆栈

                  management:
                    metrics:
                      enable:
                        jvm: true
                        all: true
                      export:
                        elastic:
                          enables: true
                          step: 10s
                          index: micrometer-${spring.application.name}
                          host: http://localhost:9200
                        simple:
                          enabled: true
                      distribution:
                        percentiles-histogram:
                          http:
                            server:
                              requests: true
                        sla:
                          http:
                            server:
                              requests: 100ms, 400ms, 500ms, 2000ms
                        percentiles:
                          http:
                            server:
                              requests: 0.5, 0.9, 0.95, 0.99
                    endpoint:
                      metrics:
                        enabled: true
                    endpoints:
                      web:
                        exposure:
                          include: '*'
                  

                  【讨论】:

                    【解决方案14】:

                    application.properties 中设置management.endpoints.web.exposure.include=metrics 以通过HTTP 公开/actuator/metrics


                    参考:https://docs.spring.io/spring-boot/docs/current/reference/html/actuator.html

                    参考:https://docs.spring.io/spring-boot/docs/current/reference/html/actuator.html#actuator.endpoints

                    Actuator 端点让您可以监控您的应用程序并与之交互。 Spring Boot 包含许多内置端点,并允许您添加自己的端点。例如,健康端点提供基本的应用健康信息。

                    每个单独的端点都可以是enableddisabledexposed(可远程访问)HTTPJMX。当端点被启用和公开时,它被认为是可用的。 内置端点只有在它们可用时才会被自动配置。大多数应用程序选择通过 HTTP 公开,其中端点的 ID 以及 /actuator 的前缀被映射到 URL。例如,默认情况下,健康端点映射到 /actuator/health。

                    参考:https://docs.spring.io/spring-boot/docs/current/reference/html/actuator.html#actuator.endpoints.enabling

                    默认情况下,除了关闭之外的所有端点都启用。

                    参考:https://docs.spring.io/spring-boot/docs/current/reference/html/actuator.html#actuator.endpoints.exposing

                    【讨论】:

                      猜你喜欢
                      • 1970-01-01
                      • 2018-11-29
                      • 2018-11-16
                      • 1970-01-01
                      • 2018-12-06
                      • 1970-01-01
                      • 2020-11-15
                      • 1970-01-01
                      • 2015-07-04
                      相关资源
                      最近更新 更多