Skip to main content
 首页 » 编程设计

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

2025年02月15日21xing901022

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

management.endpoint.metrics.enabled=true 

然而,当我打
localhost:8080/actuator/metrics  

我得到 404。

解决办法是什么?

请您参考如下方法:

我想用更多信息来增强 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 中引入的更改
  • 端点如 /health , /metrics等是不再在默认根上下文中可用。从现在开始,它们可以在http://{host}:{port}/actuator .
    此外,您的应用程序的所有其他端点是否以其他上下文开头也没有关系,例如 /hello -- 执行器可在 /actuator 获得而不是在 /hello/actuator .
  • 来自 /actuator 的回复端点默认为 HATEOAS启用。在 Spring Boot 2 之前,只有这种情况 if HATEOAS is on the classpath and explicitly enabledapplication.yml
  • 要通过 HTTP 使执行器端点可用,它需要同时为 启用和暴露 .

    默认:
  • 只有 /health/info无论您的应用程序中是否存在和配置 Spring Security,端点都是公开的。
  • 所有端点,但 /shutdown已启用(尽管只有 /health/info 暴露)
  • 如果您想公开所有端点(并不总是一个好主意),您可以通过添加 management.endpoints.web.exposure.include=* 来实现。至 application.properties .如果您使用 yml-configurations,请不要忘记引用通配符。
  • endpoints.xyz 开头的旧属性不推荐使用以 management.xyz 开头的属性

  • 如需完整文档,请参阅 official doc还有 migration guide