做者:黄青石
https://www.cnblogs.com/huang...
在使用Springboot的时候,都要涉及到服务的中止和启动,当咱们中止服务的时候,不少时候你们都是kill -9 直接把程序进程杀掉,这样程序不会执行优雅的关闭。并且一些没有执行完的程序就会直接退出。html
咱们不少时候都须要安全的将服务中止,也就是把没有处理完的工做继续处理完成。好比中止一些依赖的服务,输出一些日志,发一些信号给其余的应用系统,这个在保证系统的高可用是很是有必要的。java
那么咱么就来看一下几种中止springboot的方法。web
第一种就是Springboot提供的actuator的功能,它能够执行shutdown, health, info等,默认状况下,actuator的shutdown是disable的,咱们须要打开它。首先引入acturator的maven依赖。面试
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency>
而后将shutdown节点打开,也将/actuator/shutdown暴露web访问也设置上,除了shutdown以外还有health, info的web访问都打开的话将management.endpoints.web.exposure.include=*就能够。spring
将以下配置设置到application.properties里边,设置一下服务的端口号为3333。后端
server.port=3333 management.endpoint.shutdown.enabled==shutdown
接下来,我们建立一个springboot工程,而后设置一个bean对象,配置上PreDestroy方法。安全
这样在中止的时候会打印语句。bean的整个生命周期分为建立、初始化、销毁,当最后关闭的时候会执行销毁操做。在销毁的方法中执行一条输出日志。springboot
/** * @author huangqingshi * @Date 2019-08-17 */ public class TerminateBean { @PreDestroy public void preDestroy() { System.out.println("TerminalBean is destroyed"); } }
作一个configuration,而后提供一个获取bean的方法,这样该bean对象会被初始化。微信
/** * @author huangqingshi * @Date 2019-08-17 */ @Configurationpublic class ShutDownConfig { @Bean public TerminateBean getTerminateBean(){ return new TerminateBean(); } }
在启动类里边输出一个启动日志,当工程启动的时候,会看到启动的输出,接下来我们执行中止命令。多线程
curl -X POST http://localhost:3333/actuator/shutdown
如下日志能够输出启动时的日志打印和中止时的日志打印,同时程序已经中止。是否是比较神奇。
第二种方法也比较简单,获取程序启动时候的context,而后关闭主程序启动时的context。这样程序在关闭的时候也会调用PreDestroy注解。以下方法在程序启动十秒后进行关闭。
关注微信公众号:Java技术栈,在后台回复:boot,能够获取我整理的 N 篇最新Spring Boot 教程,都是干货。
/* method 2: use ctx.close to shutdown all application context */ ConfigurableApplicationContext ctx = SpringApplication.run(ShutdowndemoApplication.class, args); try { TimeUnit.SECONDS.sleep(10); } catch (InterruptedException e) { e.printStackTrace(); } ctx.close();
第三种方法,在springboot启动的时候将进程号写入一个app.pid文件,生成的路径是能够指定的,能够经过命令 cat /Users/huangqingshi/app.id | xargs kill 命令直接中止服务,这个时候bean对象的PreDestroy方法也会调用的。
这种方法你们使用的比较广泛。关注微信公众号:Java技术栈,在后台回复:boot,能够获取我整理的 N 篇最新Spring Boot 教程,都是干货。
写一个start.sh用于启动springboot程序,而后写一个中止程序将服务中止。
/* method 3 : generate a pid in a specified path, while usecommand to shutdown pid : 'cat /Users/huangqingshi/app.pid | xargs kill' */ SpringApplication application = new SpringApplication(ShutdowndemoApplication.class); application.addListeners(new ApplicationPidFileWriter("/Users/huangqingshi/app.pid")); application.run();
第四种方法,经过调用一个SpringApplication.exit()方法也能够退出程序,同时将生成一个退出码,这个退出码能够传递给全部的context。
这个就是一个JVM的钩子,经过调用这个方法的话会把全部PreDestroy的方法执行并中止,而且传递给具体的退出码给全部Context。经过调用System.exit(exitCode)能够将这个错误码也传给JVM。
程序执行完后最后会输出:Process finished with exit code 0,给JVM一个SIGNAL。
/* method 4: exit this application using static method */ ConfigurableApplicationContext ctx = SpringApplication.run(ShutdowndemoApplication.class, args); exitApplication(ctx); public static void exitApplication(ConfigurableApplicationContext context) { int exitCode = SpringApplication.exit(context, (ExitCodeGenerator) () -> 0); System.exit(exitCode); }
第五种方法,本身写一个Controller,而后将本身写好的Controller获取到程序的context,而后调用本身配置的Controller方法退出程序。经过调用本身写的/shutDownContext方法关闭程序:curl -X POST http://localhost:3333/shutDownContext。
/** * @author huangqingshi * @Date 2019-08-17 */ @RestControllerpublic class ShutDownController implements ApplicationContextAware { private ApplicationContext context; @PostMapping("/shutDownContext") public String shutDownContext() { ConfigurableApplicationContext ctx = (ConfigurableApplicationContext) context; ctx.close(); return "context is shutdown"; } @GetMapping("/") public String getIndex() { return "OK"; } @Override public void setApplicationContext(ApplicationContext applicationContext) throws BeansException { context = applicationContext; } }
好了,springboot的优雅关闭方法也都实现好了,也有同窗问,如何暴力中止呢,简单,直接kill -9 相应的PID便可。
以上这几种方法实现的话比较简单,可是真实工做中还须要考虑的点还不少,好比须要保护暴露的点不被别人利用,通常要加一些防火墙,或者只在内网使用,保证程序安全。
在真实的工做中的时候第三种比较经常使用,程序中通常使用内存队列或线程池的时候最好要优雅的关机,将内存队列没有处理的保存起来或线程池中没处理完的程序处理完。可是由于停机的时候比较快,因此停服务的时候最好不要处理大量的数据操做,这样会影响程序中止。
推荐去个人博客阅读更多:
2.Spring MVC、Spring Boot、Spring Cloud 系列教程
3.Maven、Git、Eclipse、Intellij IDEA 系列工具教程
生活很美好,明天见~