The pitfalls I stepped on in the early stage (spring boot 1.x)
1. Add mavne dependencies
<!-- springboot monitoring--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency>
2. Enable shutdown
Add the following configuration file
#Enable HTTP access of shutdown endpoint endpoint endpoint endpoint endpoint endpoint endpoint.shutdown.enabled=true#No need to verify endpoints.shutdown.sensitive=false
You can see the following log when starting, which means it is successful
3. Elegant shutdown
Send POST request http://localhost:8080/shutdown
If the response code is 404, you can try POST http://localhost:8080/actuator/shutdown
spring boot 2.0
If the spring boot version you are using is 2.x, you will find that these POST requests will have a result of 404.
Here is how spring boot 2.0 elegant downtime is implemented.
1. Modify the Application Startup Class
Tomcat container
@SpringBootApplicationpublic class ShutdownApplication { public static void main(String[] args) { SpringApplication.run(ShutdownApplication.class, args); } /** * Used to accept shutdown events*/ @Bean public GracefulShutdown gracefulShutdown() { return new GracefulShutdown(); } /** * Configure tomcat * * @return */ @Bean public ServletWebServerFactory servletContainer() { TomcatServletWebServerFactory tomcat = new TomcatServletWebServerFactory(); tomcat.addConnectorCustomizers(gracefulShutdown()); return tomcat; } /** * Close Spring Boot gracefully. The container must be tomcat */ private class GracefulShutdown implements TomcatConnectorCustomizer, ApplicationListener<ContextClosedEvent> { private final Logger log = LoggerFactory.getLogger(GracefulShutdown.class); private volatile Connector connector; private final int waitTime = 10; @Override public void customize(Connector connector) { this.connector = connector; } @Override public void onApplicationEvent(ContextClosedEvent contextClosedEvent) { this.connector.pause(); Executor executor = this.connector.getProtocolHandler().getExecutor(); if (executor instance of ThreadPoolExecutor) { try { ThreadPoolExecutor threadPoolExecutor = (ThreadPoolExecutor) executor; threadPoolExecutor.shutdown(); if (!threadPoolExecutor.awaitTermination(waitTime, TimeUnit.SECONDS)) { log.warn("Tomcat process cannot end in " + waitTime + " seconds, try to force end"); } } catch (InterruptedException ex) { Thread.currentThread().interrupt(); } } } } } } } } } } } }Undertow container (not used, not guaranteed to be available)
@SpringBootApplicationpublic class Application { public static void main(String[] args) { SpringApplication.run(Application.class, args); } /** * Close Spring Boot elegantly */ @Component public class GracefulShutdown implements ApplicationListener<ContextClosedEvent> { @Autowired private GracefulShutdownWrapper gracefulShutdownWrapper; @Autowired private ServletWebServerApplicationContext context; @Override public void onApplicationEvent(ContextClosedEvent contextClosedEvent){ gracefulShutdownWrapper.getGracefulShutdownHandler().shutdown(); try { UndertowServletWebServer webServer = (UndertowServletWebServer)context.getWebServer(); Field field = webServer.getClass().getDeclaredField("undertow"); field.setAccessible(true); Undertow undertow = (Undertow) field.get(webServer); List<Undertow.ListenerInfo> listenerInfo = undertow.getListenerInfo(); Undertow.ListenerInfo listener = listenerInfo.get(0); ConnectorStatistics connectorStatistics = listener.getConnectorStatistics(); while (connectorStatistics.getActiveConnections() > 0){} } catch (Exception e){ // Application Shutdown } } } @Component public class GracefulShutdownWrapper implements HandlerWrapper{ private GracefulShutdownHandler gracefulShutdownHandler; @Override public HttpHandler wrap(HttpHandler handler) { if(gracefulShutdownHandler == null) { this.gracefulShutdownHandler = new GracefulShutdownHandler(handler); } return gracefulShutdownHandler; } public GracefulShutdownHandler getGracefulShutdownHandler() { return gracefulShutdownHandler; } } @Component @AllArgsConstructor public class UndertowExtraConfiguration { private final GracefulShutdownWrapper gracefulShutdownWrapper; @Bean public UndertowServletWebServerFactory servletWebServerFactory() { UndertowServletWebServerFactory factory = new UndertowServletWebServerFactory(); factory.addDeploymentInfoCustomizers(deploymentInfo -> deploymentInfo.addOuterHandlerChainWrapper(gracefulShutdownWrapper)); factory.addBuilderCustomizers(builder -> builder.setServerOption(UndertowOptions.ENABLE_STATISTICS, true)); return factory; } }}2. Use the kill command to kill the process
Use the following command to kill the process. This command sends a termination signal to a process.
kill -15 [PID]
The above is all the content of this article. I hope it will be helpful to everyone's learning and I hope everyone will support Wulin.com more.