springboot - 2。3新特性

來源:魅力女性吧 3.17W
springboot 2.3新特性

2、3.0新特性—優雅停機

目前SpringBoot內置的四個嵌入式Web服務器(Jetty,Reactor Netty,Tomcat和Undertow)以及響應式和基於Servlet的Web應用程序都支持優雅關機。

配置寬限期後,在關閉時,Web服務器將不再允許新請求,並且將等待寬限期以使活動請求完成。

Demo嘗試

1、新建一個Spring-web項目,引入SpringBoot最新版本2.3.0.RELEASE

<dependency>

<groupId>org.springframework.boot</groupId>

<artifactId>spring-boot</artifactId>

<version>2.3.0.RELEASE</version>

</dependency>

優雅停機要求Tomcat的版本必須在 9.0.33及以上, spring-boot 2.3.0.RELEASE 版本內置的Tomcat 是9.0.35 , 所以默認是支持的, 不用我們再考慮

2、在application.properties添加配置

## 開啟優雅停機, 如果不配置是默認IMMEDIATE, 立即停機

server.shutdown=graceful

## 優雅停機寬限期時間

spring.lifecycle.timeout-per-shutdown-phase=20s

3、編寫接口代碼測試

@GetMapping("sleep")

public String sleep(Integer timeout){

try{

log.info("begin sleep:{}",timeout)

TimeUnit.SECONDS.sleep(timeout)

log.info("end sleep:{}",timeout)

}catch(Exception e){

e.printStackTrace()

}

return "sleep:" + timeout

}

基本思路是, 上面寬限期時間配置的是20秒, 然後我們提供一個接口自定義睡眠時間, 調用兩次接口, 入參分別15秒和30秒, 在請求後馬上停機, 觀察日誌輸出結果, 如果15秒入參請求能夠成功輸出並相應頁面, 30秒入參請求會相應異常, 則説明優雅停機是正常工作的!

熱門標籤