Get the App
SLTechnology News&Howtos  ›  Development  › 

How to stop service gracefully for SpringBoot

Shulou Source: shulou.com Published: 2022-06-03 08:44:01 09月15日 Update

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!

Tags: Elegance processing service container logic support business source code content function embedded situation method time more server thread buffer period behavior problem Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Xiaomi Linux OPPO Reno Microsoft MariaDB