简体   繁体   English

Spring Boot HealthIndicator 示例

[英]Spring Boot HealthIndicator by Example

I am looking to build a robust and detailed health check endpoint ( /health ) on my Spring Boot REST service.我希望在我的 Spring Boot REST 服务上构建一个强大而详细的健康检查端点 ( /health )。 I just read this excellent Baeldung article on the subject but still have a few concerns.我刚刚阅读了有关该主题的这篇出色的 Baeldung 文章,但仍有一些担忧。

Ideally my /health endpoint would be able to take into account the individual health of ALL my subsystems (10+ of them) including the health of the host (CPU, disk utilization, available memory, etc.).理想情况下,我的/health端点将能够考虑我所有子系统(其中 10 个以上)的个人健康状况,包括主机的健康状况(CPU、磁盘利用率、可用内存等)。

I can't tell whether Spring Boot expects you to build one-and-only one HealthIndicator impl.我不知道 Spring Boot 是否希望您构建一个且唯一的HealthIndicator impl。 Or if the idea is to build one HealthIndicator impl for each major subsystem (each of which can be independently " up " or " down " respectively.或者,如果想法是为每个主要子系统构建一个HealthIndicator impl(每个子系统可以分别独立地“向上”或“向下”。

Also, in that Baeldung example, what is the difference between the top-level status and the myHealthCheck.status and where do each of them come from (in the code)?另外,在那个 Baeldung 示例中,顶级statusmyHealthCheck.status之间有什么区别,它们每个来自哪里(在代码中)?

{
    "status" : "DOWN",
    "myHealthCheck" : {
        "status" : "DOWN",
        "Error Code" : 1,
        "Description" : "You custom MyHealthCheck endpoint is down"
     },
     "diskSpace" : {
         "status" : "UP",
         "free" : 209047318528,
         "threshold" : 10485760
     }
}

And where does diskSpace come from?! diskSpace是从哪里来的?!

You can implement several HealthIndicator s and they will all contribute to the final overall value of UP or DOWN .您可以实现多个HealthIndicator ,它们都会对UPDOWN的最终总价值做出贡献。 The final value is an aggregate of all other values (determined by a StatusAggregator ).最终值是所有其他值的聚合(由StatusAggregator确定)。

Example 1 (Healthy)示例 1(健康)

@Component
public class ExampleOneHealthIndicator implements HealthIndicator {

    @Override
    public Health health() {
      // do something to check health

      // inspect the return 

      // healthy
      return Health.up().build();
    }
}

Example 2 (Unhealthy)示例 2(不健康)

@Component
public class ExampleTwoHealthIndicator implements HealthIndicator {

    @Override
    public Health health() {
      // do something to check health

      // inspect the return 

      // unhealthy
      return Health.down().build();
    }
}

Example 3 (Unhealthy with details)示例 3(不健康的细节)

@Component
public class ExampleThreeHealthIndicator implements HealthIndicator {

    @Override
    public Health health() {
      // do something to check health

      // inspect the return 

      // unhealthy with details
      return Health.down()
                .withDetail("reason", "details about why it failed")
                .build();
    }
}

With the above three examples and the management.endpoint.health.show-details set to always (in the application.properties ), you'd get the following result when calling /actuator/health .通过以上三个示例,并且management.endpoint.health.show-details设置为always (在application.properties ),您在调用/actuator/health时会得到以下结果。

{
    "status": "DOWN",
    "components": {
        "diskSpace": {
            "status": "UP",
            "details": {
                "total": 1000240963584,
                "free": 880857858048,
                "threshold": 10485760
            }
        },
        "exampleOne": {
            "status": "UP"
        },
        "exampleThree": {
            "status": "DOWN",
            "details": {
                "reason": "details about why it failed"
            }
        },
        "exampleTwo": {
            "status": "DOWN"
        },
        "ping": {
            "status": "UP"
        }
    }
}

The names in the response are the class name (camel-cased) before HealthIndicator (eg ExampleOneHealthIndicator --> exampleOne )在响应中的名称是类名(骆驼套管)前HealthIndicator (如ExampleOneHealthIndicator - > exampleOne

The other names (such as diskSpace or ping ) come from the built in health checks and the class names are named just as you'd expect ( DiskSpaceHealthIndicator & PingHealthIndicator )其他名称(例如diskSpaceping )来自内置的健康检查,类名称的命名与您期望的一样( DiskSpaceHealthIndicatorPingHealthIndicator

Note that the top-level status is DOWN .请注意,顶级statusDOWN This is determined by the StatusAggregator .这是由StatusAggregator决定的。 The default one is https://docs.spring.io/spring-boot/docs/current/api/org/springframework/boot/actuate/health/SimpleStatusAggregator.html默认是https://docs.spring.io/spring-boot/docs/current/api/org/springframework/boot/actuate/health/SimpleStatusAggregator.html

Example code示例代码

https://github.com/dustinschultz/spring-boot-actuator-custom-health-checks https://github.com/dustinschultz/spring-boot-actuator-custom-health-checks

1) You can have as much health indicators as you like. 1) 您可以拥有尽可能多的健康指标。 Just create beans which will extend org.springframework.boot.actuate.health.HealthIndicator and they will be automatically picked up by actuator's healthcheck endpoint.只需创建将扩展org.springframework.boot.actuate.health.HealthIndicator bean,它们将被执行器的健康检查端点自动拾取。 The name of your bean will be the name of certain health indicator.您的 bean 的名称将是某些健康指标的名称。 In your example myHealthCheck and diskSpace are beans in spring context that were called when you hit /health .在您的示例中, myHealthCheckdiskSpace是 spring 上下文中的 bean,当您点击/health时会调用它们。 diskSpace is one of predefined health indicators in spring boot comming from org.springframework.boot.actuate.health.DiskSpaceHealthIndicator . diskSpace 是 spring boot 中预定义的健康指标之一,来自org.springframework.boot.actuate.health.DiskSpaceHealthIndicator

2) top level status is accumulative state of all your health indicators. 2) 顶级状态是您所有健康指标的累积状态。 You can configure the way how it works but by default it'll show the lowest state (you have one of health indicators in DOWN state so accumulative state shown as DOWN)您可以配置它的工作方式,但默认情况下它将显示最低状态(您有一个处于 DOWN 状态的健康指标,因此累积状态显示为 DOWN)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM