成人免费xxxxx在线视频软件_久久精品久久久_亚洲国产精品久久久_天天色天天色_亚洲人成一区_欧美一级欧美三级在线观看

別再使用 RestTemplate了,來了解一下官方推薦的 WebClient !

開發 前端
由于 RestTemplace 已棄用,開發人員應開始使用 WebClient 進行 REST 調用,非阻塞 I/O 調用肯定會提高應用程序性能。它不僅提供了許多其他令人興奮的功能,例如改進的錯誤處理和對流的支持,而且如果需要,它還可以在阻塞模式下使用來模擬 RestTemplate 行為。

在 Spring Framework 5.0 及更高版本中,RestTemplate 已被棄用,取而代之的是較新的 WebClient。這意味著雖然 RestTemplate 仍然可用,但鼓勵 Spring 開發人員遷移到新項目的 WebClient。

WebClient 優于 RestTemplate 的原因有幾個:

  • 非阻塞 I/O:WebClient 構建在 Reactor 之上,它提供了一種非阻塞、反應式的方法來處理 I/O。這可以在高流量應用程序中實現更好的可擴展性和更高的性能。
  • 函數式風格:WebClient 使用函數式編程風格,可以使代碼更易于閱讀和理解。它還提供了流暢的 API,可以更輕松地配置和自定義請求。
  • 更好地支持流式傳輸:WebClient 支持請求和響應正文的流式傳輸,這對于處理大文件或實時數據非常有用。
  • 改進的錯誤處理:WebClient 提供比 RestTemplate 更好的錯誤處理和日志記錄,從而更輕松地診斷和解決問題。

重點:即使升級了spring web 6.0.0版本,也無法在HttpRequestFactory中設置請求超時,這是放棄使用 RestTemplate 的最大因素之一。

圖片圖片

設置請求超時不會有任何影響

總的來說,雖然 RestTemplate 可能仍然適用于某些用例,但 WebClient 提供了幾個優勢,使其成為現代 Spring 應用程序的更好選擇。

讓我們看看如何在 SpringBoot 3 應用程序中使用 WebClient。

(1) 創建網絡客戶端:

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) 同步發送請求(就像RestTemplate一樣)

如果你想堅持使用發送 HTTP 請求并等待響應的老方法,也可以使用 WebClient 實現如下所示的相同功能:

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) 異步發送請求:

有時我們不想等待響應,而是希望異步處理響應,這可以按如下方式完成:

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);
}

要使用此函數,只需傳入要向其發送 POST 請求的 URL 以及要在請求正文中以 URL 編碼字符串形式發送的數據。關注工眾號:碼猿技術專欄,回復關鍵詞:1111 獲取阿里內部Java性能調優手冊!該函數將返回來自服務器的響應,或者如果請求由于任何原因失敗,則返回一條錯誤消息。

請注意,在此示例中,WebClient是使用默認配置構建的。你可能需要根據不同要求進行不同的配置。

另請注意,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() 的參數。如果請求成功并收到響應作為參數,則執行第一個 lambda 表達式;如果請求失敗并收到錯誤作為參數,則執行第二個 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() 每次調用都采用兩個參數:

  • aPredicate確定錯誤狀態代碼是否與條件匹配
  • aFunction用于返回Mono,即要傳播到訂閱者的錯誤信息。

如果狀態代碼與條件匹配,Mono則會發出相應的狀態代碼,并且Mono鏈會因錯誤而終止。在此示例中,Mono 將發出一條 RuntimeException 錯誤消息,指示該錯誤是客戶端錯誤還是服務器錯誤。

(5) 根據錯誤狀態采取行動:

要根據Mono的subscribe()方法中的錯誤采取操作,可以在subscribe函數中處理響應的lambda表達式之后添加另一個lambda表達。如果在處理Monumber的過程中出現錯誤,則執行第二個lambda表達式。

下面是如何使用makePostRequestAsync函數和處理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的實例,則代碼將從異常中提取狀態代碼和狀態文本,并將它們記錄到日志中。

還可以根據發生的特定錯誤在此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 的屬性,只能設置一次。如果需要,我們始終可以使用新的連接超時值創建一個新的 Web 客戶端實例。

連接超時、讀取超時和請求超時的區別如下:

圖片圖片

結論

由于 RestTemplace 已棄用,開發人員應開始使用 WebClient 進行 REST 調用,非阻塞 I/O 調用肯定會提高應用程序性能。它不僅提供了許多其他令人興奮的功能,例如改進的錯誤處理和對流的支持,而且如果需要,它還可以在阻塞模式下使用來模擬 RestTemplate 行為。

責任編輯:武曉燕 來源: 碼猿技術專欄
相關推薦

2023-10-08 10:37:48

springweb版本

2024-04-16 13:34:26

JSONMsgpack存儲

2020-03-01 13:55:15

datasetsear免費數據集2500萬

2024-09-19 09:30:39

緩存框架抽象

2021-01-18 05:19:11

數字指紋

2020-02-10 14:26:10

GitHub代碼倉庫

2023-10-23 15:38:12

Spring 5開發

2025-02-17 16:45:40

2021-12-15 10:29:44

iOS蘋果系統

2020-12-10 08:44:35

WebSocket輪詢Comet

2022-03-24 13:36:18

Java悲觀鎖樂觀鎖

2020-08-27 15:35:01

存儲

2020-03-01 17:53:38

Excel大數據微軟

2019-02-20 14:16:43

2018-06-05 17:40:36

人工智能語音識別

2024-04-11 12:19:01

Rust數據類型

2019-11-27 11:27:52

分布式存儲系統

2018-04-25 06:46:52

2023-03-02 08:00:55

包管理工具pnpm 包

2019-03-11 14:33:21

Redis內存模型數據庫
點贊
收藏

51CTO技術棧公眾號

主站蜘蛛池模板: 妞干网av| 欧美一区二区三区精品 | 久久亚洲精品国产精品紫薇 | 97精品超碰一区二区三区 | 激情久久网 | 91精品国产日韩91久久久久久 | 日本理论片好看理论片 | www国产亚洲精品久久网站 | www久久国产 | 91久久综合亚洲鲁鲁五月天 | 国产在线视频一区二区 | 亚洲高清视频一区二区 | 日韩在线资源 | 精品久久久久久久久久 | 精品国产31久久久久久 | jlzzjlzz国产精品久久 | 伊人av在线播放 | 国产日韩视频 | 国产成人精品久久二区二区91 | 亚洲成人免费视频 | 亚洲色图网址 | 成年人视频在线免费观看 | 欧美精品中文字幕久久二区 | 91国内精精品久久久久久婷婷 | 暖暖成人免费视频 | 日本一区不卡 | 成人欧美一区二区三区黑人孕妇 | 中文一区二区 | 91影视 | 成人一区二区在线 | 性一交一乱一伦视频免费观看 | 成人免费视频观看视频 | 一区二区免费在线视频 | 久久久久国产 | www日韩 | 999精品在线 | 欧美一级在线免费观看 | 欧美亚洲视频在线观看 | 毛片片| 亚卅毛片 | 亚洲一区二区三区四区五区午夜 |