Spring 未找到引导执行器健康检查

Spring Boot actuator healthcheck not found

Spring 在这里启动 2.3.8。我的 application.yml:

server:
  port: 9200
  error:
    whitelabel:
      enabled: false
  ssl:
    enabled: false
spring:
  cache:
    type: none
  datasource:
    driver-class-name: com.mysql.jdbc.Driver
    url: jdbc:mysql://${db.hostAndPort}/${db.name}?useSSL=false&nullNamePatternMatchesAll=true&serverTimezone=UTC
    username: ${db.username}
    password: ${db.password}
    testWhileIdle: true
    validationQuery: SELECT 1
  jpa:
    show-sql: false
    database-platform: org.hibernate.spatial.dialect.mysql.MySQL5InnoDBSpatialDialect
    hibernate:
      ddl-auto: none
      naming:
        physical-strategy: org.springframework.boot.orm.jpa.hibernate.SpringPhysicalNamingStrategy
        implicit-strategy: org.springframework.boot.orm.jpa.hibernate.SpringImplicitNamingStrategy
    properties:
      hibernate.dialect: org.hibernate.dialect.MySQL5Dialect
      hibernate.cache.use_second_level_cache: false
      hibernate.cache.use_query_cache: false
      hibernate.generate_statistics: false
      hibernate.hbm2ddl.auto: validate
management:
  port: 9200
  endpoint:
    health:
      show-details: always
  endpoints:
    web:
      exposure:
        include: prometheus

然后我写了我自己的习惯 HealthIndicator impl:

@Component
@Slf4j
public class MyAppHealthIndicator implements HealthIndicator {

    @Override
    public Health health() {
        Health.Builder health = Health.up();
        return health.build();
    }

}

当我 运行 我的应用程序时,它启动得很好。但是当我去 运行 a curl 对健康检查端点时:

$ curl -k -i -H "Content-Type: application/json" -X GET http://localhost:9200/actuator/health
HTTP/1.1 404 
Vary: Origin
Vary: Access-Control-Request-Method
Vary: Access-Control-Request-Headers
X-Content-Type-Options: nosniff
X-XSS-Protection: 1; mode=block
Cache-Control: no-cache, no-store, max-age=0, must-revalidate
Pragma: no-cache
Expires: 0
X-Frame-Options: DENY
Content-Type: application/json
Transfer-Encoding: chunked
Date: Sun, 31 Jan 2021 01:30:39 GMT

{"timestamp":"2021-01-31T01:30:39.206+00:00","status":404,"error":"Not Found","message":"","path":"/actuator/health"}

我是不是忘记了一步?我需要添加什么吗?我的 YAML 文件有什么问题吗?为什么它找不到我的健康检查 url?

您不需要自定义 MyAppHealthIndicator class,因为 Spring Boot 带有默认健康指示器,默认情况下通过 Endoint /actuator/health/

激活

您的问题是 application.yml 配置,您在其中定义了您只想公开 prometheus 端点。但是您还应该启用健康端点:

...
endpoints:
  web:
    exposure:
      include: health, prometheus
...

现在您可以通过以下方式访问它:http://localhost:9200/actuator/health/

更多细节见官方文档:https://docs.spring.io/spring-boot/docs/current/reference/html/production-ready-features.html#production-ready-endpoints