【问题标题】:Spring Boot 2 Actuator /health and /info returns HTTP 404Spring Boot 2 执行器 /health 和 /info 返回 HTTP 404
【发布时间】:2020-09-11 13:05:31
【问题描述】:

我已将 pom.xml 中的依赖项添加到全新的 Spring Boot 应用程序中,如 Spring documentation 中所述

<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>

并将以下属性添加到 application.yml 以便仅公开 /health/info 端点

management:
  endpoints:
    web:
      exposure:
        include:
          - info
          - health
      jmx:
        exposure:
          exclude: "*"
  endpoint:
    info:
      enabled: true
    health:
      enabled: true

但无法通过 localhost:8080/healthlocalhost:8080/info 到达运行应用程序时提到的端点 - 有 HTTP 404 响应。

我错过了什么吗?提前感谢您的帮助!

【问题讨论】:

  • 如果您的应用程序在localhost:8080/(无上下文路径)上运行,它应该可以工作/很难说,缺少什么...配置匹配默认值/可以省略,除了@ 987654333@ 部分,这似乎也不好缩进(至少在帖子中)。
  • @RebaiAhmed 你说的很对!最后,我在spring.io/guides/gs/actuator-service 找到了它。如果您愿意 - 在单独的答案中指定它,以便我可以标记为解决方案,否则我将自己回答。谢谢!
  • 你可以让它成为你的答案:) @DmitryAdonin

标签: java spring spring-boot spring-boot-actuator


【解决方案1】:

有两种可能的配置可以改变 Actuator /health 和 /info 端点:

首先,spring boot mvc server,假设你有这样的:

server:
   port: 8080
   servlet:
      context-path: /test

二、Actuator管理服务器

management:
  server:
  #port: #don't change it, then the port for actuator will be the same as server.port above
  endpoints:
     web:
       base-path: /actuator  #the default value for sring boot 2.x is /actuator, even you don't specify it, don't forget to include this in the URL
       exposure:
          include: "*"
  endpoint:
     health:
       show-details: always

所以访问 Actuator 的正确 URL 应该是:

http://localhost:8080/test/actuator/info

http://localhost:8080/test/actuator/health

总的来说,访问 Actuator 端点的 URL 模式应该是:

http://localhost:{server.port}/{server.servlet.context-path}/{management.endpoints.web.base-path}/**

您可以将您的配置与此示例进行比较,以了解导致 404 错误的原因。

【讨论】:

  • @Qui Zhou 你说的很对!最后在spring.io/guides/gs/actuator-service中找到了,有点奇怪docs.spring.io/spring-boot/docs/current/reference/html/…中没有提到
  • 很高兴您解决了这个问题!我实际上是通过阅读文档Spring Boot Actuator: Production-ready Features 了解到的(它实际上提到了 /actuator 前缀),然后做了一些实践来验证这一点。
  • 还有一点需要注意的是,根据是否使用 spring boot 1.X 和 2.X,执行器端点的默认路径会有所不同。例如,在 1.X 中,默认执行器健康端点只是 [context-root]/health,而在 2.X 中,路径是 [context-root]/actuator/health
  • @sandboxbohemian 好点!这是因为在 Spring Boot 1.x 中,management.endpoints.web.base-path 的默认值为 /
猜你喜欢
  • 2018-06-18
  • 1970-01-01
  • 2020-06-30
  • 2020-10-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-08-24
  • 2019-02-04
相关资源
最近更新 更多