【问题标题】:How to implement liveness/readines using springboot 2.3.0如何使用 spring boot 2.3.0 实现 liveness/readiness
【发布时间】:2020-11-25 12:30:37
【问题描述】:

我最近了解到 springboot 2.3.0 提供了 liveness/readiness。为了实现它们,我将 springboot 版本更新为 2.3.0 并在 pom.xml 中添加了依赖 spring-boot-starter-validation。我还更新了 helm chart 的 env 部分以包含:

  name: management.health.probes.enabled
  value: 'true'
  name: management.endpoint.health.group.readiness.include
  value: 'readinessState,db'

为了实现组件的活跃性和就绪性探测,我只需要这样做吗?如果是这样,有没有办法在本地进行测试?我的同事告诉我,如果我可以在 application.properties 中本地编写环境,我应该能够在本地测试它(运行邮递员并公开 api,例如 /actuator/health/livness 之类的)。

【问题讨论】:

    标签: java spring-boot


    【解决方案1】:

    在 pom 中添加执行器依赖

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
    

    HTTP 探针仅针对在 Kubernetes 上运行的应用程序进行配置。您可以在本地尝试使用 management.health.probes.enabled=true 配置属性手动启用探针

    您可以通过 curl 或使用邮递员点击以下端点来检查活跃度和准备情况

    // http://localhost:8080/actuator/health/liveness
    // HTTP/1.1 200 OK
    
    {
      "status": "UP",
      "components": {
        "livenessProbe": {
          "status": "UP"
        }
      }
    }
    
    // http://localhost:8080/actuator/health/readiness
    // HTTP/1.1 503 SERVICE UNAVAILABLE
    
    {
      "status": "OUT_OF_SERVICE",
      "components": {
        "readinessProbe": {
          "status": "OUT_OF_SERVICE"
        }
      }
    }
    

    您当然可以配置额外的健康指标作为探测器的一部分,检查外部系统的状态:数据库、Web API、共享缓存。

    management.endpoint.health.group.liveness.include=livenessProbe,customCheck
    

    【讨论】:

    • 太棒了。对于本地测试,我添加了 management.endpoint.health.group.readiness.include=readinessProbe,dbmanagement.health.probes.enabled=true 以及数据库 URL、用户名和密码。它就像一个魅力。谢谢!
    • 这行得通!我只是有一个问题。当我调用 localhost:8080/actuator/health 时,我希望它能给我带来活力和准备,包括 db。但我没有得到任何这些(只是状态)。我错过了什么吗?
    • 你看到的和这里的一样吗spring.io/blog/2020/03/25/…
    • 我只看到这些:{ "status": "DOWN", "groups": [ "liveness", "readiness" ] }。当我单独执行它们时,它们工作正常。
    猜你喜欢
    • 2018-02-11
    • 2019-01-04
    • 2020-12-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多