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”,指的是非关系型的数据库. 非关系型数据库主要有这些特点:非关系型的.分布式的.开源的.水平可扩展的. 原始的目的是为 ...
随机推荐
- 001_Chrome 76支持原生HTML 图片懒加载Lazy loading
Table Of Content 什么是懒加载? 语法参数及使用方式? 有哪些特点? 与js有关的实践 什么是懒加载? 技术背景 Web应用需要经常向后台服务器请求资源(通过查询数据库,是非常耗时耗资 ...
- springboot项目下的Caused by: org.apache.ibatis.binding.BindingException: Invalid bound statement (not found):
今天遇到mybatis-puls的报错Caused by: org.apache.ibatis.binding.BindingException: Invalid bound statement (n ...
- javascript入门 之 zTree(十四 增删查改)(二)
<!DOCTYPE html> <HTML> <HEAD> <TITLE> ZTREE DEMO - addNodes / editName / rem ...
- Quil-delta-enhanced 简介
Quill 是一款时下非常热门的富文本编辑器,它拥有非常强大的扩展能力,可以让开发者根据自己的需要编写插件,使编辑器支持的内容类型更加丰富.它之所以能够拥有这么强大的扩展能力,一方面是因为它的架构和 ...
- spark rdd元素println
1.spark api主要分两种:转换操作和行动操作.如果在转化操作中println spark打印了 我也看不到. val result = sqlContext.sql(sql) val resu ...
- pgsql中json格式数组查询结果变成了字符串
场景复原 最近使用到了json的数组,用来存储多个文件的值,发现在连表查询的时候返回结果变成了字符串. { "id": "repl-placeholder-007&quo ...
- 线程池:Execution框架
每问题每线程:在于它没有对已创建线程的数量进行任何限制,除非对客户端能够抛出的请求速率进行限制. 下边 有些图片看不到,清看原地址:http://www.360doc.com/content/10/1 ...
- 数组的增加与删除(push、pop、unshift、shift)
1. 数组增删和换位置(原数组将被修改) push() //在数组最后面插入项,返回数组的长度 数组1改后的长度 = 数组1.push(元素1); pop() //取出数组中的最后一 ...
- 2019-06-02 Python之微信好友数据分析以及运用Pyecharts可视化
一.库的使用说明 pass 二.微信好友信息的获取 def get_friends_info(self): #获取好像信息,返回lis列表 bot = Bot() lis = [['name', 'r ...
- 2019-05-19 Python之第一个爬虫和测试
一.使用request和get访问某个网页20次并且打印返回状态,内容 扩展:常见状态码含义 200 - 服务器成功返回网页,404 - 请求的网页不存在,403(禁止)服务器拒绝请求,404(未 ...