如何扩展现有的执行器健康检查端点?

时间:2019-09-30 22:04:20

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

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

/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.");
    }
}

1 个答案:

答案 0 :(得分:0)

为了扩展/ 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

尽管这不仅限于健康检查操作。