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

新手上路,请多包涵

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

management.endpoint.metrics.enabled=true

然而,当我打

localhost:8080/actuator/metrics

我得到404。

解决方案是什么?

原文由 Dachstein 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 297
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 之前,只有 当 HATEOAS 在类路径上并application.yml 中显式启用时才会出现这种情况

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

默认:

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

  • 除了 /shutdown 之外的所有端点都被启用(虽然只有 /health/info 被暴露)

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

  2. 不推荐使用以 endpoints.xyz 开头的旧属性,而支持以 management.xyz 开头的属性

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

原文由 senseiwu 发布,翻译遵循 CC BY-SA 4.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进