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的更多相关文章

  1. 整合springboot(app后台框架搭建四)

    springboot可以说是为了适用SOA服务出现,一方面,极大的简便了配置,加速了开发速度:第二方面,也是一个嵌入式的web服务,通过jar包运行就是一个web服务: 还有提供了很多metric,i ...

  2. SpringBoot生命周期管理之停掉应用服务几种方法

    前言 在生产环境下管理Spring Boot应用的生命周期非常重要.Spring容器通过ApplicationContext处理应用服务的所有的beans的创建.初始化.销毁. 本文着重于生命周期中的 ...

  3. springboot入门_helloworld

    开始学习springboot,在此做记录,有不正确之处,还望读者指正. springboot框架的设计目的是用来简化新Spring应用的初始环境搭建以及开发过程.主要体现有:1 xml配置文件,使用s ...

  4. 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 ...

  5. SpringBoot微服务架构下的MVC模型总结

    SpringBoot微服务架构下的MVC模型产生的原因: 微服务概念改变着软件开发领域,传统的开源框架结构开发,由于其繁琐的配置流程 , 复杂的设置行为,为项目的开发增加了繁重的工作量,微服务致力于解 ...

  6. springboot学习入门简易版二---springboot2.0项目创建

    2 springboot项目创建(5) 环境要求:jdk1.8+ 项目结构: 2.1创建maven工程 Group id :com.springbootdemo Artifact id: spring ...

  7. springBoot整合spring、springMVC、mybatis

    前文 1.为什么使用springBoot 众所周知,spring是Java在搭建后台时非常实用的框架,其整合了市场上几乎所有的主流框架于一体,使后端编程更加高效.快速: 而SpringBoot更是把s ...

  8. DOCKER 学习笔记5 Springboot+nginx+mysql 容器编排

    前言 在上节的内容中,我们已经通过一个简单的实例,将Docker-compose 进行了实际的应用.这一小节中.我们将通过学习和了解,着重认识容器的编排,上一节只算是一个小小的测试.在这一节中.我们将 ...

  9. MongoDB【第一篇】MongodDB初识

    NoSQL介绍 一.NoSQL简介 NoSQL,全称是”Not Only Sql”,指的是非关系型的数据库. 非关系型数据库主要有这些特点:非关系型的.分布式的.开源的.水平可扩展的. 原始的目的是为 ...

随机推荐

  1. linux下zip/unzip详解

    linux下zip_unzip详解 命令列表:zip    -q (quiet)    -r (recursive)    -0(level0-level9)    -e (encrypt)    - ...

  2. 自执行函数-[javascript]-[语法]

    在看别人的代码的时候,遇到了一种写法,之前没有见过,如下: ![](https://img2018.cnblogs.com/blog/1735896/201912/1735896-2019122114 ...

  3. 用Fiddler抓取手机APP数据包

    Fiddler下载地址 1.允许远程连接 2.允许监听https 3.重启Fiddler 这步很重要,不要忘了 4.手机配置 用ipconfig命令查询当前PC的局域网IP 将手机连接上同一个WIFI ...

  4. 1011 World Cup Betting (20 分)

    With the 2010 FIFA World Cup running, football fans the world over were becoming increasingly excite ...

  5. 构建LNMP

                                                                             构建LNMP 案例1:部署LNMP环境 案例2:构建L ...

  6. 自定义yum仓库

                                             自定义yum仓库 案例4:自定义yum软件仓库 4.1问题 本例要求在CentOS真机上利用RHEL7的光盘镜像文件准 ...

  7. python3(九) Section

    # list或tuple的部分元素 L = ['Michael', 'Sarah', 'Tracy', 'Bob', 'Jack'] # -----------------传统方法 print([L[ ...

  8. leetcode Perform String Shifts

    Perform String Shifts You are given a string s containing lowercase English letters, and a matrix sh ...

  9. Multiple Books多账薄

    有些公司因管理需要配置多本账薄,比如管理帐和PRC,那么在Epicor 10中如何实现呢? 1创建 new Book: 2 created a map: Financial Management -& ...

  10. L1线性回归

    线性回归 主要内容包括: 线性回归的基本要素 线性回归模型从零开始的实现 线性回归模型使用pytorch的简洁实现 代码下载地址 https://download.csdn.net/download/ ...