spring boot 如何優(yōu)雅關(guān)閉服務(wù)
spring boot 優(yōu)雅的關(guān)閉服務(wù)
實(shí)現(xiàn)ContextClosedEvent 監(jiān)聽器,監(jiān)聽到關(guān)閉事件后,關(guān)閉springboot進(jìn)程
**網(wǎng)上有很多例子 使用spring boot 插件做關(guān)閉經(jīng)測試此插件只能是關(guān)閉spring boot服務(wù),不能殺死服務(wù)進(jìn)程。還是需要實(shí)現(xiàn)關(guān)閉監(jiān)聽,去殺死進(jìn)程。網(wǎng)上有很多例子 使用spring boot 插件做關(guān)閉經(jīng)測試此插件只能是關(guān)閉spring boot服務(wù),不能殺死服務(wù)進(jìn)程。還是需要實(shí)現(xiàn)關(guān)閉監(jiān)聽,去殺死進(jìn)程。網(wǎng)上有很多例子 使用spring boot 插件做關(guān)閉經(jīng)測試此插件只能是關(guān)閉spring boot服務(wù),不能殺死服務(wù)進(jìn)程。還是需要實(shí)現(xiàn)關(guān)閉監(jiān)聽,去殺死進(jìn)程。重要的事說三遍**
actuator 關(guān)閉spring boot 實(shí)現(xiàn)方式引入actuator 配置 shutdown調(diào)用http://127.0.0.1/xxx/
引入actuator<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency>-->
配置在application.properties中開啟關(guān)閉management.endpoints.web.exposure.include=*#management.endpoint.shutdown.enabled = true
1.調(diào)用
1.主入口public static ConfigurableApplicationContext configurableApplicationContext;public static void main(String[] args) throws InterruptedException { configurableApplicationContext = SpringApplication.run(GatewayApplication.class, args);}2.關(guān)閉監(jiān)聽@Controllerpublic static class ShutdownAction implements ApplicationListener<ContextClosedEvent> {@Overridepublic void onApplicationEvent(ContextClosedEvent event) {System.exit(SpringApplication.exit(configurableApplicationContext));}}3.關(guān)閉服務(wù)命令 /** * 關(guān)閉服務(wù) */ @RequestMapping(value = '/stop', method = RequestMethod.GET) @ResponseBody public void stopServer() { MySpringApplication.configurableApplicationContext.close(); }
到此這篇關(guān)于spring boot 如何優(yōu)雅關(guān)閉服務(wù)的文章就介紹到這了,更多相關(guān)spring boot 關(guān)閉服務(wù) 內(nèi)容請搜索好吧啦網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持好吧啦網(wǎng)!
相關(guān)文章:
1. 詳解盒子端CSS動畫性能提升2. CSS hack用法案例詳解3. PHP字符串前后字符或空格刪除方法介紹4. 使用HttpClient消費(fèi)ASP.NET Web API服務(wù)案例5. Jsp+Servlet實(shí)現(xiàn)文件上傳下載 刪除上傳文件(三)6. JSP servlet實(shí)現(xiàn)文件上傳下載和刪除7. input submit、button和回車鍵提交數(shù)據(jù)詳解8. ASP.NET Core實(shí)現(xiàn)中間件的幾種方式9. ASP常用日期格式化函數(shù) FormatDate()10. 詳解瀏覽器的緩存機(jī)制
