Spring Boot 2执行器/ health和/ info返回HTTP 404

问题描述

我已将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响应。

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

解决方法

有两种可能会更改执行器/ health和/ info端点的配置:

首先, spring boot mvc服务器,假设您拥有这样的服务器:

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

第二,执行器管理服务器

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

总体而言,访问执行器端点的URL模式应为:

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

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