【问题标题】:Spring Boot HealthIndicator by ExampleSpring Boot HealthIndicator 示例
【发布时间】:2017-12-22 03:07:04
【问题描述】:

我希望在我的 Spring Boot REST 服务上构建一个强大而详细的健康检查端点 (/health)。我刚刚阅读了 this excellent Baeldung article 关于这个主题的内容,但仍有一些顾虑。

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

我不知道 Spring Boot 是否希望您构建一个且只有一个 HealthIndicator impl。 或者如果想法是为每个主要子系统构建一个HealthIndicator impl(每个子系统都可以独立地“up”或“down” "分别。

另外,在那个 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从何而来?!

【问题讨论】:

    标签: java spring spring-boot spring-boot-actuator health-monitoring


    【解决方案1】:

    您可以实现多个HealthIndicators,它们都会对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

    【讨论】:

      【解决方案2】:

      1) 您可以拥有尽可能多的健康指标。只需创建将扩展 org.springframework.boot.actuate.health.HealthIndicator 的 bean,它们将被执行器的健康检查端点自动拾取。您的 bean 的名称将是某些健康指标的名称。在您的示例中,myHealthCheckdiskSpace 是在您点击/health 时调用的spring 上下文中的bean。 diskSpace 是来自org.springframework.boot.actuate.health.DiskSpaceHealthIndicator 的spring boot 中预定义的健康指标之一。

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

      【讨论】:

      • 非常感谢@SimY4 (+1) - 我如何自己配置顶级状态?我知道默认是采用所有健康指标的最低状态,但在我的特殊情况下,我有一些自定义逻辑来确定整体健康状况(一些 HealthIndicators 可能已关闭,服务可能仍被视为“启动” , ETC。)。有任何想法吗?再次感谢!
      • @smeeb 你必须提供你自己的org.springframework.boot.actuate.health.HealthAggregator bean,它将为你的健康指标产生累积状态,或者使用你的自定义状态。比如,您可以介绍自己的状态 SEEMS_OK 或 ALMOST,并使用 management.health.status.order= 属性将它们与其他人排序
      猜你喜欢
      • 1970-01-01
      • 2017-11-24
      • 2016-09-20
      • 2018-07-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-08-18
      • 2015-02-27
      相关资源
      最近更新 更多