【问题标题】:How to extend existing actuator health check endpoint?如何扩展现有的执行器健康检查端点?
【发布时间】:2019-09-30 22:04:20
【问题描述】:

有现有的执行器健康端点,例如:

/actuator/health

我将如何扩展这个现有的端点来表示:

/actuator/health/myendpoint

为了执行一些健康检查?

当前代码:

package com.example.actuatordemo.health;

import org.springframework.boot.actuate.health.AbstractHealthIndicator;
import org.springframework.boot.actuate.health.Health;
import org.springframework.stereotype.Component;

@Component
public class CustomHealthIndicator extends AbstractHealthIndicator {

    @Override
    protected void doHealthCheck(Health.Builder builder) throws Exception {
        // Use the builder to build the health status details that should be reported.
        // If you throw an exception, the status will be DOWN with the exception message.

        builder.up()
                .withDetail("app", "Testing endpoint extension!")
                .withDetail("error", "Oops.");
    }
}

【问题讨论】:

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


    【解决方案1】:

    为了扩展 /health 端点,您必须像这样实现 HealthIndicator 接口。在此示例中,自定义 HealthService 返回您要添加到健康端点的所需值的映射。

      import com.metavera.tako.fc.service.HealthService;
      import org.springframework.beans.factory.annotation.Autowired;
      import org.springframework.boot.actuate.health.*;
      import org.springframework.stereotype.Component;
    
      import java.util.Map;
    
    
      @Component
      public class HealthCheck implements HealthIndicator {
    
      @Autowired
      HealthService healthService;
    
    
      @Override
      public Health health() {
          return myCustomHealth();
      }
    
      private Health myCustomHealth() {
          Health.Builder builder = new Health.Builder(Status.UP);
          Map<String, Object> response = healthService.getHealthStatus();
    
          for (Map.Entry<String, Object> entry : response.entrySet()) {
              String key = entry.getKey();
              Object value = response.get(key);
              builder.withDetail(key, value);
          }
          return builder.build();
      }
    }
    

    虽然上述解决方案允许您修改现有的 /health 端点,但在第 53.7 节下还有关于如何创建自定义端点的附加文档。

    https://docs.spring.io/spring-boot/docs/current/reference/html/production-ready-endpoints.html

    虽然这不仅限于健康检查操作。

    【讨论】:

    • 如何在没有硬块的情况下实现 HealthIndicator。应用程序仍应启动,但状态应显示连接失败
    • @Minisha 健康端点应该返回当前时间点的健康状况。所以阻塞对我来说很有意义。如果您不想阻止“无限”时间,我建议使用超时。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-05-29
    • 1970-01-01
    • 1970-01-01
    • 2020-01-17
    • 2018-10-29
    相关资源
    最近更新 更多