似乎我的spring应用程序在k8s部署中调用了两次优雅关闭。有人有类似的问题吗?
{"level":"INFO","message":"Commencing graceful shutdown. Waiting for active requests to complete","logger":"org.springframework.boot.web.embedded.tomcat.GracefulShutdown","thread":"SpringApplicationShutdownHook"}
{"level":"INFO","message":"Graceful shutdown complete","logger":"org.springframework.boot.web.embedded.tomcat.GracefulShutdown","thread":"tomcat-shutdown"}
{"level":"INFO","message":"Commencing graceful shutdown. Waiting for active requests to complete","logger":"org.springframework.boot.web.embedded.tomcat.GracefulShutdown","thread":"SpringApplicationShutdownHook"}
{"level":"INFO","message":"Graceful shutdown complete","logger":"org.springframework.boot.web.embedded.tomcat.GracefulShutdown","thread":"tomcat-shutdown"}
{"level":"INFO","message":"Closing JPA EntityManagerFactory for persistence unit 'default'","traceId":"","spanId":"","requestId":"","user":"","logger":"org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean","thread":"SpringApplicationShutdownHook"}
应用程序属性:
spring:
lifecycle:
timeout-per-shutdown-phase: 20s
server:
shutdown: graceful
我的简化部署:
apiVersion: apps/v1
kind: Deployment
metadata:
name: my-api
spec:
template:
spec:
containers:
- name: my-container
image: my-api
ports:
- name: container-port
containerPort: 8080
- name: metrics
containerPort: 8081
lifecycle:
preStop:
exec:
command: ["sh", "-c", "sleep 10"]
- name: cloud-sql-proxy
image: gcr.io/cloudsql-docker/gce-proxy:1.30.0
command:
- "/cloud_sql_proxy"
- "-ip_address_types=PRIVATE"
- "-structured_logs"
- "-verbose=false"
lifecycle:
preStop:
exec:
command: ["sh", "-c", "sleep 10"]
它的行为方式,因为你有应用程序和执行器在不同的端口(至少我猜从你的部署描述符)。检查你的启动日志,你应该看到如下内容:
o.s.b.w.e.t.TomcatWebServer Tomcat initialized with port(s): 8081 (http)
o.a.c.h.Http11NioProtocol Initializing ProtocolHandler ["http-nio-8081"]
o.a.c.h.Http11NioProtocol Starting ProtocolHandler ["http-nio-8081"]
o.s.b.w.e.t.TomcatWebServer Tomcat started on port(s): 8081 (http) with context path ''
o.s.b.w.e.t.TomcatWebServer Tomcat initialized with port(s): 8080 (http)
o.a.c.h.Http11NioProtocol Initializing ProtocolHandler ["http-nio-8080"]
o.a.c.h.Http11NioProtocol Starting ProtocolHandler ["http-nio-8080"]
o.s.b.w.e.t.TomcatWebServer Tomcat started on port(s): 8080 (http) with context path ''
所以当应用程序关闭时,这两个都被关闭。