gpt4 book ai didi

spring-boot - Spring Boot 自定义 Kubernetes 就绪探测

转载 作者:行者123 更新时间:2023-12-05 03:36:29 24 4
gpt4 key购买 nike

我想实现自定义逻辑来确定我的 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

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

最佳答案

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;
}
}

}

关于spring-boot - Spring Boot 自定义 Kubernetes 就绪探测,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69577468/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com