別再使用 RestTemplate了,來(lái)了解一下官方推薦的 WebClient !
在 Spring Framework 5.0 及更高版本中,RestTemplate 已被棄用,取而代之的是較新的 WebClient。這意味著雖然 RestTemplate 仍然可用,但鼓勵(lì) Spring 開(kāi)發(fā)人員遷移到新項(xiàng)目的 WebClient。
WebClient 優(yōu)于 RestTemplate 的原因有幾個(gè):
- 非阻塞 I/O:WebClient 構(gòu)建在 Reactor 之上,它提供了一種非阻塞、反應(yīng)式的方法來(lái)處理 I/O。這可以在高流量應(yīng)用程序中實(shí)現(xiàn)更好的可擴(kuò)展性和更高的性能。
- 函數(shù)式風(fēng)格:WebClient 使用函數(shù)式編程風(fēng)格,可以使代碼更易于閱讀和理解。它還提供了流暢的 API,可以更輕松地配置和自定義請(qǐng)求。
- 更好地支持流式傳輸:WebClient 支持請(qǐng)求和響應(yīng)正文的流式傳輸,這對(duì)于處理大文件或?qū)崟r(shí)數(shù)據(jù)非常有用。
- 改進(jìn)的錯(cuò)誤處理:WebClient 提供比 RestTemplate 更好的錯(cuò)誤處理和日志記錄,從而更輕松地診斷和解決問(wèn)題。
重點(diǎn):即使升級(jí)了spring web 6.0.0版本,也無(wú)法在HttpRequestFactory中設(shè)置請(qǐng)求超時(shí),這是放棄使用 RestTemplate 的最大因素之一。
圖片
設(shè)置請(qǐng)求超時(shí)不會(huì)有任何影響
總的來(lái)說(shuō),雖然 RestTemplate 可能仍然適用于某些用例,但 WebClient 提供了幾個(gè)優(yōu)勢(shì),使其成為現(xiàn)代 Spring 應(yīng)用程序的更好選擇。
讓我們看看如何在 SpringBoot 3 應(yīng)用程序中使用 WebClient。
(1) 創(chuàng)建網(wǎng)絡(luò)客戶端:
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ā)送請(qǐng)求(就像RestTemplate一樣)
如果你想堅(jiān)持使用發(fā)送 HTTP 請(qǐng)求并等待響應(yīng)的老方法,也可以使用 WebClient 實(shí)現(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()用于同步等待響應(yīng),這可能并不適合所有情況,你可能需要考慮subscribe()異步使用和處理響應(yīng)。
(3) 異步發(fā)送請(qǐng)求:
有時(shí)我們不想等待響應(yīng),而是希望異步處理響應(yīng),這可以按如下方式完成:
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 請(qǐng)求的 URL 以及要在請(qǐng)求正文中以 URL 編碼字符串形式發(fā)送的數(shù)據(jù)。關(guān)注工眾號(hào):碼猿技術(shù)專欄,回復(fù)關(guān)鍵詞:1111 獲取阿里內(nèi)部Java性能調(diào)優(yōu)手冊(cè)!該函數(shù)將返回來(lái)自服務(wù)器的響應(yīng),或者如果請(qǐng)求由于任何原因失敗,則返回一條錯(cuò)誤消息。
請(qǐng)注意,在此示例中,WebClient是使用默認(rèn)配置構(gòu)建的。你可能需要根據(jù)不同要求進(jìn)行不同的配置。
另請(qǐng)注意,block()用于同步等待響應(yīng),這可能并不適合所有情況。你可能需要考慮subscribe()異步使用和處理響應(yīng)。
要使用響應(yīng),您可以訂閱Mono并異步處理響應(yīng)。下面是一個(gè)例子:
makePostRequestAsync( "https://example.com/api" , "param1=value1?m2=value2" )
.subscribe(response -> {
// 處理響應(yīng)
System.out.println ( response );
}, error -> {
/ / 處理錯(cuò)誤
System.err.println ( error .getMessage ());
}
);
subscribe()用于異步處理響應(yīng),你可以提供兩個(gè) lambda 表達(dá)式作為 subscribe() 的參數(shù)。如果請(qǐng)求成功并收到響應(yīng)作為參數(shù),則執(zhí)行第一個(gè) lambda 表達(dá)式;如果請(qǐng)求失敗并收到錯(cuò)誤作為參數(shù),則執(zhí)行第二個(gè) lambda 表達(dá)式。
(4) 處理4XX和5XX錯(cuò)誤:
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()方法被調(diào)用兩次,一次針對(duì) 4xx 客戶端錯(cuò)誤,一次針對(duì) 5xx 服務(wù)器錯(cuò)誤。onStatus() 每次調(diào)用都采用兩個(gè)參數(shù):
- aPredicate確定錯(cuò)誤狀態(tài)代碼是否與條件匹配
- aFunction用于返回Mono,即要傳播到訂閱者的錯(cuò)誤信息。
如果狀態(tài)代碼與條件匹配,Mono則會(huì)發(fā)出相應(yīng)的狀態(tài)代碼,并且Mono鏈會(huì)因錯(cuò)誤而終止。在此示例中,Mono 將發(fā)出一條 RuntimeException 錯(cuò)誤消息,指示該錯(cuò)誤是客戶端錯(cuò)誤還是服務(wù)器錯(cuò)誤。
(5) 根據(jù)錯(cuò)誤狀態(tài)采取行動(dòng):
要根據(jù)Mono的subscribe()方法中的錯(cuò)誤采取操作,可以在subscribe函數(shù)中處理響應(yīng)的lambda表達(dá)式之后添加另一個(gè)lambda表達(dá)。如果在處理Monumber的過(guò)程中出現(xiàn)錯(cuò)誤,則執(zhí)行第二個(gè)lambda表達(dá)式。
下面是如何使用makePostRequestAsync函數(shù)和處理subscribe方法中的錯(cuò)誤的更新示例:
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方法中的第二個(gè)lambda表達(dá)式檢查錯(cuò)誤是否是WebClientResponseException的實(shí)例,這是WebClient在服務(wù)器有錯(cuò)誤響應(yīng)時(shí)拋出的特定類(lèi)型的異常。如果它是WebClientResponseException的實(shí)例,則代碼將從異常中提取狀態(tài)代碼和狀態(tài)文本,并將它們記錄到日志中。
還可以根據(jù)發(fā)生的特定錯(cuò)誤在此lambda表達(dá)式中添加其他錯(cuò)誤處理邏輯。例如,你可以重試請(qǐng)求、回退到默認(rèn)值或以特定方式記錄錯(cuò)誤。
(6) 處理成功響應(yīng)和錯(cuò)誤的完整代碼:
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");
}
}
}
});
超時(shí)
我們可以在每個(gè)請(qǐng)求中設(shè)置超時(shí),如下所示:
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();
但是,我們無(wú)法在每個(gè)請(qǐng)求中設(shè)置連接超時(shí),這是WebClient 的屬性,只能設(shè)置一次。如果需要,我們始終可以使用新的連接超時(shí)值創(chuàng)建一個(gè)新的 Web 客戶端實(shí)例。
連接超時(shí)、讀取超時(shí)和請(qǐng)求超時(shí)的區(qū)別如下:
圖片
結(jié)論
由于 RestTemplace 已棄用,開(kāi)發(fā)人員應(yīng)開(kāi)始使用 WebClient 進(jìn)行 REST 調(diào)用,非阻塞 I/O 調(diào)用肯定會(huì)提高應(yīng)用程序性能。它不僅提供了許多其他令人興奮的功能,例如改進(jìn)的錯(cuò)誤處理和對(duì)流的支持,而且如果需要,它還可以在阻塞模式下使用來(lái)模擬 RestTemplate 行為。