spring boot 如何优雅关闭服务
#代码知识 发布时间: 2026-01-12
spring boot 优雅的关闭服务

实现ContextClosedEvent 监听器,监听到关闭事件后,关闭springboot进程
**
网上有很多例子 使用spring boot 插件做关闭经测试此插件只能是关闭spring boot服务,不能杀死服务进程。还是需要实现关闭监听,去杀死进程。
网上有很多例子 使用spring boot 插件做关闭经测试此插件只能是关闭spring boot服务,不能杀死服务进程。还是需要实现关闭监听,去杀死进程。
网上有很多例子 使用spring boot 插件做关闭经测试此插件只能是关闭spring boot服务,不能杀死服务进程。还是需要实现关闭监听,去杀死进程。
重要的事说三遍
**
actuator 关闭spring boot 实现方式
引入actuator 配置 shutdown
调用http://127.0.0.1/xxx/
引入actuator <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency>-->
配置
在application.properties中开启关闭
management.endpoints.web.exposure.include=*
#management.endpoint.shutdown.enabled = true
1.调用
1.主入口
public static ConfigurableApplicationContext configurableApplicationContext;
public static void main(String[] args) throws InterruptedException {
configurableApplicationContext = SpringApplication.run(GatewayApplication.class, args);
}
2.关闭监听
@Controller
public static class ShutdownAction implements ApplicationListener<ContextClosedEvent> {
@Override
public void onApplicationEvent(ContextClosedEvent event) {
System.exit(SpringApplication.exit(configurableApplicationContext));
}
}
3.关闭服务命令
/**
* 关闭服务
*/
@RequestMapping(value = "/stop", method = RequestMethod.GET)
@ResponseBody
public void stopServer() {
MySpringApplication.configurableApplicationContext.close();
} 代码知识SEO
上一篇 : Android 边播边缓存的实现(MP4 未加密m3u8)
下一篇 : 详解Vue中的watch和computed
-
SEO外包最佳选择国内专业的白帽SEO机构,熟知搜索算法,各行业企业站优化策略!
SEO公司
-
可定制SEO优化套餐基于整站优化与品牌搜索展现,定制个性化营销推广方案!
SEO套餐
-
SEO入门教程多年积累SEO实战案例,从新手到专家,从入门到精通,海量的SEO学习资料!
SEO教程
-
SEO项目资源高质量SEO项目资源,稀缺性外链,优质文案代写,老域名提权,云主机相关配置折扣!
SEO资源
-
SEO快速建站快速搭建符合搜索引擎友好的企业网站,协助备案,域名选择,服务器配置等相关服务!
SEO建站
-
快速搜索引擎优化建议没有任何SEO机构,可以承诺搜索引擎排名的具体位置,如果有,那么请您多注意!专业的SEO机构,一般情况下只能确保目标关键词进入到首页或者前几页,如果您有相关问题,欢迎咨询!