Как расширить существующую конечную точку проверки состояния привода? - PullRequest
0 голосов
/ 01 октября 2019

Существуют конечные точки работоспособности привода, такие как:

/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 голосов
/ 15 октября 2019

Чтобы расширить конечную точку / 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();
  }
}

Хотя приведенное выше решение позволяет изменять существующую конечную точку / работоспособность, имеется дополнительная документацияо том, как создать пользовательские конечные точки здесь, в разделе 53.7.

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

Хотя это не ограничивается только операциями проверки здоровья.

...