spring cloud gateway k8s优雅启停

通过配置readiness探针和preStop hook,实现优雅启动和停止(滚动部署)

1. k8s工作负载配置

          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

2. 网关改造

@SpringBootApplication
public class GracefulShutdownApplication {

	public static void main(String[] args) {
		SpringApplication.run(GracefulShutdownApplication.class, args);
	}

	@Bean
	public GracefulShutdown gracefulShutdown() {
		return new GracefulShutdown();
	}
}
public class GracefulShutdown implements ApplicationListener {
    @Override
    public void onApplicationEvent(ContextClosedEvent contextClosedEvent) {
        HttpResources.disposeLoopsAndConnectionsLater().delaySubscription(Duration.ofSeconds(20)).block();
    }
}

经过测试发现,可以实现请求0失败

你可能感兴趣的:(kubernetes,容器,云原生)