Shutdown SpringBoot App
2023-04-18 13:08:48 时间
Shutdown SpringBoot App
Spring Boot使用ApplicationContext来创建,初始化和销毁所用的bean。本文将会讲解如何shut down一个spring boot应用程序。
Shutdown Endpoint
Spring Boot actuator自带了shutdown的endpoint。首先我们添加pom依赖:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
接下来我们需要开启shutdown的配置:
management.endpoints.web.exposure.include=*
management.endpoint.shutdown.enabled=true
上面的配置对外暴露了 /shutdown 接口。我们可以直接这样调用:
curl -X POST localhost:8080/actuator/shutdown
close Application Context
我们也可以直接调用Application Context的close() 方法来关闭Application Context。
@SpringBootApplication
public class ConfigurableApp {
public static void main(String[] args) {
ConfigurableApplicationContext ctx = new
SpringApplicationBuilder(ConfigurableApp.class).web(WebApplicationType.NONE).run();
System.out.println("Spring Boot application started");
ctx.getBean(TerminateBean.class);
ctx.close();
}
}
为了验证App是否被关闭,我们可以在TerminateBean中添加@PreDestroy来监测App是否被关闭:
@Component
public class TerminateBean {
@PreDestroy
public void onDestroy() throws Exception {
System.out.println("Spring Container is destroyed!");
}
}
这是程序的输出:
2020-02-03 23:12:08.583 INFO 30527 --- [ main] com.flydean.ConfigurableApp : Started ConfigurableApp in 2.922 seconds (JVM running for 3.559)
Spring Boot application started
Spring Container is destroyed!
还有一种办法就是暴露close接口如下所示:
@RestController
public class ShutdownController implements ApplicationContextAware {
private ApplicationContext context;
@PostMapping("/shutdownContext")
public void shutdownContext() {
((ConfigurableApplicationContext) context).close();
}
@Override
public void setApplicationContext(ApplicationContext ctx) throws BeansException {
this.context = ctx;
}
}
这样我们就可以通过/shutdownContext接口来关闭ApplicationContext。
退出SpringApplication
上篇文章我们讲过可以通过实现ExitCodeGenerator 接口来返回特定的exit code:
@SpringBootApplication
public class ExitCodeApp implements ExitCodeGenerator {
public static void main(String[] args) {
System.exit(SpringApplication.exit(SpringApplication.run(ExitCodeApp.class, args)));
}
@Override
public int getExitCode() {
return 11;
}
}
从外部程序kill App
熟悉shell的同学都知道如果想在外部kill一个程序,需要知道该App的pid,Spring Boot也可以很方便的生成pid:
@SpringBootApplication
public class KillApp {
public static void main(String[] args) {
SpringApplicationBuilder app = new SpringApplicationBuilder(KillApp.class)
.web(WebApplicationType.NONE);
app.build().addListeners(new ApplicationPidFileWriter("./bin/shutdown.pid"));
app.run();
}
}
上面的程序将会在./bin/shutdown.pid生成应用程序的pid,供shell使用。
我们可以这样使用:
kill $(cat ./bin/shutdown.pid)
本文的例子可以参考 https://github.com/ddean2009/learn-springboot2/tree/master/springboot-shutdown
更多教程请参考 flydean的博客
相关文章
- 编程生涯中遇到超厉害的三个技术大牛
- 千万不要和一种编程语言厮守终生
- 概念版macOS可以用iOS应用了,苹果会把两大系统融合吗?
- 美团点评开源用 Vue.js 开发小程序的前端框架 mpvue
- 谷歌发布Android P首个预览版:专门优化异形全面屏
- 2018年2月iOS面试总结
- 程序员的10个老大难问题
- iOS开发中的11种锁以及性能对比
- 彻底理解 Android MD 设计里的 “阴影”
- 这届iOS到底是怎么了?
- 密码成为被淘汰的落后技术 微软将支持用手机解锁电脑
- 安卓GO系统发展缓慢:目前仅十余个App可用
- 揭秘12306团队:为一张票"抢"一年 与机器抢票对战
- 安卓7.0跃居份额第一:你升级了吗?
- 提升iOS开发效率的一些keys
- 旅行青蛙Unity游戏逆向修改--iOS篇
- iOS架构师的必经之路
- 推荐提升iOS开发效率的一些keys
- 谷歌自主开发系统Fuchsia OS曝光:流畅度爆表
- 安卓9.0第一版完工!3月圆周率日推送:干掉碎片化