前期踩的坑 (spring boot 1.x)
創(chuàng)新互聯(lián)公司是專(zhuān)業(yè)的蘄春網(wǎng)站建設(shè)公司,蘄春接單;提供網(wǎng)站制作、成都做網(wǎng)站,網(wǎng)頁(yè)設(shè)計(jì),網(wǎng)站設(shè)計(jì),建網(wǎng)站,PHP網(wǎng)站建設(shè)等專(zhuān)業(yè)做網(wǎng)站服務(wù);采用PHP框架,可快速的進(jìn)行蘄春網(wǎng)站開(kāi)發(fā)網(wǎng)頁(yè)制作和功能擴(kuò)展;專(zhuān)業(yè)做搜索引擎喜愛(ài)的網(wǎng)站,專(zhuān)業(yè)的做網(wǎng)站團(tuán)隊(duì),希望更多企業(yè)前來(lái)合作!
1. 添加mavne依賴(lài)
org.springframework.boot spring-boot-starter-actuator
2. 啟用shutdown
在配置文件里添加下面的配置
#啟用shutdown endpoint的HTTP訪(fǎng)問(wèn) endpoints.shutdown.enabled=true #不需要驗(yàn)證 endpoints.shutdown.sensitive=false
啟動(dòng)的時(shí)候可以看到下面的日志,就說(shuō)明成功了
3. 優(yōu)雅停機(jī)
發(fā)送POST請(qǐng)求 http://localhost:8080/shutdown
如果響應(yīng)碼是404 可以嘗試POST http://localhost:8080/actuator/shutdown
spring boot 2.0
如果你使用的spring boot版本是2.x的就會(huì)發(fā)現(xiàn),這些POST請(qǐng)求都會(huì)出現(xiàn)404的結(jié)果。
下面是spring boot 2.0 優(yōu)雅停機(jī)的實(shí)現(xiàn)方式。
1.修改Application啟動(dòng)類(lèi)
tomcat容器
@SpringBootApplication public class ShutdownApplication { public static void main(String[] args) { SpringApplication.run(ShutdownApplication.class, args); } /** * 用于接受 shutdown 事件 */ @Bean public GracefulShutdown gracefulShutdown() { return new GracefulShutdown(); } /** * 配置tomcat * * @return */ @Bean public ServletWebServerFactory servletContainer() { TomcatServletWebServerFactory tomcat = new TomcatServletWebServerFactory(); tomcat.addConnectorCustomizers(gracefulShutdown()); return tomcat; } /** * 優(yōu)雅關(guān)閉 Spring Boot。容器必須是 tomcat */ private class GracefulShutdown implements TomcatConnectorCustomizer, ApplicationListener{ 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 instanceof ThreadPoolExecutor) { try { ThreadPoolExecutor threadPoolExecutor = (ThreadPoolExecutor) executor; threadPoolExecutor.shutdown(); if (!threadPoolExecutor.awaitTermination(waitTime, TimeUnit.SECONDS)) { log.warn("Tomcat 進(jìn)程在" + waitTime + " 秒內(nèi)無(wú)法結(jié)束,嘗試強(qiáng)制結(jié)束"); } } catch (InterruptedException ex) { Thread.currentThread().interrupt(); } } } } }
Undertow容器 (沒(méi)有使用過(guò),不保證可用)
@SpringBootApplication public class Application { public static void main(String[] args) { SpringApplication.run(Application.class, args); } /** * 優(yōu)雅關(guān)閉 Spring Boot */ @Component public class GracefulShutdown implements ApplicationListener{ @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 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. 使用 kill命令殺死進(jìn)程
使用下面的命令殺死進(jìn)程。該命令是向 某個(gè)進(jìn)程發(fā)送終止信號(hào)。
kill -15 [PID]
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持創(chuàng)新互聯(lián)。