Spring Boot 優雅中止服務的幾種方法!

做者:黃青石
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便可。

總結一下:

以上這幾種方法實現的話比較簡單,可是真實工做中還須要考慮的點還不少,好比須要保護暴露的點不被別人利用,通常要加一些防火牆,或者只在內網使用,保證程序安全。

在真實的工做中的時候第三種比較經常使用,程序中通常使用內存隊列或線程池的時候最好要優雅的關機,將內存隊列沒有處理的保存起來或線程池中沒處理完的程序處理完。可是由於停機的時候比較快,因此停服務的時候最好不要處理大量的數據操做,這樣會影響程序中止。

推薦去個人博客閱讀更多:

1.Java JVM、集合、多線程、新特性系列教程

2.Spring MVC、Spring Boot、Spring Cloud 系列教程

3.Maven、Git、Eclipse、Intellij IDEA 系列工具教程

4.Java、後端、架構、阿里巴巴等大廠最新面試題

生活很美好,明天見~

相關文章
相關標籤/搜索