WebClient non-blocking client RestTemplate blocking client

After receiving the plurality of client requests, the performance of the method of blocking a significant decrease.

Reactive and non-blocking performance of the method should be independent of the number of requests, stable performance

Add Spring Boot WebFlux Starter dependent

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-webflux</artifactId>
</dependency>

Compare consuming

@GetMapping("/slow-service-tweets")
private List<Tweet> getAllTweets() {
    Thread.sleep(2000L); // 延迟
    return Arrays.asList(
      new Tweet("RestTemplate rules", "@user1"),
      new Tweet("WebClient is better", "@user2"),
      new Tweet("OK, both are useful", "@user1"));
}

RestTemplate time-consuming service calls

@GetMapping("/tweets-blocking")
public List<Tweet> getTweetsBlocking() {
    log.info("Starting BLOCKING Controller!");
    final String uri = getSlowServiceUri();
    RestTemplate restTemplate = new RestTemplate();
    ResponseEntity<List<Tweet>> response = restTemplate.exchange(
      uri, HttpMethod.GET, null,
      new ParameterizedTypeReference<List<Tweet>>(){});
    List<Tweet> result = response.getBody();
    result.forEach(tweet -> log.info(tweet.toString()));
    log.info("Exiting BLOCKING Controller!");
    return result;
}

Since RestTemplate is a synchronous call, enter the code when calling Endpoint block waiting for a response to be time-consuming service calls. Only after the response is received, the method will be performed in the subsequent code, the log follows:

Starting BLOCKING Controller!
Tweet(text=RestTemplate rules, username=@user1)
Tweet(text=WebClient is better, username=@user2)
Tweet(text=OK, both are useful, username=@user1)
Exiting BLOCKING Controller!

Next  WebClient time-consuming service calls

@GetMapping(value = "/tweets-non-blocking",
            produces = MediaType.TEXT_EVENT_STREAM_VALUE)
public Flux<Tweet> getTweetsNonBlocking() {
    log.info("Starting NON-BLOCKING Controller!");
    Flux<Tweet> tweetFlux = WebClient.create()
      .get()
      .uri(getSlowServiceUri())
      .retrieve()
      .bodyToFlux(Tweet.class);
    tweetFlux.subscribe(tweet -> log.info(tweet.toString()));
    log.info("Exiting NON-BLOCKING Controller!");
    return tweetFlux;
}

  

WebClient return  Flux  Publisher after the execution is complete. When the result is ready, publisher will send a list of tweets to subscribers. Note: The client (referred to herein as a Web browser) call / tweets-non-blocking Endpoint  may also subscribe  Flux  object. This method had been implemented Endpoint completed before a response is received.

 

Starting NON-BLOCKING Controller!
Exiting NON-BLOCKING Controller!
Tweet(text=RestTemplate rules, username=@user1)
Tweet(text=WebClient is better, username=@user2)
Tweet(text=OK, both are useful, username=@user1) 

 to sum up:

This article discusses Spring in two different ways of using Web Client.

 

RestTemplate using Java Servlet API, thus blocking synchronous call. Instead, WebClient is asynchronous and does not block while waiting for the response of the executing thread. Only when the results are ready response, we will initiate a notification.

 

RestTemplate still have its uses. Non-blocking mode in some scenarios take up system resources than blocking method is much less, when the WebClient is a better choice

 

Guess you like

Origin www.cnblogs.com/otways/p/11411652.html