代码之家  ›  专栏  ›  技术社区  ›  smeeb

Spring Boot HealthIndicator示例

  •  3
  • smeeb  · 技术社区  · 8 年前

    我希望构建一个健壮且详细的健康检查端点( /health )在我的弹簧靴休息服务上。我只是在读 this excellent Baeldung article 但仍有一些担忧。

    理想情况下,我的 /健康状况 endpoint将能够考虑我所有子系统(其中10多个子系统)的个人健康状况,包括主机的健康状况(CPU、磁盘利用率、可用内存等)。

    我不知道Spring Boot是否希望您构建一个且仅构建一个 HealthIndicator 实施。 或 如果要建造一个 健康指示器 每个主要子系统的impl(每个子系统可以独立) 向上的 “或” 向下 “分别。

    另外,在Baeldung的例子中,顶级 status 以及 myHealthCheck.status 它们都来自哪里(代码中)?

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

    在哪里呢 diskSpace 来自

    2 回复  |  直到 8 年前
        1
  •  5
  •   dustin.schultz    6 年前

    您可以实现几个 HealthIndicator 它们都将为 向上 或 向下 . 最终值是所有其他值的总和(由 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"
            }
        }
    }
    

    响应中的名称是之前的类名(大小写为camel) 健康指示器 (例如。 ExampleOneHealthIndicator --&燃气轮机; exampleOne )

    其他名称(例如 diskSpace 或 ping )来自内置的运行状况检查,类名的命名与您期望的一样( DiskSpaceHealthIndicator & PingHealthIndicator )

    请注意,顶层 status 是 DOWN . 这是由 状态聚合器 . 默认值为 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

        2
  •  3
  •   Alex    8 年前

    1) 您可以有任意多的健康指标。只需创建将扩展的bean org.springframework.boot.actuate.health.HealthIndicator 它们将由致动器的健康检查端点自动拾取。bean的名称将是特定健康指示器的名称。在您的示例中 myHealthCheck 和 diskSpace 是spring上下文中的bean,当您点击 /health . diskSpace是spring boot comming中预定义的运行状况指标之一 org.springframework.boot.actuate.health.DiskSpaceHealthIndicator .

    2) 顶级状态是您所有健康指标的累积状态。您可以配置其工作方式,但默认情况下,它将显示最低状态(您有一个运行状况指标处于下降状态,因此累积状态显示为下降)