【问题标题】:How to add a custom health check in spring boot health?如何在 Spring Boot Health 中添加自定义健康检查?
【发布时间】:2017-12-04 14:20:05
【问题描述】:
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-actuator</artifactId>
</dependency>

这将为您的应用程序添加几个有用的端点。其中之一是 /health。当您启动应用程序并导航到 /health 端点时,您会看到它已经返回了一些数据。

{
    "status":"UP",
    "diskSpace": {
        "status":"UP",
        "free":56443746,
        "threshold":1345660
    }
}

如何在spring boot health中添加自定义健康检查?

【问题讨论】:

    标签: spring spring-boot spring-boot-actuator


    【解决方案1】:

    添加自定义运行状况检查很容易。只需创建一个新的 Java 类,从 AbstractHealthIndicator 扩展它并实现 doHealthCheck 方法。该方法通过一些有用的方法获取一个构建器。如果您的健康状况良好,请致电 builder.up(),否则请致电 builder.down()。您如何检查健康状况完全取决于您。也许你想 ping 一些服务器或检查一些文件。

    @Component
    public class CustomHealthCheck extends AbstractHealthIndicator {
        @Override
        protected void doHealthCheck(Health.Builder bldr) throws Exception {
            // TODO implement some check
            boolean running = true;
            if (running) {
              bldr.up();
            } else {
              bldr.down();
            }
        }
    }
    

    这足以激活新的健康检查(确保 @ComponentScan 在您的应用程序上)。重新启动您的应用程序并将您的浏览器定位到 /health 端点,您将看到新添加的健康检查。

    {
        "status":"UP",
        "CustomHealthCheck": {
            "status":"UP"
        },
        "diskSpace": {
            "status":"UP",
            "free":56443746,
            "threshold":1345660
        }
    }
    

    【讨论】:

    • 对于k8s的ready检查,还需要在application.properties文件key-value management.endpoint.health.group.readiness.include="custom,readinessState"添加custom
    【解决方案2】:

    自 Spring Boot 2.X 起

    正如@yuranos87 所述,执行器概念在 Spring Boot 2.X 中发生了变化,但您仍然可以通过实现 HealthIndicator 或对于响应式应用程序 ReactiveHealthIndicator 轻松添加自定义 健康检查

    @Component
    public class CacheHealthIndicator implements HealthIndicator {
    
    @Override
    public Health health() {
        long result = checkSomething();
        if (result <= 0) {
            return Health.down().withDetail("Something Result", result).build();
        }
        return Health.up().build();      
      }
    }
    

    @Component
    public class CacheHealthIndicator implements ReactiveHealthIndicator {
    
    @Override
    public Mono<Health> health() {
        return Mono.fromCallable(() -> checkSomething())
            .map(result -> {
                if (result <= 0) {
                    return Health.down().withDetail("Something Result", result).build();
                }
                return Health.up().build();
            });
       }
    }
    

    此外,您可以使用@Endpoint@EndpointWebExtension 添加或扩展任何端点。这里的端点是infohealth 等等。因此,您可以使用 @Endpoint 添加自定义健康检查,但使用 HealthIndicator 更容易。

    您可以在 Spring Boot 文档中找到有关 custom health checkscustom endpoints 的更多信息。

    【讨论】:

    • 还需要添加以下属性,以便在响应中添加自定义消息management.endpoint.health.show-details=always
    【解决方案3】:

    Spring Boot 2.X 显着改变了执行器。通过@EndpointWebExtension 启用了一种新的、更好的扩展现有端点的机制。

    话虽如此,健康端点的扩展有点棘手,因为它的一个扩展是由执行器本身开箱即用提供的。如果不操纵 bean 初始化过程,您的应用程序将无法启动,因为它会看到 2 个扩展并且不知道该选择哪一个。 一种更简单的方法是使用 info 并对其进行扩展:

    @Component
    @EndpointWebExtension(endpoint = InfoEndpoint.class)
    public class InfoWebEndpointExtension {
       @Value("${info.build.version}")
       private String versionNumber;
       @Value("${git.commit.id}")
       private String gitCommit;
       @Value("${info.build.name}")
       private String applicationName;
       ...
       @ReadOperation
       public WebEndpointResponse<Map> info() {
    

    不要忘记您还可以重新映射 URL。在我的情况下,我更喜欢 /status/health 并且不希望 /actuator/ 在路径中:

    management.endpoints.web.base-path=/
    management.endpoints.web.path-mapping.info=status
    

    我更喜欢 /info 的另一个原因是因为我没有得到这种嵌套结构,这是 /health 的默认结构:

    {
    "status": {
        "status": "ON",
    

    【讨论】:

      【解决方案4】:

      如果您想要自定义状态消息,那么您可以在此处查看答案 - https://stackoverflow.com/a/66985769/4952800

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2019-08-05
        • 1970-01-01
        • 2019-04-29
        • 1970-01-01
        • 2023-03-10
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多