当前位置:   article > 正文

SpringBoot 优雅停止服务的几种方法_/actuator/shutdown

/actuator/shutdown

方法一

  • Springboot提供的actuator的功能,它可以执行shutdown, health, info
  1. <dependency>
  2. <groupId>org.springframework.boot</groupId>
  3. <artifactId>spring-boot-starter-actuator</artifactId>
  4. </dependency>
  1. # 设置服务的端口号为3300
  2. server.port=3300
  3. management.endpoint.shutdown.enabled=true
  4. management.endpoints.web.exposure.include=shutdown

shutdown节点打开,将/actuator/shutdown暴露web访问也设置上
除了shutdown之外还有health, info的web访问都打开的话将
配置management.endpoints.web.exposure.include=*即可。

  • 关闭时执行销毁操作
  1. import javax.annotation.PreDestroy;
  2. public class TerminateBean {
  3. // 关闭时执行销毁操作
  4. @PreDestroy
  5. public void destroy() {
  6. System.out.println("TerminalBean is destroyed");
  7. }
  8. }
  • 自定义Config注册bean
  1. import com.hqs.springboot.shutdowndemo.bean.TerminateBean;
  2. import org.springframework.context.annotation.Bean;
  3. import org.springframework.context.annotation.Configuration;
  4. @Configuration
  5. public class ShutDownConfig {
  6. @Bean
  7. public TerminateBean getTerminateBean() {
  8. return new TerminateBean();
  9. }
  10. }

在启动类里输出启动日志

curl -X POST http://localhost:3300/actuator/shutdown

日志可以输出启动时的日志和停止时的日志

方法二

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

  1. /*
  2. * method 2: use ctx.close to shutdown all application context
  3. */
  4. ConfigurableApplicationContext ctx = SpringApplication.run(ShutdowndemoApplication.class, args);
  5. try {
  6. TimeUnit.SECONDS.sleep(10);
  7. } catch (InterruptedException e) {
  8. e.printStackTrace();
  9. }
  10. ctx.close();

方法三

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

  1. /*
  2. * method 3 : generate a pid in a specified path, while use command to shutdown pid :
  3. 'cat /Users/huangqingshi/app.pid | xargs kill'
  4. */
  5. SpringApplication application = new SpringApplication(ShutdowndemoApplication.class);
  6. application.addListeners(new ApplicationPidFileWriter("/Users/huangqingshi/app.pid"));
  7. application.run();

方法四

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

  1. /*
  2. * method 4: exit this application using static method
  3. */
  4. ConfigurableApplicationContext ctx = SpringApplication.run(ShutdowndemoApplication.class, args);
  5. exitApplication(ctx);
  6. public static void exitApplication(ConfigurableApplicationContext context) {
  7. int exitCode = SpringApplication.exit(context, (ExitCodeGenerator) () -> 0);
  8. System.exit(exitCode);
  9. }

方法五

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

  1. import org.springframework.beans.BeansException;
  2. import org.springframework.context.ApplicationContext;
  3. import org.springframework.context.ApplicationContextAware;
  4. import org.springframework.context.ConfigurableApplicationContext;
  5. import org.springframework.web.bind.annotation.GetMapping;
  6. import org.springframework.web.bind.annotation.PostMapping;
  7. import org.springframework.web.bind.annotation.RestController;
  8. @RestController
  9. public class ShutDownController implements ApplicationContextAware {
  10. private ApplicationContext context;
  11. @PostMapping("/shutDownContext")
  12. public String shutDownContext() {
  13. ConfigurableApplicationContext ctx = (ConfigurableApplicationContext) context;
  14. ctx.close();
  15. return "context is shutdown";
  16. }
  17. @GetMapping("/")
  18. public String getIndex() {
  19. return "OK";
  20. }
  21. @Override
  22. public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
  23. context = applicationContext;
  24. }
  25. }

如何暴力停止呢,简单,直接kill -9 相应的PID即可。

真实工作中还需要考虑的点还很多,比如需要保护暴露的点不被别人利用,一般要加一些防火墙,或者只在内网使用,保证程序安全。

在真实的工作中的时候第三种比较常用,程序中一般使用内存队列线程池的时候最好要优雅的关机,将内存队列没有处理的保存起来或线程池中没处理完的程序处理完。但是因为停机的时候比较快,所以停服务的时候最好不要处理大量的数据操作,这样会影响程序停止。

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/你好赵伟/article/detail/334184
推荐阅读
相关标签
  

闽ICP备14008679号