做网站模板的软件,做网站的业务员,做棋牌网站违法,专业的网络公司有哪些在使用 Spring Boot 的时候#xff0c;都要涉及到服务的停止和启动#xff0c;当我们停止服务的时候#xff0c;很多时候大家都是 kill -9 直接把程序进程杀掉#xff0c;这样程序不会执行优雅的关闭。而且一些没有执行完的程序就会直接退出。我们很多时候都需要安全的将服…在使用 Spring Boot 的时候都要涉及到服务的停止和启动当我们停止服务的时候很多时候大家都是 kill -9 直接把程序进程杀掉这样程序不会执行优雅的关闭。而且一些没有执行完的程序就会直接退出。我们很多时候都需要安全的将服务停止也就是把没有处理完的工作继续处理完成。比如停止一些依赖的服务输出一些日志发一些信号给其他的应用系统这个在保证系统的高可用是非常有必要的。那么咱么就来看一下几种停止 Spring Boot 的方法。第一种就是 Spring Boot 提供的 actuator 的功能它可以执行 shutdown, health, info 等默认情况下actuator 的 shutdown 是 disable 的我们需要打开它首先引入 acturator 的 maven 依赖。 dependency groupIdorg.springframework.bootgroupId artifactIdspring-boot-starter-actuatorartifactId dependency然后将 shutdown 节点打开也将 /actuator/shutdown 暴露 web 访问也设置上除了 shutdown 之外还有 health, info 的 web 访问都打开的话将 management.endpoints.web.exposure.include* 就可以。将如下配置设置到 application.properties 里边设置一下服务的端口号为 3333。server.port3333management.endpoint.shutdown.enabledtruemanagement.endpoints.web.exposure.includeshutdown接下来咱们创建一个 Spring Boot 工程然后设置一个 bean 对象配置上 PreDestroy 方法。这样在停止的时候会打印语句。bean 的整个生命周期分为创建、初始化、销毁当最后关闭的时候会执行销毁操作在销毁的方法中执行一条输出日志。package com.hqs.springboot.shutdowndemo.bean;import javax.annotation.PreDestroy;/** * author huangqingshi * Date 2019-08-17 */public class TerminateBean { PreDestroy public void preDestroy() { System.out.println(TerminalBean is destroyed); }}做一个 configuration然后提供一个获取 bean 的方法这样该 bean 对象会被初始化。package com.hqs.springboot.shutdowndemo.config;import com.hqs.springboot.shutdowndemo.bean.TerminateBean;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.Configuration;/** * 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 注解如下方法在程序启动十秒后进行关闭。/* 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();第三种方法在 Spring Boot 启动的时候将进程号写入一个 app.pid 文件生成的路径是可以指定的可以通过命令 cat /Users/huangqingshi/app.id | xargs kill 命令直接停止服务这个时候 bean 对象的 PreDestroy 方法也会调用的。这种方法大家使用的比较普遍。写一个 start.sh 用于启动 Spring Boot 程序然后写一个停止程序将服务停止。/* method 3 : generate a pid in a specified path, while use command 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。package com.hqs.springboot.shutdowndemo.controller;import org.springframework.beans.BeansException;import org.springframework.context.ApplicationContext;import org.springframework.context.ApplicationContextAware;import org.springframework.context.ConfigurableApplicationContext;import org.springframework.web.bind.annotation.GetMapping;import org.springframework.web.bind.annotation.PostMapping;import org.springframework.web.bind.annotation.RestController;/** * 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; }}好了Spring Boot 的优雅关闭方法也都实现好了也有同学问如何暴力停止呢简单直接 kill -9 相应的 PID 即可。总结以上这几种方法实现的话比较简单但是真实工作中还需要考虑的点还很多比如需要保护暴露的点不被别人利用一般要加一些防火墙或者只在内网使用保证程序安全。在真实的工作中的时候第三种比较常用程序中一般使用内存队列或线程池的时候最好要优雅的关机将内存队列没有处理的保存起来或线程池中没处理完的程序处理完。但是因为停机的时候比较快所以停服务的时候最好不要处理大量的数据操作这样会影响程序停止。所谓技多不压身我们所读过的每一本书所学过的每一门语言在未来指不定都能给我们意想不到的回馈呢。其实做为一个开发者有一个学习的氛围跟一个交流圈子特别重要这里我推荐一个Java学习交流群私信小编回复JAVA获得进群资料不管你是小白还是大牛欢迎入驻大家一起交流成长。