五種實現方式配置Spring Boot API接口超時時間
環境:SpringBoot3.4.0
1. 簡介
在開發API接口時,配置API接口的超時時間是一項非常重要的任務。超時時間的設置對于確保系統的穩定性和響應性至關重要。當客戶端發送請求到服務器時,如果服務器由于某些原因(如數據庫查詢緩慢、外部服務調用失敗等)無法及時響應,那么設置合理的超時時間可以防止服務器資源被長時間占用,從而避免系統崩潰或性能下降。
在Spring Boot中,有多種方式可以配置API接口的超時時間。針對不同的應用場景,選擇正確的配置方式,可以確保系統在面對各種復雜場景時都能保持高效和穩定。
本篇文章將通過的介紹如下幾種API超時情況的配置:
- 事務超時時間配置
如果你的當前的API接口涉及到事務相關,那么我們可以通過設置設置的超時時間來確保由于數據庫緩慢要引起的超時情況。 - 基于Resilience4j的超時保護機制
我們可以通過Resilience4j提供的超時機制來設置有效的超時時間。 - 異步請求超時
如果你當前請求是異步請求,那么我們可以通過配置異步超時時間來限制接口等待時間。 - HTTP Client超時配置
我們將介紹3種HTTP Client超時的配置,分別是RestTemplate,RestClient,WebClient。 - 基于NGINX代理超時配置
通常我們的后端接口一般會通過NGINX進行反向代理,在這種情況下,我們可以在其代理上配置超時時間。
2. 實戰案例
2.1 事務超時配置
我們可以在數據庫調用中實現請求超時的一種方法是利用Spring的@Transactional注解。該注解具有一個可以設置的超時屬性。該屬性的默認值為-1,相當于沒有設置任何超時。
@Transactional(timeout = 1)
public List<User> query() {
this.userRepository.findById(8L).ifPresent(System.out::println) ;
try {
TimeUnit.SECONDS.sleep(1) ;
} catch (InterruptedException e) {}
return this.userRepository.findAll() ;
}
如上配置注解中配置超時時間為1s,內部執行時先根據id查詢,此時能正常執行,當休眠1s后,再次執行數據庫操作將拋出超時異常。
首先,我們進行如下異常配置:
@ExceptionHandler(TransactionTimedOutException.class)
public ResponseEntity<Object> txTimeout(TransactionTimedOutException ex) {
return ResponseEntity.ok("請求超時: " + ex.getMessage()) ;
}
測試接口
@GetMapping
public ResponseEntity<Object> query() {
return ResponseEntity.ok(this.userService.query()) ;
}
測試結果
圖片
以上我們利用了事務的超時時間來保護接口。
2.2 基于Resilience4j的超時保護機制
Resilience4j提供了一個TimeLimiter模塊,專門用來處理超時保護的。
首先,引入下面依賴:
<dependency>
<groupId>io.github.resilience4j</groupId>
<artifactId>resilience4j-spring-boot3</artifactId>
<version>2.2.0</version>
</dependency>
接下來,通過注解配置需要保護的接口
@TimeLimiter(name = "queryUser", fallbackMethod = "fallbackQuery")
@GetMapping
public CompletionStage<ResponseEntity<Object>> query() {
return CompletableFuture.supplyAsync(() -> {
try {
// 模擬耗時操作
TimeUnit.SECONDS.sleep(2) ;
} catch (InterruptedException e) {}
return ResponseEntity.ok("success") ;
}) ;
}
public CompletionStage<ResponseEntity<Object>> fallbackQuery(Throwable e) {
return CompletableFuture.completedStage(ResponseEntity.ok(e.getMessage())) ;
}
說明:
name:在配置文件中定義的超時相關配置,如果配置文件中沒有配置則使用默認的配置。
fallbackMethod:當發生超時現象將調用的降級方法。
注意:方法的返回值必須是CompletionStage類型。
最后,配置超時
resilience4j:
timelimiter:
instances:
#該名稱為上面注解中的name
queryUser:
timeout-duration: 1s
測試結果
圖片
此種方式是不是非常的簡單好用,一個注解搞定。
2.3 異步請求超時配置
當我們的API接口是異步請求時,我們可以直接在配置文件中對異步請求的超時時間進行配置:
spring:
mvc:
async:
request-timeout: 1s
異步請求接口
@GetMapping("/async")
public Callable<String> async() {
return () -> {
try {
TimeUnit.SECONDS.sleep(10) ;
} catch (InterruptedException e) {
return "任務中斷 - " + e.getMessage() ;
}
return "異步請求成功" ;
} ;
}
測試結果
圖片
雖然這里休眠了10s,但在1s后,直接輸出了異常信息。
2.4 HTTP Client超時配置
這里我們將介紹3種接口調用的超時配置,分別是:RestTemplate,RestClient已經WebClient,其中RestTemplate與RestClient是基于阻塞式調用并且RestClient是Spring6.1版本開始提供的;而WebClient則是基于響應式的調用(非阻塞)。官方推薦使用WebClient。
RestTemplate超時配置
@Bean
RestTemplate restTemplate(RestTemplateBuilder builder) {
return builder
// 連接超時配置
.connectTimeout(Duration.ofSeconds(1))
// 讀取超時配置
.readTimeout(Duration.ofSeconds(1))
.build() ;
}
這是最簡單的配置,你還可以通過如下工廠方式配置
@Bean
RestTemplate restTemplate() {
ClientHttpRequestFactorySettings settings = ClientHttpRequestFactorySettings.defaults()
.withConnectTimeout(Duration.ofSeconds(1))
.withReadTimeout(Duration.ofSeconds(1));
RestTemplate restTemplate = new RestTemplate(
ClientHttpRequestFactoryBuilder.detect().build(settings)) ;
return restTemplate ;
}
根據你的環境選擇不同的方式進行配置。
RestClient超時配置
RestClient的配置方式與上面的RestTemplate差不多。
@Bean
RestClient restClient() {
ClientHttpRequestFactorySettings settings = ClientHttpRequestFactorySettings.defaults()
.withConnectTimeout(Duration.ofSeconds(1))
.withReadTimeout(Duration.ofSeconds(1)) ;
return RestClient
.builder()
.requestFactory(ClientHttpRequestFactoryBuilder.detect().build(settings))
.build() ;
}
最后,我們再來介紹官方推薦的WebClient。
WebClient超時配置
首先,我們要引入以下的依賴:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-webflux</artifactId>
</dependency>
下面進行超時配置:
@Bean
WebClient webClient() {
HttpClient httpClient = HttpClient.create()
.doOnConnected(conn -> conn
.addHandlerLast(new ReadTimeoutHandler(1))
.addHandlerLast(new WriteTimeoutHandler(1))) ;
return WebClient.builder()
.clientConnector(new ReactorClientHttpConnector(httpClient ))
.build() ;
}
下面我們通過WebClient進行接口調用進行測試
訪問接口
@GetMapping("/client")
public String client() {
try {
TimeUnit.SECONDS.sleep(3) ;
} catch (InterruptedException e) {
return "任務中斷 - " + e.getMessage() ;
}
return "success" ;
}
通過WebClient訪問該接口:
private final WebClient webClient ;
this.webClient
.get()
.uri("http://localhost:8080/api/users/client")
.retrieve()
.bodyToMono(String.class)
// 當發生錯誤時會自動調用該方法進行恢復繼續執行
.onErrorResume(ex -> {
return Mono.just("發生錯誤: " + ex.getMessage()) ;
})
.subscribe(System.out::println) ;
測試結果:
io.netty.handler.timeout.ReadTimeoutException: null
發生錯誤: null
以上就是關于HTTP Client的超時配置。
2.5 基于NGINX代理超時配置
通過NGINX反向代理配置超時時間
location / {
proxy_pass http://127.0.0.1:8080;
proxy_connect_timeout 1s; # 連接超時時間為30秒
proxy_send_timeout 1s; # 發送請求超時時間為60秒
proxy_read_timeout 1s; # 讀取響應超時時間為60秒
}
當發生超時時,我們這里通過日志查看:
[error] 11172#27080: *1 upstream timed out
(10060: A connection attempt failed because the connected
party did not properly respond after a period of time,
or established connection failed because connected
host has failed to respond) while reading
response header from upstream,
client: 127.0.0.1, server: localhost,
request: "GET /api/users/client HTTP/1.1",
upstream: "http://127.0.0.1:8080/api/users/client",
host: "localhost:1080"
當發生異常,我們還可以進行如下的配置,進行友好提示:
location / {
proxy_pass http://127.0.0.1:8080;
proxy_connect_timeout 1s; # 連接超時時間為30秒
proxy_send_timeout 1s; # 發送請求超時時間為60秒
proxy_read_timeout 1s; # 讀取響應超時時間為60秒
# 指定自定義錯誤頁面
error_page 504 /timeout.html;
# 指定自定義錯誤頁面的位置
location = /timeout.html {
root D:/all/html/;
internal;
}
}
圖片