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