別再使用 RestTemplate了,來了解一下官方推薦的 WebClient !
在 Spring Framework 5.0 及更高版本中,RestTemplate 已被棄用,取而代之的是較新的 WebClient。這意味著雖然 RestTemplate 仍然可用,但鼓勵 Spring 開發(fā)人員遷移到新項目的 WebClient。
WebClient 優(yōu)于 RestTemplate 的原因有幾個:
- 非阻塞 I/O:WebClient 構建在 Reactor 之上,它提供了一種非阻塞、反應式的方法來處理 I/O。這可以在高流量應用程序中實現(xiàn)更好的可擴展性和更高的性能。
- 函數(shù)式風格:WebClient 使用函數(shù)式編程風格,可以使代碼更易于閱讀和理解。它還提供了流暢的 API,可以更輕松地配置和自定義請求。
- 更好地支持流式傳輸:WebClient 支持請求和響應正文的流式傳輸,這對于處理大文件或實時數(shù)據(jù)非常有用。
- 改進的錯誤處理:WebClient 提供比 RestTemplate 更好的錯誤處理和日志記錄,從而更輕松地診斷和解決問題。
重點:即使升級了spring web 6.0.0版本,也無法在HttpRequestFactory中設置請求超時,這是放棄使用 RestTemplate 的最大因素之一。
圖片
設置請求超時不會有任何影響
總的來說,雖然 RestTemplate 可能仍然適用于某些用例,但 WebClient 提供了幾個優(yōu)勢,使其成為現(xiàn)代 Spring 應用程序的更好選擇。
讓我們看看如何在 SpringBoot 3 應用程序中使用 WebClient。
(1) 創(chuàng)建網(wǎng)絡客戶端:
import io.netty.channel.ChannelOption;
import io.netty.channel.ConnectTimeoutException;
import io.netty.handler.timeout.ReadTimeoutException;
import io.netty.handler.timeout.ReadTimeoutHandler;
import io.netty.handler.timeout.TimeoutException;
import jakarta.annotation.PostConstruct;
import java.time.Duration;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpMethod;
import org.springframework.http.MediaType;
import org.springframework.http.client.reactive.ReactorClientHttpConnector;
import org.springframework.stereotype.Service;
import org.springframework.web.reactive.function.client.WebClient;
import org.springframework.web.reactive.function.client.WebClientRequestException;
import org.springframework.web.reactive.function.client.WebClientResponseException;
import reactor.core.publisher.Mono;
import reactor.netty.http.client.HttpClient;
HttpClient httpClient =
HttpClient.create()
.option(ChannelOption.CONNECT_TIMEOUT_MILLIS, connectionTimeout)
.responseTimeout(Duration.ofMillis(requestTimeout))
.doOnConnected(conn -> conn.addHandlerLast(new ReadTimeoutHandler(readTimeout)));
WebClient client =
WebClient.builder().clientConnector(new ReactorClientHttpConnector(httpClient)).build();
(2) 同步發(fā)送請求(就像RestTemplate一樣)
如果你想堅持使用發(fā)送 HTTP 請求并等待響應的老方法,也可以使用 WebClient 實現(xiàn)如下所示的相同功能:
public String postSynchronously(String url, String requestBody) {
LOG.info("Going to hit API - URL {} Body {}", url, requestBody);
String response = "";
try {
response =
client
.method(HttpMethod.POST)
.uri(url)
.accept(MediaType.ALL)
.contentType(MediaType.APPLICATION_JSON)
.bodyValue(requestBody)
.retrieve()
.bodyToMono(String.class)
.block();
} catch (Exception ex) {
LOG.error("Error while calling API ", ex);
throw new RunTimeException("XYZ service api error: " + ex.getMessage());
} finally {
LOG.info("API Response {}", response);
}
return response;
}
block()用于同步等待響應,這可能并不適合所有情況,你可能需要考慮subscribe()異步使用和處理響應。
(3) 異步發(fā)送請求:
有時我們不想等待響應,而是希望異步處理響應,這可以按如下方式完成:
import org.springframework.http.MediaType;
import org.springframework.web.reactive.function.BodyInserters;
import org.springframework.web.reactive.function.client.WebClient;
import reactor.core.publisher.Mono;
public static Mono<String> makePostRequestAsync(String url, String postData) {
WebClient webClient = WebClient.builder().build();
return webClient.post()
.uri(url)
.contentType(MediaType.APPLICATION_FORM_URLENCODED)
.body(BodyInserters.fromFormData("data", postData))
.retrieve()
.bodyToMono(String.class);
}
要使用此函數(shù),只需傳入要向其發(fā)送 POST 請求的 URL 以及要在請求正文中以 URL 編碼字符串形式發(fā)送的數(shù)據(jù)。關注工眾號:碼猿技術專欄,回復關鍵詞:1111 獲取阿里內(nèi)部Java性能調優(yōu)手冊!該函數(shù)將返回來自服務器的響應,或者如果請求由于任何原因失敗,則返回一條錯誤消息。
請注意,在此示例中,WebClient是使用默認配置構建的。你可能需要根據(jù)不同要求進行不同的配置。
另請注意,block()用于同步等待響應,這可能并不適合所有情況。你可能需要考慮subscribe()異步使用和處理響應。
要使用響應,您可以訂閱Mono并異步處理響應。下面是一個例子:
makePostRequestAsync( "https://example.com/api" , "param1=value1?m2=value2" )
.subscribe(response -> {
// 處理響應
System.out.println ( response );
}, error -> {
/ / 處理錯誤
System.err.println ( error .getMessage ());
}
);
subscribe()用于異步處理響應,你可以提供兩個 lambda 表達式作為 subscribe() 的參數(shù)。如果請求成功并收到響應作為參數(shù),則執(zhí)行第一個 lambda 表達式;如果請求失敗并收到錯誤作為參數(shù),則執(zhí)行第二個 lambda 表達式。
(4) 處理4XX和5XX錯誤:
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.web.reactive.function.BodyInserters;
import org.springframework.web.reactive.function.client.WebClient;
import reactor.core.publisher.Mono;
public static Mono<String> makePostRequestAsync(String url, String postData) {
WebClient webClient = WebClient.builder()
.baseUrl(url)
.build();
return webClient.post()
.uri("/")
.contentType(MediaType.APPLICATION_FORM_URLENCODED)
.body(BodyInserters.fromFormData("data", postData))
.retrieve()
.onStatus(HttpStatus::is4xxClientError, clientResponse -> Mono.error(new RuntimeException("Client error")))
.onStatus(HttpStatus::is5xxServerError, clientResponse -> Mono.error(new RuntimeException("Server error")))
.bodyToMono(String.class);
}
在此示例中,該onStatus()方法被調用兩次,一次針對 4xx 客戶端錯誤,一次針對 5xx 服務器錯誤。onStatus() 每次調用都采用兩個參數(shù):
- aPredicate確定錯誤狀態(tài)代碼是否與條件匹配
- aFunction用于返回Mono,即要傳播到訂閱者的錯誤信息。
如果狀態(tài)代碼與條件匹配,Mono則會發(fā)出相應的狀態(tài)代碼,并且Mono鏈會因錯誤而終止。在此示例中,Mono 將發(fā)出一條 RuntimeException 錯誤消息,指示該錯誤是客戶端錯誤還是服務器錯誤。
(5) 根據(jù)錯誤狀態(tài)采取行動:
要根據(jù)Mono的subscribe()方法中的錯誤采取操作,可以在subscribe函數(shù)中處理響應的lambda表達式之后添加另一個lambda表達。如果在處理Monumber的過程中出現(xiàn)錯誤,則執(zhí)行第二個lambda表達式。
下面是如何使用makePostRequestAsync函數(shù)和處理subscribe方法中的錯誤的更新示例:
makePostRequestAsync("https://example.com/api", "param1=value1?m2=value2")
.subscribe(response -> {
// handle the response
System.out.println(response);
}, error -> {
// handle the error
System.err.println("An error occurred: " + error.getMessage());
if (error instanceof WebClientResponseException) {
WebClientResponseException webClientResponseException = (WebClientResponseException) error;
int statusCode = webClientResponseException.getStatusCode().value();
String statusText = webClientResponseException.getStatusText();
System.err.println("Error status code: " + statusCode);
System.err.println("Error status text: " + statusText);
}
});
subscribe方法中的第二個lambda表達式檢查錯誤是否是WebClientResponseException的實例,這是WebClient在服務器有錯誤響應時拋出的特定類型的異常。如果它是WebClientResponseException的實例,則代碼將從異常中提取狀態(tài)代碼和狀態(tài)文本,并將它們記錄到日志中。
還可以根據(jù)發(fā)生的特定錯誤在此lambda表達式中添加其他錯誤處理邏輯。例如,你可以重試請求、回退到默認值或以特定方式記錄錯誤。
(6) 處理成功響應和錯誤的完整代碼:
responseMono.subscribe(
response -> {
// handle the response
LOG.info("SUCCESS API Response {}", response);
},
error -> {
// handle the error
LOG.error("An error occurred: {}", error.getMessage());
LOG.error("error class: {}", error.getClass());
// Errors / Exceptions from Server
if (error instanceof WebClientResponseException) {
WebClientResponseException webClientResponseException =
(WebClientResponseException) error;
int statusCode = webClientResponseException.getStatusCode().value();
String statusText = webClientResponseException.getStatusText();
LOG.info("Error status code: {}", statusCode);
LOG.info("Error status text: {}", statusText);
if (statusCode >= 400 && statusCode < 500) {
LOG.info(
"Error Response body {}", webClientResponseException.getResponseBodyAsString());
}
Throwable cause = webClientResponseException.getCause();
LOG.error("webClientResponseException");
if (null != cause) {
LOG.info("Cause {}", cause.getClass());
if (cause instanceof ReadTimeoutException) {
LOG.error("ReadTimeout Exception");
}
if (cause instanceof TimeoutException) {
LOG.error("Timeout Exception");
}
}
}
// Client errors i.e. Timeouts etc -
if (error instanceof WebClientRequestException) {
LOG.error("webClientRequestException");
WebClientRequestException webClientRequestException =
(WebClientRequestException) error;
Throwable cause = webClientRequestException.getCause();
if (null != cause) {
LOG.info("Cause {}", cause.getClass());
if (cause instanceof ReadTimeoutException) {
LOG.error("ReadTimeout Exception");
}
if (cause instanceof ConnectTimeoutException) {
LOG.error("Connect Timeout Exception");
}
}
}
});
超時
我們可以在每個請求中設置超時,如下所示:
return webClient
.method(this.httpMethod)
.uri(this.uri)
.headers(httpHeaders -> httpHeaders.addAll(additionalHeaders))
.bodyValue(this.requestEntity)
.retrieve()
.bodyToMono(responseType)
.timeout(Duration.ofMillis(readTimeout)) // request timeout for this request
.block();
但是,我們無法在每個請求中設置連接超時,這是WebClient 的屬性,只能設置一次。如果需要,我們始終可以使用新的連接超時值創(chuàng)建一個新的 Web 客戶端實例。
連接超時、讀取超時和請求超時的區(qū)別如下:
圖片
結論
由于 RestTemplace 已棄用,開發(fā)人員應開始使用 WebClient 進行 REST 調用,非阻塞 I/O 調用肯定會提高應用程序性能。它不僅提供了許多其他令人興奮的功能,例如改進的錯誤處理和對流的支持,而且如果需要,它還可以在阻塞模式下使用來模擬 RestTemplate 行為。