spring boot 如何优雅关闭服务
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();
}
上一篇:Idea工具中创建 SpringBoot工程及入门详解
栏 目:JAVA代码
本文标题:spring boot 如何优雅关闭服务
本文地址:http://www.codeinn.net/misctech/125730.html


阅读排行
- 1Java Swing组件BoxLayout布局用法示例
- 2java中-jar 与nohup的对比
- 3Java邮件发送程序(可以同时发给多个地址、可以带附件)
- 4Caused by: java.lang.ClassNotFoundException: org.objectweb.asm.Type异常
- 5Java中自定义异常详解及实例代码
- 6深入理解Java中的克隆
- 7java读取excel文件的两种方法
- 8解析SpringSecurity+JWT认证流程实现
- 9spring boot里增加表单验证hibernate-validator并在freemarker模板里显示错误信息(推荐)
- 10深入解析java虚拟机




