Shutdown SpringBoot App
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的博客
Shutdown SpringBoot App的更多相关文章
- 整合springboot(app后台框架搭建四)
springboot可以说是为了适用SOA服务出现,一方面,极大的简便了配置,加速了开发速度:第二方面,也是一个嵌入式的web服务,通过jar包运行就是一个web服务: 还有提供了很多metric,i ...
- SpringBoot生命周期管理之停掉应用服务几种方法
前言 在生产环境下管理Spring Boot应用的生命周期非常重要.Spring容器通过ApplicationContext处理应用服务的所有的beans的创建.初始化.销毁. 本文着重于生命周期中的 ...
- springboot入门_helloworld
开始学习springboot,在此做记录,有不正确之处,还望读者指正. springboot框架的设计目的是用来简化新Spring应用的初始环境搭建以及开发过程.主要体现有:1 xml配置文件,使用s ...
- spring Boot异步操作报错误: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'com.self.spring.springboot.Jeep' available
我也是最近开始学习Spring Boot,在执行异步操作的时候总是汇报如下的错误: Exception in thread "main" org.springframework.b ...
- SpringBoot微服务架构下的MVC模型总结
SpringBoot微服务架构下的MVC模型产生的原因: 微服务概念改变着软件开发领域,传统的开源框架结构开发,由于其繁琐的配置流程 , 复杂的设置行为,为项目的开发增加了繁重的工作量,微服务致力于解 ...
- springboot学习入门简易版二---springboot2.0项目创建
2 springboot项目创建(5) 环境要求:jdk1.8+ 项目结构: 2.1创建maven工程 Group id :com.springbootdemo Artifact id: spring ...
- springBoot整合spring、springMVC、mybatis
前文 1.为什么使用springBoot 众所周知,spring是Java在搭建后台时非常实用的框架,其整合了市场上几乎所有的主流框架于一体,使后端编程更加高效.快速: 而SpringBoot更是把s ...
- DOCKER 学习笔记5 Springboot+nginx+mysql 容器编排
前言 在上节的内容中,我们已经通过一个简单的实例,将Docker-compose 进行了实际的应用.这一小节中.我们将通过学习和了解,着重认识容器的编排,上一节只算是一个小小的测试.在这一节中.我们将 ...
- MongoDB【第一篇】MongodDB初识
NoSQL介绍 一.NoSQL简介 NoSQL,全称是”Not Only Sql”,指的是非关系型的数据库. 非关系型数据库主要有这些特点:非关系型的.分布式的.开源的.水平可扩展的. 原始的目的是为 ...
随机推荐
- A 大地魂力
时间限制 : - MS 空间限制 : - KB 评测说明 : 1s,256m 问题描述 奶牛贝西认为,要改变世界,就必须吸收大地的力量,贝西把大地的力量称为魂力.要吸取大地的魂力就需要在地上开出 ...
- NKOJ3775 数列操作
问题描述 给定一个长度为n的序列,你有一次机会选中一段连续的长度不超过d的区间,将里面所有数字全部修改为0.请找到最长的一段连续区间,使得该区间内所有数字之和不超过p. 输入格式 第一行包含三个整数n ...
- Java String与char
1. char类型 + char 类型 = 字符对应的ASCII码值相加(数字): char类型 + String 类型 = 字符对应的ASCII码值相加(数字) + String 类型: Strin ...
- PTA数据结构与算法题目集(中文) 7-5
PTA数据结构与算法题目集(中文) 7-5 堆中的路径 7-5 堆中的路径 (25 分) 将一系列给定数字插入一个初始为空的小顶堆H[].随后对任意给定的下标i,打印从H[i]到根结点的路径. ...
- 完整微信小程序授权登录页面教程
完整微信小程序授权登录页面教程 1.前言 微信官方对getUserInfo接口做了修改,授权窗口无法直接弹出,而取而代之是需要创建一个button,将其open-type属性绑定getUseInfo方 ...
- 面试官求你了,别再问我TCP的三次握手和四次挥手
少点代码,多点头发 本文已经收录至我的GitHub,欢迎大家踊跃star 和 issues. https://github.com/midou-tech/articles 三次握手建立链接,四次挥手断 ...
- GO中的channel使用小结
go关键字可以用来开启一个goroutine(协程))进行任务处理,而多个任务之间如果需要通信,就需要用到channel了. func testSimple(){ intChan := make(ch ...
- springIoc中的单列对象的分析
最近有个同事去面试,其中有一个问题是关于spring单例的.本篇博文就发表一下小编我自己的理解~~. 使用过spring的程序猿应该都知道,我们的bean(controller.service和Dao ...
- 21-Java-Hibernate框架(一)
一.Hibernate了解 Hibernate框架是Java持久层的框架,是Gavin King发明的,2001年发布的,JBoss公司的产品,2003年进入市场. Hibernate是基于对象来操作 ...
- A - Oil Deposits DFS
The GeoSurvComp geologic survey company is responsible for detecting underground oil deposits. GeoSu ...