如何在Spring Boot 2.3.0中实现活动性/可用性检查

huangapple go评论76阅读模式
英文:

How to implement liveness/readines using springboot 2.3.0

问题

我最近了解到Spring Boot 2.3.0提供了存活性(liveness)和就绪性(readiness)功能。为了实现它们,我将Spring Boot版本更新为2.3.0,并在pom中添加了spring-boot-starter-validation依赖。我还更新了Helm图表的环境部分,包括以下内容:

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

这些步骤是否足以实现组件的存活性和就绪性探针?如果是这样,是否有方法可以在本地测试?我的同事告诉我,如果我可以在application.properties中本地编写环境配置,那么我应该能够在本地进行测试(运行Postman并公开API,例如/actuator/health/liveness等)。

(Note: The code section remains unchanged as it is and is not translated.)

英文:

I recently learned that springboot 2.3.0 offers liveness/readiness. In order to implement them, I updated springboot version to 2.3.0 and added dependency spring-boot-starter-validation in pom. I updated helm chart's env section as well to contain:

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

Is this all I need to in order to implement liveness and readiness probes for the component? If so, is there way to test this locally? My co-worker told me if I can write environment locally in application.properties, I should be able to test it locally (running postman and expose api such as /actuator/health/livness or something).

答案1

得分: 1

在pom中添加执行器依赖项

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

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

您可以通过curl或使用postman命中以下端点来检查存活性和就绪性:

  • 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
英文:

Add actuator dependency in pom

    &lt;dependency&gt;
        &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
        &lt;artifactId&gt;spring-boot-starter-actuator&lt;/artifactId&gt;
    &lt;/dependency&gt;

HTTP Probes are only configured for applications running on Kubernetes. You can give it a try locally by manually enabling the probes with the management.health.probes.enabled=true configuration property

You can check the liveness and readiness by curl or using postman to hit below endpoints

// http://localhost:8080/actuator/health/liveness
// HTTP/1.1 200 OK

{
  &quot;status&quot;: &quot;UP&quot;,
  &quot;components&quot;: {
    &quot;livenessProbe&quot;: {
      &quot;status&quot;: &quot;UP&quot;
    }
  }
}

// http://localhost:8080/actuator/health/readiness
// HTTP/1.1 503 SERVICE UNAVAILABLE

{
  &quot;status&quot;: &quot;OUT_OF_SERVICE&quot;,
  &quot;components&quot;: {
    &quot;readinessProbe&quot;: {
      &quot;status&quot;: &quot;OUT_OF_SERVICE&quot;
    }
  }
}

You can of course configure additional Health Indicators to be part of the Probes, checking for the state of external systems: a database, a Web API, a shared cache.

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

huangapple
  • 本文由 发表于 2020年8月5日 18:35:55
  • 转载请务必保留本文链接:https://go.coder-hub.com/63263339.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定