In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-05 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
It is believed that many inexperienced people have no idea about how to stop service gracefully in SpringBoot. Therefore, this article summarizes the causes and solutions of the problem. Through this article, I hope you can solve this problem.
I. introduction
When external traffic is requested to the server interface to execute business logic, if the server performs a kill at this time, spring boot will directly close the container (tomcat, etc.) by default, causing the business logic to fail. In some business scenarios, data inconsistencies will occur and transaction logic will not be rolled back.
In the latest version of spring boot 2.3, this feature is built in, eliminating the need to expand the container thread pool. Currently, spring boot embedded web servers (Jetty, Reactor Netty, Tomcat and Undertow) and reactive and Servlet-based web applications all support graceful downtime.
For the shutdown behaviors supported here, let's take a look at the source code enumeration as follows:
Public enum Shutdown {/ * elegant downtime (limited downtime) * * / GRACEFUL, / * immediate shutdown * / IMMEDIATE;} 2. Use
When enabled with server.shutdown=graceful, when the web container is closed, the web server will no longer receive new requests and will wait for the buffer period for the active request to complete.
Timeout-per-shutdown-phase configuration of buffer period: the default time is 30s, which means the maximum waiting time is 30s. No matter whether the thread task is completed or not, the timeout will be stopped. Be sure to set it reasonably according to the actual needs of the project.
After the above configuration is completed and re-released, the server supports elegant suspension of service.
3. Trigger elegant suspension of service 1. Execute kill-2 instead of kill-9 for PID entering the city.
Kill-9, the violence aesthetics force kill process, does not execute ShutdownHook;, but kill-2 is equivalent to the shortcut key Ctrl + C that triggers Java's ShutdownHook event handling for elegant downtime or some post-processing. Please refer to the following source code:
@ Override public void registerShutdownHook () {if (this.shutdownHook = = null) {/ / No shutdown hook registered yet. This.shutdownHook = new Thread (SHUTDOWN_HOOK_THREAD_NAME) {@ Override public void run () {synchronized (startupShutdownMonitor) {doClose ();}; Runtime.getRuntime () .addShutdownHook (this.shutdownHook);}} 2. Graceful downtime through actuate endpoint
Graceful shutdown can be performed with POST request / actuator/shutdown. The source code is parsed as follows:
@ Endpoint (id = "shutdown", enableByDefault = false) public class ShutdownEndpoint implements ApplicationContextAware {@ WriteOperation public Map shutdown () {Thread thread = new Thread (this::performShutdown); thread.setContextClassLoader (getClass (). GetClassLoader ()); thread.start ();} private void performShutdown () {try {Thread.sleep (500L);} catch (InterruptedException ex) {Thread.currentThread (). Interrupt () } / / here the close logic is the same as the above shutdownhook processing this.context.close ();}} III. Different spring boot embedded web containers differ in elegant downtime behavior.
After reading the above, have you mastered how SpringBoot can stop service gracefully? If you want to learn more skills or want to know more about it, you are welcome to follow the industry information channel, thank you for reading!
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.