首页 > 解决方案 > Spring Boot 自定义 Kubernetes 就绪探针

问题描述

我想实现自定义逻辑来确定我的 pod 的准备情况,我检查了这个:https ://docs.spring.io/spring-boot/docs/current/reference/html/actuator.html#actuator.endpoints.kubernetes -probes.external-state并且他们提到了一个示例属性: management.endpoint.health.group.readiness.include=readinessState,customCheck

问题是 - 我如何覆盖customCheck?在我的情况下,我想使用 HTTP 探针,所以 yaml 看起来像:

readinessProbe:
  initialDelaySeconds: 10
  periodSeconds: 10
  httpGet:
    path: /actuator/health
    port: 12345

话又说回来-我应该在哪里以及如何应用确定应用程序何时准备就绪的逻辑(就像上面的链接一样,我想依靠外部服务才能准备好)

标签: spring-bootkuberneteskubernetes-health-check

解决方案


customCheck 是自定义 HealthIndicator 的键。给定 HealthIndicator 的键是不带 HealthIndicator 后缀的 bean 的名称

您可以阅读: https ://docs.spring.io/spring-boot/docs/current/reference/html/actuator.html#actuator.endpoints.health.writing-custom-health-indicators

您正在定义 readinessProbe,因此可能点击 /actuator/health/readiness 是更好的选择。

public class CustomCheckHealthIndicator extends AvailabilityStateHealthIndicator {

    private final YourService yourService;

    public CustomCheckHealthIndicator(ApplicationAvailability availability, YourService yourService) {
        super(availability, ReadinessState.class, (statusMappings) -> {
            statusMappings.add(ReadinessState.ACCEPTING_TRAFFIC, Status.UP);
            statusMappings.add(ReadinessState.REFUSING_TRAFFIC, Status.OUT_OF_SERVICE);
        });
        this.yourService = yourService;
    }

    @Override
    protected AvailabilityState getState(ApplicationAvailability applicationAvailability) {
        if (yourService.isInitCompleted()) {
            return ReadinessState.ACCEPTING_TRAFFIC;
        } else {
            return ReadinessState.REFUSING_TRAFFIC;
        }
    }

}

推荐阅读