還在使用 RestTemplate?來了解一下官方推薦的 WebClient !
閱讀本文大概需要 7 分鐘。
來自:網(wǎng)絡(luò),侵刪
-
「非阻塞 I/O」:WebClient 構(gòu)建在 Reactor 之上,它提供了一種非阻塞、反應(yīng)式的方法來處理 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ò)誤處理和日志記錄,從而更輕松地診斷和解決問題。
設(shè)置請(qǐng)求超時(shí)不會(huì)有任何影響
? 讓我們看看如何在 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一樣)
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;
}
(3) 異步發(fā)送請(qǐ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);
}
? 請(qǐng)注意,在此示例中,WebClient是使用默認(rèn)配置構(gòu)建的。你可能需要根據(jù)不同要求進(jìn)行不同的配置。 另請(qǐng)注意,block()用于同步等待響應(yīng),這可能并不適合所有情況。你可能需要考慮subscribe()異步使用和處理響應(yīng)。 ?
makePostRequestAsync( "https://example.com/api" , "param1=value1?m2=value2" )
.subscribe(response -> {
// 處理響應(yīng)
System.out.println ( response );
}, error -> {
/ / 處理錯(cuò)誤
System.err.println ( error .getMessage ());
}
);
(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);
}
-
aPredicate確定錯(cuò)誤狀態(tài)代碼是否與條件匹配 -
aFunction用于返回Mono,即要傳播到訂閱者的錯(cuò)誤信息。
(5) 根據(jù)錯(cuò)誤狀態(tài)采取行動(dòng):
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);
}
});
(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í):
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();
結(jié)論
本文為翻譯文,來源:https://medium.com
推薦閱讀:
SELECT COUNT(*) 會(huì)造成全表掃描?回去等通知吧
Spring 在多線程環(huán)境下如何確保事務(wù)一致性?
互聯(lián)網(wǎng)初中高級(jí)大廠面試題(9個(gè)G) 內(nèi)容包含Java基礎(chǔ)、JavaWeb、MySQL性能優(yōu)化、JVM、鎖、百萬并發(fā)、消息隊(duì)列、高性能緩存、反射、Spring全家桶原理、微服務(wù)、Zookeeper......等技術(shù)棧!
?戳閱讀原文領(lǐng)??! 朕已閱

