日本综合一区二区|亚洲中文天堂综合|日韩欧美自拍一区|男女精品天堂一区|欧美自拍第6页亚洲成人精品一区|亚洲黄色天堂一区二区成人|超碰91偷拍第一页|日韩av夜夜嗨中文字幕|久久蜜综合视频官网|精美人妻一区二区三区

RELATEED CONSULTING
相關(guān)咨詢
選擇下列產(chǎn)品馬上在線溝通
服務時間:8:30-17:00
你可能遇到了下面的問題
關(guān)閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
Springboot優(yōu)雅停止服務的幾種方法

在使用Springboot的時候,都要涉及到服務的停止和啟動,當我們停止服務的時候,很多時候大家都是kill -9 直接把程序進程殺掉,這樣程序不會執(zhí)行優(yōu)雅的關(guān)閉。而且一些沒有執(zhí)行完的程序就會直接退出。

我們很多時候都需要安全的將服務停止,也就是把沒有處理完的工作繼續(xù)處理完成。比如停止一些依賴的服務,輸出一些日志,發(fā)一些信號給其他的應用系統(tǒng),這個在保證系統(tǒng)的高可用是非常有必要的。那么咱么就來看一下幾種停止springboot的方法。

第一種就是Springboot提供的actuator的功能,它可以執(zhí)行shutdown, health, info等,默認情況下,actuator的shutdown是disable的,我們需要打開它。首先引入acturator的maven依賴?!?/p>

 
 
 
 
  1.   
  2.        org.springframework.boot  
  3.        spring-boot-starter-actuator  
  4.     

然后將shutdown節(jié)點打開,也將/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 

接下來,咱們創(chuàng)建一個springboot工程,然后設置一個bean對象,配置上PreDestroy方法。這樣在停止的時候會打印語句。bean的整個生命周期分為創(chuàng)建、初始化、銷毀,當最后關(guān)閉的時候會執(zhí)行銷毀操作。在銷毀的方法中執(zhí)行一條輸出日志。 

 
 
 
 
  1. package com.hqs.springboot.shutdowndemo.bean;  
  2. import javax.annotation.PreDestroy;  
  3. /**  
  4.  * @author huangqingshi  
  5.  * @Date 2019-08-17  
  6.  */  
  7. public class TerminateBean {  
  8.     @PreDestroy  
  9.     public void preDestroy() {  
  10.         System.out.println("TerminalBean is destroyed");  
  11.     }  

做一個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. /**  
  6.  * @author huangqingshi  
  7.  * @Date 2019-08-17  
  8.  */  
  9. @Configuration  
  10. public class ShutDownConfig {  
  11.     @Bean  
  12.     public TerminateBean getTerminateBean() {  
  13.         return new TerminateBean();  
  14.     }  

在啟動類里邊輸出一個啟動日志,當工程啟動的時候,會看到啟動的輸出,接下來咱們執(zhí)行停止命令。 

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

以下日志可以輸出啟動時的日志打印和停止時的日志打印,同時程序已經(jīng)停止。是不是比較神奇。

第二種方法也比較簡單,獲取程序啟動時候的context,然后關(guān)閉主程序啟動時的context。這樣程序在關(guān)閉的時候也會調(diào)用PreDestroy注解。如下方法在程序啟動十秒后進行關(guān)閉。 

 
 
 
 
  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啟動的時候?qū)⑦M程號寫入一個app.pid文件,生成的路徑是可以指定的,可以通過命令 cat /Users/huangqingshi/app.id | xargs kill 命令直接停止服務,這個時候bean對象的PreDestroy方法也會調(diào)用的。這種方法大家使用的比較普遍。寫一個start.sh用于啟動springboot程序,然后寫一個停止程序?qū)⒎胀V??! ?nbsp;

 
 
 
 
  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(); 

第四種方法,通過調(diào)用一個SpringApplication.exit()方法也可以退出程序,同時將生成一個退出碼,這個退出碼可以傳遞給所有的context。

這個就是一個JVM的鉤子,通過調(diào)用這個方法的話會把所有PreDestroy的方法執(zhí)行并停止,并且傳遞給具體的退出碼給所有Context。通過調(diào)用System.exit(exitCode)可以將這個錯誤碼也傳給JVM。程序執(zhí)行完后最后會輸出: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,然后調(diào)用自己配置的Controller方法退出程序。

通過調(diào)用自己寫的/shutDownContext方法關(guān)閉程序: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.RestController;  
  9. /**  
  10.  * @author huangqingshi  
  11.  * @Date 2019-08-17  
  12.  */  
  13. @RestController  
  14. public class ShutDownController implements ApplicationContextAware {  
  15.     private ApplicationContext context;  
  16.     @PostMapping("/shutDownContext")  
  17.     public String shutDownContext() {  
  18.         ConfigurableApplicationContext ctx = (ConfigurableApplicationContext) context; 
  19.         ctx.close();  
  20.         return "context is shutdown";  
  21.     }  
  22.     @GetMapping("/")  
  23.     public String getIndex() {  
  24.         return "OK";  
  25.     }  
  26.     @Override  
  27.     public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {  
  28.         context = applicationContext;  
  29.     }  

好了,springboot的優(yōu)雅關(guān)閉方法也都實現(xiàn)好了,也有同學問,如何暴力停止呢,簡單,直接kill -9 相應的PID即可。

總結(jié)一下:

以上這幾種方法實現(xiàn)的話比較簡單,但是真實工作中還需要考慮的點還很多,比如需要保護暴露的點不被別人利用,一般要加一些防火墻,或者只在內(nèi)網(wǎng)使用,保證程序安全。

在真實的工作中的時候第三種比較常用,程序中一般使用內(nèi)存隊列或線程池的時候最好要優(yōu)雅的關(guān)機,將內(nèi)存隊列沒有處理的保存起來或線程池中沒處理完的程序處理完。但是因為停機的時候比較快,所以停服務的時候最好不要處理大量的數(shù)據(jù)操作,這樣會影響程序停止。


分享題目:Springboot優(yōu)雅停止服務的幾種方法
路徑分享:http://www.dlmjj.cn/article/cdjjisc.html