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)聽
@Controller
public static class ShutdownAction implements ApplicationListener<ContextClosedEvent> {
@Override
public 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)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
搭建SpringBoot項(xiàng)目三種方式(圖文教程)
Springboot作為當(dāng)下最主流的java開發(fā)框架,已成為IT從業(yè)人員的入門必備技能,本文主要介紹了搭建SpringBoot項(xiàng)目三種方式,感興趣的可以了解一下2023-09-09
Springboot詳解整合SpringSecurity實(shí)現(xiàn)全過程
Spring Security基于Spring開發(fā),項(xiàng)目中如果使用Springboot作為基礎(chǔ),配合Spring Security做權(quán)限更加方便,而Shiro需要和Spring進(jìn)行整合開發(fā)。因此作為spring全家桶中的Spring Security在java領(lǐng)域很常用2022-07-07
基于Retrofit+Rxjava實(shí)現(xiàn)帶進(jìn)度顯示的下載文件
這篇文章主要為大家詳細(xì)介紹了基于Retrofit+Rxjava實(shí)現(xiàn)帶進(jìn)度顯示的下載文件,具有一定的參考價值,感興趣的小伙伴們可以參考一下2018-05-05
Spring Boot 員工管理系統(tǒng)超詳細(xì)教程(源碼分享)
這篇文章主要介紹了Spring Boot 員工管理系統(tǒng)超詳細(xì)教程(源碼分享),本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2021-05-05
Java線程重復(fù)執(zhí)行以及操作共享變量的代碼示例
這篇文章主要介紹了Java中對線程重復(fù)執(zhí)行以及操作共享變量的代碼示例,來自于Java面試題目的練習(xí)整理,需要的朋友可以參考下2015-12-12

