天天看點

Springboot 優雅停止服務的幾種方法!

Springboot 優雅停止服務的幾種方法!

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

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

第一種

第一種就是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=*就可以。

将如下配置設定到application.properties裡邊,設定一下服務的端口号為3333。

server.port=3333
management.endpoint.shutdown.enabled==shutdown
           

複制

接下來,咱們建立一個springboot工程,然後設定一個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對象會被初始化。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
           

複制

以下日志可以輸出啟動時的日志列印和停止時的日志列印,同時程式已經停止。是不是比較神奇。

Springboot 優雅停止服務的幾種方法!

第二種

第二種方法也比較簡單,擷取程式啟動時候的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();
           

複制

第三種

第三種方法,在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);
}
           

複制

Springboot 優雅停止服務的幾種方法!

第五種

第五種方法,自己寫一個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即可。

總結一下:

以上這幾種方法實作的話比較簡單,但是真實工作中還需要考慮的點還很多,比如需要保護暴露的點不被别人利用,一般要加一些防火牆,或者隻在内網使用,保證程式安全。

在真實的工作中的時候第三種比較常用,程式中一般使用記憶體隊列或線程池的時候最好要優雅的關機,将記憶體隊列沒有處理的儲存起來或線程池中沒處理完的程式處理完。但是因為停機的時候比較快,是以停服務的時候最好不要處理大量的資料操作,這樣會影響程式停止。