Spring Boot 2 - 执行器指标端点不起作用

Dac*_*ein 21 spring-boot spring-boot-actuator

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

management.endpoint.metrics.enabled=true
Run Code Online (Sandbox Code Playgroud)

但是,当我击中时

localhost:8080/actuator/metrics 
Run Code Online (Sandbox Code Playgroud)

我得到404.

什么是解决方案?

sen*_*iwu 53

我想用更多的信息来增强OP的答案,因为我在最终绊倒这个解决方案之前有点挣扎,并且似乎有很多关于使用Spring Boot 2改变执行器行为的困惑

没有改变的是什么

您需要在spring-boot-starter-actuator中包含依赖

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-actuator</artifactId>
    </dependency>
</dependencies>
Run Code Online (Sandbox Code Playgroud)

如果要通过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>
Run Code Online (Sandbox Code Playgroud)

Spring Boot 2中引入的更改

  1. 像端点/health,/metrics等等都不再提供默认的根上下文.它们从现在开始提供 http://{host}:{port}/actuator.此外,不要紧,你的应用程序的所有其他端点是否与其他一些方面开始如/hello-调节器可在/actuator与不在/hello/actuator.

  2. /actuator默认情况下,端点的响应是启用HATEOAS.在Spring Boot 2之前,只有在HATEOAS位于类路径上且明确启用时才会出现这种情况application.yml

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

    默认情况下:

    • 无论应用程序中是否存在和配置Spring Security,都只显示/health/info端点.

    • 但所有端点/shutdown启用(虽然只/health/info暴露)

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

  5. 旧的属性开头endpoints.xyz不推荐使用以.开头的属性management.xyz

有关完整文档,请参阅官方文档迁移指南

  • 如果是yaml配置,则需要引用通配符,否则将无法解析:`management.endpoints.web.exposure.include:'*'` (9认同)
  • 从spring-boot2开始,现在是`management.endpoints.web.exposure.include =*` (4认同)

小智 8

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

management.endpoints.web.exposure.include=metrics

就这样.

  • 另外 `management.endpoints.web.exposure.include=*` 对我有用 (2认同)

tbo*_*tbo 7

适用于我的是以下(以YAML格式)与Spring Boot 2发行版一起工作:

management:
  endpoints:
    web:
      exposure:
        include: info, health, metrics
  metrics:
    export:
      atlas:
        enabled: false
Run Code Online (Sandbox Code Playgroud)

也可以在这里找到特定的文档

  • 当我使用它时,/ metrics端点有效,但是仅显示键,而不显示值。怎么了??? (2认同)

Kri*_*ish 7

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

management.endpoints.beans.enabled=false
management.endpoints.web.exposure.include=*
Run Code Online (Sandbox Code Playgroud)


Dil*_*lan 7

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

management:
  endpoints:
    web:
      exposure:
        include: "*"
Run Code Online (Sandbox Code Playgroud)


dis*_*ley 6

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

需要修改我的 Spring Boot 执行器的原始依赖项pom.xml

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-actuator</artifactId>
</dependency>
Run Code Online (Sandbox Code Playgroud)

到:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
Run Code Online (Sandbox Code Playgroud)

注意starterartifactId.


Dac*_*ein 3

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

management.endpoints.web.expose=*
Run Code Online (Sandbox Code Playgroud)

然而,保护执行器端点很重要

阅读此处: https ://docs.spring.io/spring-boot/docs/current/reference/html/product-ready-monitoring.html