詳解Springboot 優(yōu)雅停止服務(wù)的幾種方法
在使用Springboot的時(shí)候,都要涉及到服務(wù)的停止和啟動(dòng),當(dāng)我們停止服務(wù)的時(shí)候,很多時(shí)候大家都是kill -9 直接把程序進(jìn)程殺掉,這樣程序不會(huì)執(zhí)行優(yōu)雅的關(guān)閉。而且一些沒有執(zhí)行完的程序就會(huì)直接退出。
我們很多時(shí)候都需要安全的將服務(wù)停止,也就是把沒有處理完的工作繼續(xù)處理完成。比如停止一些依賴的服務(wù),輸出一些日志,發(fā)一些信號(hào)給其他的應(yīng)用系統(tǒng),這個(gè)在保證系統(tǒng)的高可用是非常有必要的。那么咱么就來(lái)看一下幾種停止springboot的方法。
第一種就是Springboot提供的actuator的功能,它可以執(zhí)行shutdown, health, info等,默認(rèn)情況下,actuator的shutdown是disable的,我們需要打開它。首先引入acturator的maven依賴。
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency>
然后將shutdown節(jié)點(diǎn)打開,也將/actuator/shutdown暴露web訪問也設(shè)置上,除了shutdown之外還有health, info的web訪問都打開的話將management.endpoints.web.exposure.include=*就可以。將如下配置設(shè)置到application.properties里邊。設(shè)置一下服務(wù)的端口號(hào)為3333。
server.port=3333management.endpoint.shutdown.enabled=true management.endpoints.web.exposure.include=shutdown
接下來(lái),咱們創(chuàng)建一個(gè)springboot工程,然后設(shè)置一個(gè)bean對(duì)象,配置上PreDestroy方法。這樣在停止的時(shí)候會(huì)打印語(yǔ)句。bean的整個(gè)生命周期分為創(chuàng)建、初始化、銷毀,當(dāng)最后關(guān)閉的時(shí)候會(huì)執(zhí)行銷毀操作。在銷毀的方法中執(zhí)行一條輸出日志。
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"); } }
做一個(gè)configuration,然后提供一個(gè)獲取bean的方法,這樣該bean對(duì)象會(huì)被初始化。
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 */ @Configuration public class ShutDownConfig { @Bean public TerminateBean getTerminateBean() { return new TerminateBean(); } }
在啟動(dòng)類里邊輸出一個(gè)啟動(dòng)日志,當(dāng)工程啟動(dòng)的時(shí)候,會(huì)看到啟動(dòng)的輸出,接下來(lái)咱們執(zhí)行停止命令。
、
curl -X POST http://localhost:3333/actuator/shutdown
以下日志可以輸出啟動(dòng)時(shí)的日志打印和停止時(shí)的日志打印,同時(shí)程序已經(jīng)停止。是不是比較神奇。
第二種方法也比較簡(jiǎn)單,獲取程序啟動(dòng)時(shí)候的context,然后關(guān)閉主程序啟動(dòng)時(shí)的context。這樣程序在關(guān)閉的時(shí)候也會(huì)調(diào)用PreDestroy注解。如下方法在程序啟動(dòng)十秒后進(jìn)行關(guān)閉。
/* 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啟動(dòng)的時(shí)候?qū)⑦M(jìn)程號(hào)寫入一個(gè)app.pid文件,生成的路徑是可以指定的,可以通過命令 cat /Users/huangqingshi/app.id | xargs kill 命令直接停止服務(wù),這個(gè)時(shí)候bean對(duì)象的PreDestroy方法也會(huì)調(diào)用的。這種方法大家使用的比較普遍。寫一個(gè)start.sh用于啟動(dòng)springboot程序,然后寫一個(gè)停止程序?qū)⒎?wù)停止?! ?/p>
/* 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();
第四種方法,通過調(diào)用一個(gè)SpringApplication.exit()方法也可以退出程序,同時(shí)將生成一個(gè)退出碼,這個(gè)退出碼可以傳遞給所有的context。這個(gè)就是一個(gè)JVM的鉤子,通過調(diào)用這個(gè)方法的話會(huì)把所有PreDestroy的方法執(zhí)行并停止,并且傳遞給具體的退出碼給所有Context。通過調(diào)用System.exit(exitCode)可以將這個(gè)錯(cuò)誤碼也傳給JVM。程序執(zhí)行完后最后會(huì)輸出:Process finished with exit code 0,給JVM一個(gè)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); }
第五種方法,自己寫一個(gè)Controller,然后將自己寫好的Controller獲取到程序的context,然后調(diào)用自己配置的Controller方法退出程序。通過調(diào)用自己寫的/shutDownContext方法關(guān)閉程序: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 */ @RestController public 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的優(yōu)雅關(guān)閉方法也都實(shí)現(xiàn)好了,也有同學(xué)問,如何暴力停止呢,簡(jiǎn)單,直接kill -9 相應(yīng)的PID即可。
總結(jié)一下:
以上這幾種方法實(shí)現(xiàn)的話比較簡(jiǎn)單,但是真實(shí)工作中還需要考慮的點(diǎn)還很多,比如需要保護(hù)暴露的點(diǎn)不被別人利用,一般要加一些防火墻,或者只在內(nèi)網(wǎng)使用,保證程序安全。
在真實(shí)的工作中的時(shí)候第三種比較常用,程序中一般使用內(nèi)存隊(duì)列或線程池的時(shí)候最好要優(yōu)雅的關(guān)機(jī),將內(nèi)存隊(duì)列沒有處理的保存起來(lái)或線程池中沒處理完的程序處理完。但是因?yàn)橥C(jī)的時(shí)候比較快,所以停服務(wù)的時(shí)候最好不要處理大量的數(shù)據(jù)操作,這樣會(huì)影響程序停止。
好了,大家覺得還沒看全的話,可以訪問我的GIT代碼:https://github.com/stonehqs/shutdowndemo.git。
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- springboot部署linux訪問服務(wù)器資源的方法
- spring boot使用WebClient調(diào)用HTTP服務(wù)代碼示例
- springboot以FTP方式上傳文件到遠(yuǎn)程服務(wù)器
- Spring boot2X Consul如何使用Feign實(shí)現(xiàn)服務(wù)調(diào)用
- Spring boot2X Consul如何通過RestTemplate實(shí)現(xiàn)服務(wù)調(diào)用
- spring boot搭建文件服務(wù)器解決同時(shí)上傳多個(gè)圖片和下載的問題
- spring boot配置多個(gè)請(qǐng)求服務(wù)代理的完整步驟
- SpringBoot+SpringCloud用戶信息微服務(wù)傳遞實(shí)現(xiàn)解析
- SpringBoot+Eureka實(shí)現(xiàn)微服務(wù)負(fù)載均衡的示例代碼
- SpringBoot服務(wù)端數(shù)據(jù)校驗(yàn)過程詳解
相關(guān)文章
Java如何設(shè)置系統(tǒng)參數(shù)和運(yùn)行參數(shù)
這篇文章主要介紹了Java如何設(shè)置系統(tǒng)參數(shù)和運(yùn)行參數(shù),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-04-04通過FeignClient調(diào)用微服務(wù)提供的分頁(yè)對(duì)象IPage報(bào)錯(cuò)的解決
這篇文章主要介紹了通過FeignClient調(diào)用微服務(wù)提供的分頁(yè)對(duì)象IPage報(bào)錯(cuò)的解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-03-03Java將json字符串轉(zhuǎn)換為數(shù)組的幾種方法
在Java開發(fā)中,經(jīng)常會(huì)遇到將json字符串轉(zhuǎn)換為數(shù)組的需求,本文主要介紹了Java將json字符串轉(zhuǎn)換為數(shù)組的幾種方法,具有一定的參考價(jià)值,感興趣的可以了解一下2024-01-01詳解json在SpringBoot中的格式轉(zhuǎn)換
這篇文章主要介紹了詳解json在SpringBoot中的格式轉(zhuǎn)換,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-11-11Spring中11個(gè)最常用的擴(kuò)展點(diǎn)總結(jié),你知道幾個(gè)
我們知道IOC(控制反轉(zhuǎn))和AOP(面向切面編程)是spring的基石,除此之外spring的擴(kuò)展能力非常強(qiáng),下面這篇文章主要給大家介紹了關(guān)于Spring中11個(gè)最常用的擴(kuò)展點(diǎn)的相關(guān)資料,需要的朋友可以參考下2022-12-12IDEA2020.2.3 "reading maven projects"卡住的問題
這篇文章主要介紹了IDEA2020.2.3 "reading maven projects"卡住的問題及問題原因探究,通過多種方法給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友參考下吧2020-10-10Spring Boot簡(jiǎn)介與快速搭建詳細(xì)步驟
SpringBoot其本身沒有添加什么新的技術(shù),就是整合了一些現(xiàn)有的框架,并提供了一些默認(rèn)的配置,就是這些默認(rèn)的配置,極大的提高了我們的開發(fā)效率。這篇文章主要介紹了Spring Boot簡(jiǎn)介與快速搭建,需要的朋友可以參考下2021-05-05