如何扩展现有的执行器运行状况检查端点?

问题描述 投票:0回答:1

存在现有的执行器运行状况端点,例如:

/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个回答
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

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

© www.soinside.com 2019 - 2024. All rights reserved.