In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)06/01 Report--
This article is to share with you about how the springboot project should be closed in the docker container. The editor thinks it is very practical, so I share it with you. I hope you can get something after reading this article.
Preface what is elegantly closed
In my opinion, the so-called elegant shutdown is to set aside some time when the system shuts down, so that you have a chance to take care of something.
When do you need an elegant shutdown?
Do all projects need to be gracefully closed? Not necessarily, after all, the so-called elegant closure, on the other hand, means that it closes slowly, so the elegant closure of the project depends on the core degree of the project, in other words, whether the data processed by the project is the core data. In fact, the ultimate essence of the project is the processing of data.
How to achieve elegant shutdown
The general method is through the process to receive the system signal variable to achieve, what is the signal variable, you can refer to the following link https://www.cnblogs.com/liuhouhou/p/5400540.html
How to configure graceful shutdown 1 in the springboot project? if it is a springboot2.3 version, you can introduce the following jar com.github.timpeeters spring-boot-graceful-shutdown X.X.X
By introducing this jar and configuring it in the project's application.yml file
Graceful: shutdown: enabled: true
It only takes two steps to achieve an elegant shutdown. When the project is closed, look at the log and you will find the following output
The springboot versions currently supported by spring-boot-graceful-shutdown are as follows
Its implementation process
For more detailed tutorials, please refer to the following link https://github.com/timpeeters/spring-boot-graceful-shutdown
2. If springboot is version 2.3, you can directly configure server: # enable elegant shutdown in application.yml. Default: IMMEDIATE, immediately disable shutdown: gracefulspring: lifecycle: # configure elegant shutdown grace time, that is, if the project is not finished in 30s, force shutdown timeout-per-shutdown-phase: 30s
The closing effect is as follows
2021-01-15 10 o.s.b.w.e.tomcat.GracefulShutdown 52V 04.063 INFO 39004-[extShutdownHook] o.s.b.w.e.tomcat.GracefulShutdown: Commencing graceful shutdown. Waiting for active requests to complete2021-01-15 10 nfoReplicator-0 52V 04.138 INFO 39004-[nfoReplicator-0] com.netflix.discovery.DiscoveryClient: DiscoveryClient_HELLO-LOCAL/hello-local:172.17.45.115:8080-registration status: 2042021-01-15 10V 52mer 04.605 INFO 39004-[tomcat-shutdown] o.s.b.w.e.tomcat.GracefulShutdown: Graceful shutdown completec.netflix.discovery.TimedSupervisorTask: task supervisor shutting down How to gracefully close can't accept the task in a docker container
If you do the following configuration in Dockerfile
ENTRYPOINT ["sh", "- c", "java $JAVA_OPTS-Djava.security.egd=file:/dev/./urandom-jar / app.jar"]
It is impossible to achieve the effect of elegant closure. The reason is that when using docker stop to close the container, only the init (pid 1) process can receive the interrupt signal. If the pid 1 process of the container is a sh process, it does not have the ability to forward the end signal to its child process, so our real java program can not get the interrupt signal, so it can not be gracefully closed. The solution is to let the pid 1 process have a forwarding termination signal, or configure the java program into a pid 1 process. So you only need to make the following modifications to Dockerfile
ENTRYPOINT ["sh", "- c", "exec java $JAVA_OPTS-Djava.security.egd=file:/dev/./urandom-jar / app.jar"]
In fact, you can add exec in front of java. Its implementation mechanism can be referred to the following link https://spring.io/guides/topicals/spring-boot-docker
How to do graceful closing in k8s
1. Configure preStop Hook hooks
PreStop Hook is a container special command or Http request sent to Pod. If your application does not close properly when receiving SIGTERM, you can use preStop Hook to trigger a normal shutdown. Most programs close normally when receiving SIGTERM, but if you are using third-party code or the managed system is out of control, preStop Hook is a good way to trigger a normal shutdown without modifying the application.
2. Extend the terminationGracePeriodSeconds time appropriately.
The meaning of the terminationGracePeriodSeconds thing is as follows
The configuration reference is as follows
ApiVersion: apps/v1kind: Deploymentmetadata: namespace: dev name: hello labels: app: hellospec: replicas: 3 selector: matchLabels: app: hello template: metadata: labels: app: hellospec: imagePullSecrets:-name: default-secret containers:-name: hello image: {{images} ports:-containerPort: 8080 lifecycle: PreStop: httpGet: port: 8080 path: the action to be performed when you want to close terminationGracePeriodSeconds: 60
Elegant shutdown is normally configured with a certain processing time, beyond which it will be forced to kill before it is finished. Therefore, for the core business, we also have to consider whether we need to compensate the business in case of forced killing.
The above is how to close the springboot project in the docker container. The editor believes that there are some knowledge points that we may see or use in our daily work. I hope you can learn more from this article. For more details, please follow the industry information channel.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.