Spring Boot 2.3 gracefully closes the new pose, which is really fragrant!

The latest Spring Boot 2.3 is almost half a month old: h...
How to open and close gracefully?
Source code analysis

The latest Spring Boot 2.3 is almost half a month old:

https://spring.io/blog/2020/05/15/spring-boot-2-3-0-available-now

One of the new features is: Graceful shutdown

I've shared such articles before, but now I've even produced the official gesture. For the new function, I have to go to the official for a look. Here's the official description:

Graceful shutdown is supported with all four embedded web servers (Jetty, Reactor Netty, Tomcat, and Undertow) and with both reactive and Servlet-based web applications. When a grace period is configured, upon shutdown, the web server will no longer permit new requests and will wait for up to the grace period for active requests to complete.

Next, the stack leader will summarize:

Graceful shutdown supports all four embedded Web servers: jetty, reactor netty, Tomcat, undercow, and responsive and Servlet based Web applications.

When an elegant shutdown buffer time is configured, the Web server is no longer allowed to receive new requests until the application is shut down. The buffer time is to wait for all active requests currently in progress to be processed.

It should be noted that Tomcat and Jetty will immediately stop receiving requests at the network layer, while Undertow can continue to receive requests, but will immediately return 503 service unavailable error.

Note: Tomcat effective version requires: 9.0.33+

How to open and close gracefully?

The following is an example configuration of the Yaml file:

# Open and close gracefully server: shutdown: graceful # Buffer time off spring: lifecycle: timeout-per-shutdown-phase: 10s

Source code analysis

The above describes the configuration mode of graceful shutdown parameters. Next, we can see what the default configuration is through the source code.

First, let's see the first parameter configuration receiving class:

org.springframework.boot.autoconfigure.web.ServerProperties

public enum Shutdown { GRACEFUL, IMMEDIATE; private Shutdown() { } }

As shown in the source code, the default is: IMMEDIATE, so elegant closing is manually opened according to business needs.

Let's look at the second parameter configuration receiving class:

org.springframework.boot.autoconfigure.context.LifecycleProperties

As shown in the source code, the default buffer time is: 30 seconds.

Take a look at the elegant closed source code:

According to Graceful, we can find several related classes. Let's go to Tomcat's:

org.springframework.boot.web.embedded.tomcat.GracefulShutdown

public enum GracefulShutdownResult { /** * Requests remained active at the end of the grace period. */ REQUESTS_ACTIVE, /** * The server was idle with no active requests at the end of the grace period. */ IDLE, /** * The server was shutdown immediately, ignoring any active requests. */ IMMEDIATE; }

REQUESTS_ACTIVE means that the connection remains active before the end of the buffer period, that is, although the official default gives you 30 seconds of cache time to process the backlog request, if the processing is not completed after 30 seconds, Spring Boot will also force the application to close.

Therefore, it should be noted that when closing gracefully, the business processing volume at that time must be considered, and whether the set buffer time can handle the business in process.

In addition, Spring Boot graceful shutdown needs to be triggered in conjunction with the / shutdown endpoint of the activator. Please refer to this article for details: Several methods of Spring Boot to stop service gracefully.

Well, today, the head of the stack has shared it. You can learn about it, and friends who are interested in the new feature can also test it.

Recommend to my blog to read more:

1.Java JVM, collection, multithreading, new features series

2.Spring MVC, Spring Boot, Spring Cloud series tutorials

3.Maven, Git, Eclipse, Intellij IDEA series tools tutorial

4.Latest interview questions of Java, backend, architecture, Alibaba and other large factories

Feel good, don't forget to like + forward!

28 May 2020, 23:47 | Views: 3095

Add new comment

For adding a comment, please log in
or create account

0 comments