编程知识 cdmana.com

Gracefully close spring boot application

Abreast of the times Spring Boot A new feature has been added --- Elegant shutdown .

The official introduction

Official document address :https://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#boot-features-graceful-shutdown

This mechanism will have a timeout , This timeout provides a grace period , During this grace period , Existing requests will be allowed to complete , And new requests will not be allowed . The exact way new requests are not allowed depends on the Web Servers vary . Jetty,Reactor Netty and Tomcat Will stop accepting requests at the network layer . Undertow The request will be accepted , But the service is immediately unavailable (503) Respond to respond to .

This article USES Tomcat Container testing ,Tomcat Version no less than 9.0.33 This mechanism will not take effect until the end of the year .

According to the official document , This feature is easy to use , Directly in application.properties Add server.shutdown=graceful that will do . You can also use spring.lifecycle.timeout-per-shutdown-phase=30s To configure the timeout . Next we create a Spring Boot project , Verify the effect of this elegant shutdown .

Code testing

Code in github On : https://github.com/hanbin/learn-spring-boot/tree/main/graceful-shutdown

The main code is to provide a simple interface , Print one line of log per second , Print 20 individual .

/** * @author han_bin@outlook.com * @Description TODO */@RestController@Slf4jpublic class TestController {    @GetMapping("/test")    public String test() throws InterruptedException {        for(int index=0; index<20; index++){            Thread.sleep(1000L);            log.info(" Threads {} Being implemented -{}", Thread.currentThread().getName(), index);        }        return " The loop ends " + Thread.currentThread().getName();    }}

Before looking at the effect , You need to understand the Idea in , Use Run Mode start project , Clicking the button in the figure below is equivalent to sending kill -15 xxx The order of .

kill -15  Exit procedure

1. Check the effect without turning on the elegant shutdown .

 Don't turn on and shut down

You can see , The application will directly interrupt the currently running service .

2. Add the configuration server.shutdown=graceful Turn on and check the results .

 Add the configuration server.shutdown=graceful Start graceful shutdown

You can see , The effect is exactly the same as the official description , The application exits after waiting for the service to be executed . And from Commencing graceful shutdown. Waiting for active requests to complete after , The client request interface has directly failed , That is to say, at this time, the application will no longer accept new requests from the client .

3. Add the configuration

   server.shutdown=graceful   spring.lifecycle.timeout-per-shutdown-phase=30s

Start graceful shutdown , View results .

You can see , The service exits at the end of the run , Didn't hold on until 30s To shut down the process completely .

4. Add the configuration

server.shutdown=gracefulspring.lifecycle.timeout-per-shutdown-phase=3s

Start graceful shutdown , View results .

You can see , While the service is still running , But it's the configured timeout , The app will try to close , But because there's a service that hasn't been processed yet , So the shutdown failed , Next, the graceful shutdown is interrupted and forced .

Summary

Before , In order to realize the elegant offline of micro service , There is a manual implementation of similar functions . But it's a good thing that the government provides . It's very convenient to turn on this function , However, the configuration of the timeout should be based on the specific business . So as not to cause the service can not be terminated for a long time or the service is terminated prematurely , The background business is not processed .

 Preview

版权声明
本文为[Han Bin]所创,转载请带上原文链接,感谢
https://cdmana.com/2021/04/20210421235129715i.html

Scroll to Top