簡體   English   中英

Spring Boot HealthIndicator 示例

[英]Spring Boot HealthIndicator by Example

我希望在我的 Spring Boot REST 服務上構建一個強大而詳細的健康檢查端點 ( /health )。 我剛剛閱讀了有關該主題的這篇出色的 Baeldung 文章,但仍有一些擔憂。

理想情況下,我的/health端點將能夠考慮我所有子系統(其中 10 個以上)的個人健康狀況,包括主機的健康狀況(CPU、磁盤利用率、可用內存等)。

我不知道 Spring Boot 是否希望您構建一個且唯一的HealthIndicator impl。 或者,如果想法是為每個主要子系統構建一個HealthIndicator impl(每個子系統可以分別獨立地“向上”或“向下”。

另外,在那個 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
     }
}

diskSpace是從哪里來的?!

您可以實現多個HealthIndicator ,它們都會對UPDOWN的最終總價值做出貢獻。 最終值是所有其他值的聚合(由StatusAggregator確定)。

示例 1(健康)

@Component
public class ExampleOneHealthIndicator implements HealthIndicator {

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

      // inspect the return 

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

示例 2(不健康)

@Component
public class ExampleTwoHealthIndicator implements HealthIndicator {

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

      // inspect the return 

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

示例 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();
    }
}

通過以上三個示例,並且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"
        }
    }
}

在響應中的名稱是類名(駱駝套管)前HealthIndicator (如ExampleOneHealthIndicator - > exampleOne

其他名稱(例如diskSpaceping )來自內置的健康檢查,類名稱的命名與您期望的一樣( DiskSpaceHealthIndicatorPingHealthIndicator

請注意,頂級statusDOWN 這是由StatusAggregator決定的。 默認是https://docs.spring.io/spring-boot/docs/current/api/org/springframework/boot/actuate/health/SimpleStatusAggregator.html

示例代碼

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

1) 您可以擁有盡可能多的健康指標。 只需創建將擴展org.springframework.boot.actuate.health.HealthIndicator bean,它們將被執行器的健康檢查端點自動拾取。 您的 bean 的名稱將是某些健康指標的名稱。 在您的示例中, myHealthCheckdiskSpace是 spring 上下文中的 bean,當您點擊/health時會調用它們。 diskSpace 是 spring boot 中預定義的健康指標之一,來自org.springframework.boot.actuate.health.DiskSpaceHealthIndicator

2) 頂級狀態是您所有健康指標的累積狀態。 您可以配置它的工作方式,但默認情況下它將顯示最低狀態(您有一個處於 DOWN 狀態的健康指標,因此累積狀態顯示為 DOWN)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM