【问题标题】:Spring actuator's liveness and readiness are returning 404Spring actuator 的 liveness 和 readiness 正在返回 404
【发布时间】:2022-01-28 21:27:21
【问题描述】:

这是我在 application.yaml 中的配置:

management:
  endpoint:
    health:
      show-details: "ALWAYS"
      probes:
        enabled: true
  endpoints:
    enabled-by-default: true
    web:
      exposure:
        include: metrics, health, caches, restart

根据文档,这应该足以为 spring 应用程序启用 liveness 和 readiness 探测。但是端点(/actuator/health/liveness/actuator/health/readiness)仍然返回 404。我在配置中尝试了很多组合,但没有任何效果。你能告诉我该怎么做吗?

【问题讨论】:

  • 嘿,你解决了吗?需要帮助吗?

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


【解决方案1】:

如果您使用 spring 2.3.2 或更高版本,请添加以下属性:

management.endpoint.health.probes.enabled=true
management.health.livenessState.enabled=true
management.health.readinessState.enabled=true

【讨论】:

    【解决方案2】:

    我对这个问题进行了更深入的研究,因为我发现它是spring-boot-actuator 的一个有趣功能。 从我的研究中我发现livenessreadiness 的这个功能已经在spring-boot:2.3.0 中引入,所以如果你使用的是旧版本,这可能是你在执行时没有收到预期结果的原因GET /actuator/health/readiness.

    如果您将 spring-boot 版本升级到 >= 2.3.0,您可以通过添加以下内容来启用 liveness 和 readiness 探测:

    management:
      health:
        probes:
          enabled: true
    

    到您的 application.yaml 文件。 这样做后你应该能够

    GET /actuator/health

    {
        "status": "UP",
        "groups": [
            "liveness",
            "readiness"
        ]
    }
    

    但是对于 spring-boot 版本 >= 2.3.2,建议通过在 application.yaml 中使用以下内容来启用探针

    management:
      endpoint:
        health:
          probes:
            enabled: true 
    

    这样做的原因是一个错误,您可以阅读更多关于here的信息

    额外提示:如果您是 spring-boot 版本 >= 2.3.0,则您已相应地配置了 application.yaml 文件,但仍会收到 404 GET /actuator/health/liveness 您的 application.yaml 文件没有被 Spring Context 拾取的可能性很小。您可以通过更改应用程序的端口来检查是否是这种情况

    server:
      port: 8081
    

    如果您的应用程序没有在不同的端口上启动,可以肯定地说您的任何配置都没有发生。 我的 IDE 遇到过一两次这个问题。

    【讨论】: