Spring GracefulShutdown在k8s中被调用两次

vfhzx4xs  于 2023-04-19  发布在  Spring
关注(0)|答案(1)|浏览(125)

看起来我的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"]
0md85ypi

0md85ypi1#

它之所以会这样,是因为你在不同的端口上有app和actuator(至少我从你的Deployment描述符中猜到了)。检查你的启动日志,你应该会看到这样的内容:

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 ''

因此,当应用程序关闭时,这两个都将关闭。

相关问题