通过配置readiness探针和preStop hook,实现优雅启动和停止(滚动部署)
- k8s工作负载配置
python
readinessProbe:
httpGet:
path: /datetime
port: 8080
scheme: HTTP
initialDelaySeconds: 30
timeoutSeconds: 1
periodSeconds: 30
successThreshold: 1
failureThreshold: 6
lifecycle:
preStop:
exec:
command:
- /bin/sh
- '-c'
- sleep 15s
- 网关改造
java
@SpringBootApplication
public class GracefulShutdownApplication {
public static void main(String[] args) {
SpringApplication.run(GracefulShutdownApplication.class, args);
}
@Bean
public GracefulShutdown gracefulShutdown() {
return new GracefulShutdown();
}
}
java
public class GracefulShutdown implements ApplicationListener<ContextClosedEvent> {
@Override
public void onApplicationEvent(ContextClosedEvent contextClosedEvent) {
HttpResources.disposeLoopsAndConnectionsLater().delaySubscription(Duration.ofSeconds(20)).block();
}
}
经过测试发现,可以实现请求0失败