专栏名称: 朱小厮的博客
著有畅销书:《深入理解Kafka》和《RabbitMQ实战指南》。公众号主要用来分享Java技术栈、Golang技术栈、消息中间件(如Kafka、RabbitMQ)、存储、大数据以及通用型技术架构等相关的技术。
目录
相关文章推荐
GiantPandaCV  ·  《超大规模操作手册:在 GPU 集群上训练 ... ·  3 天前  
51好读  ›  专栏  ›  朱小厮的博客

优雅停止 SpringBoot 服务,拒绝 kill -9 暴力停止!

朱小厮的博客  · 公众号  ·  · 2020-01-10 08:51

正文

点击上方“朱小厮的博客”,选择“ 设为星标”

后台回复” 1024 “获取公众号专属1024GB资料


来源:rrd.me/fBssC


在使用 SpringBoot 的时候,都要涉及到服务的停止和启动,当我们停止服务的时候,很多时候大家都是kill -9 直接把程序进程杀掉,这样程序不会执行优雅的关闭。而且一些没有执行完的程序就会直接退出。

我们很多时候都需要安全的将服务停止,也就是把没有处理完的工作继续处理完成。比如停止一些依赖的服务,输出一些日志,发一些信号给其他的应用系统,这个在保证系统的高可用是非常有必要的。那么咱么就来看一下几种停止 SpringBoot 的方法。

第一种就是 SpringBoot 提供的actuator的功能 ,它可以执行shutdown, health, info等,默认情况下,actuator的shutdown是disable的,我们需要打开它。首先引入acturator的maven依赖。

  1. org.springframework.boot

  2. spring-boot-starter-actuator

然后将shutdown节点打开,也将/actuator/shutdown暴露web访问也设置上,除了shutdown之外还有health, info的web访问都打开的话将management.endpoints.web.exposure.include=*就可以。将如下配置设置到application.properties里边。设置一下服务的端口号为3333

  1. server.port=3333

  2. management.endpoint.shutdown.enabled=true

  3. management.endpoints.web.exposure.include=shutdown

接下来,咱们创建一个SpringBoot工程,然后设置一个bean对象,配置上PreDestroy方法。这样在停止的时候会打印语句。bean的整个生命周期分为创建、初始化、销毁,当最后关闭的时候会执行销毁操作。在销毁的方法中执行一条输出日志。

  1. package com.hqs.springboot.shutdowndemo.bean;


  2. import javax.annotation.PreDestroy;


  3. public class TerminateBean {


  4. @PreDestroy

  5. public void preDestroy() {

  6. System.out.println("TerminalBean is destroyed");

  7. }


  8. }

做一个configuration,然后提供一个获取bean的方法,这样该bean对象会被初始化。

  1. package com.hqs.springboot.shutdowndemo.config;


  2. import com.hqs.springboot.shutdowndemo.bean.TerminateBean;

  3. import org.springframework.context.annotation.Bean;

  4. import org.springframework.context.annotation.Configuration;


  5. @Configuration

  6. public class ShutDownConfig {


  7. @Bean

  8. public TerminateBean getTerminateBean() {

  9. return new TerminateBean();

  10. }


  11. }

在启动类里边输出一个启动日志,当工程启动的时候,会看到启动的输出,接下来咱们执行停止命令。

  1. curl -X POST http://localhost:3333/actuator/shutdown

以下日志可以输出启动时的日志打印和停止时的日志打印,同时程序已经停止。是不是比较神奇。

第二种方法也比较简单,获取程序启动时候的context,然后关闭主程序启动时的context 。这样程序在关闭的时候也会调用PreDestroy注解。如下方法在程序启动十秒后进行关闭。

  1. /* method 2: use ctx.close to shutdown all application context */

  2. ConfigurableApplicationContext ctx = SpringApplication.run(ShutdowndemoApplication.class, args);


  3. try {

  4. TimeUnit.SECONDS.sleep(10);


  5. } catch (InterruptedException e) {

  6. e.printStackTrace();

  7. }


  8. ctx.close();

第三种方法,在SpringBoot启动的时候将进程号写入一个app.pid文件,生成的路径是可以指定的 ,可以通过命令 cat /Users/huangqingshi/app.id | xargs kill 命令直接停止服务,这个时候bean对象的PreDestroy方法也会调用的。这种方法大家使用的比较普遍。写一个start.sh用于启动springboot程序,然后写一个停止程序将服务停止。

  1. /* method 3 : generate a pid in a specified path, while use command to shutdown pid :

  2. 'cat /Users/huangqingshi/app.pid | xargs kill' */

  3. SpringApplication application = new SpringApplication(ShutdowndemoApplication.class);

  4. application.addListeners(new ApplicationPidFileWriter("/Users/huangqingshi/app.pid"));

  5. application.run();

第四种方法,通过调用一个SpringApplication.exit()方法也可以退出程序 ,同时将生成一个退出码,这个退出码可以传递给所有的context。这个就是一个JVM的钩子,通过调用这个方法的话会把所有PreDestroy的方法执行并停止,并且传递给具体的退出码给所有Context。通过调用System.exit(exitCode)可以将这个错误码也传给JVM。程序执行完后最后会输出:Process finished with exit code 0,给JVM一个SIGNAL

  1. /* method 4: exit this application using static method */

  2. ConfigurableApplicationContext ctx = SpringApplication.run(ShutdowndemoApplication.class, args);

  3. exitApplication(ctx);

  1. public static void exitApplication(ConfigurableApplicationContext context) {

  2. int exitCode = SpringApplication.exit(context, (ExitCodeGenerator) () -> 0);


  3. System.exit(exitCode);

  4. }

第五种方法,自己写一个Controller,然后将自己写好的Controller获取到程序的context,然后调用自己配置的Controller方法退出程序 。通过调用自己写的/shutDownContext方法关闭程序:curl -X POST http://localhost:3333/shutDownContext。

  1. package com.hqs.springboot.shutdowndemo.controller;


  2. import org.springframework.beans.BeansException;

  3. import org.springframework.context.ApplicationContext;

  4. import org.springframework.context.ApplicationContextAware;

  5. import org.springframework.context.ConfigurableApplicationContext;

  6. import org.springframework.web.bind.annotation.GetMapping;

  7. import org.springframework.web.bind.annotation.PostMapping;

  8. import org.springframework.web.bind.annotation.







请到「今天看啥」查看全文