Springboot : How to use WebClient instead of RestTemplate for Performing Non blocking and Asynchronous calls

一笑奈何 提交于 2020-12-29 06:08:56

问题


I have a springboot project which uses Springboot Resttemplate. We have moved to springboot 2.0.1 from 1.5.3 and we are trying to make the rest calls from it asynchronous by using WebClient. We used to process the string received using Resttemplate as given below. But WebClient returns only data in Mono or Flux. How can I get the data as String. Already tried block() method , but it does asynchronous calls.

@Retryable(maxAttempts = 4, value = java.net.ConnectException.class,
           backoff = @Backoff(delay = 3000, multiplier = 2))
public Mono<String> getResponse(String url) {
    return webClient.get().uri(urlForCurrent).accept(APPLICATION_JSON)
                    .retrieve()
                    .bodyToMono(String.class);
}

Present Data flow with RestTemplate

  1. Controller receives the client call
  2. provider gets the data in String format
  3. Provider processes the String
  4. Data is given to controller

Controller.java

@RequestMapping(value = traffic/, method = RequestMethod.GET,
                produces = MediaType.APPLICATION_JSON_VALUE)
public String getTraffic(@RequestParam("place") String location) throws InterruptedException, ExecutionException {
    String trafficJSON = Provider.getTrafficJSON(location)
    return trafficJSON;
}

Provider.java

public String getTrafficJSON(String location) {
    String url = ----;

    ResponseEntity<String> response = dataFetcher.getResponse(url);

    /// RESPONSEBODY IS READ AS STRING AND IT NEEDS TO BE PROCESSED
    if (null != response {
        return parser.transformJSON(response.getBody(), params);
    }

    return null;
}

DataFetcher.java

@Retryable(maxAttempts = 4,
           value = java.net.ConnectException.class,
           backoff = @Backoff(delay = 3000, multiplier = 2))
public ResponseEntity<String> getResponse(String url) {
    /* ----------------------- */
    return getRestTemplate().getForEntity(urlForCurrent, String.class);
}

回答1:


Due to the fact that there are lot of misconception, so here I'm going to clear up some things.

Spring has officially stated that they will deprecate RestTemplate in the future so if you can, use WebClient if you want to be as future proof as possible.

as stated in the RestTemplate API

NOTE: As of 5.0, the non-blocking, reactive org.springframework.web.reactive.client.WebClient offers a modern alternative to the RestTemplate with efficient support for both sync and async, as well as streaming scenarios. The RestTemplate will be deprecated in a future version and will not have major new features added going forward. See the WebClient section of the Spring Framework reference documentation for more details and example code.

Non reactive application

If your application is a non-reactive application (not returning fluxes or monos to the calling clients) what you have to do is to use block() if you need the value. You can of course use Mono or Flux internally in your application but in the end you must call block() to get the concrete value that you need to return to the calling client.

Non reactive applications use tomcat as the underlying server implementation, which is a servlet based server that will assign 1 thread per request so you will not gain the performance gains you get with a reactive application.

The latest version of Tomcat supports the latest servlet spec +3.0 that in turn supports Non-blocking I/O but how this support is for WebFlux applications is as of this writing not clear in the Spring docs.

Reactive application

If you on the other hand you have a reactive application you should never under any circumstances ever call block() or subscribe() in your application. Blocking is exactly what it says, it will block a thread and block that threads execution until it can move on, this is bad in a reactive world.

Underlying server implementation here is a netty server which is a non servlet, event based server that will not assign one thread to each request, the server itself is thread agnostic and any thread available will handle anything at any time during any request.

How do i know what application i have?

Spring states that if you have both spring-web and spring-webflux on the classpath, the application will favor spring-web and per default start up a non-reactive application with an underlying tomcat server.

This behaviour can manually be overridden if needed as spring states.

Adding both spring-boot-starter-web and spring-boot-starter-webflux modules in your application results in Spring Boot auto-configuring Spring MVC, not WebFlux. This behavior has been chosen because many Spring developers add spring-boot-starter-webflux to their Spring MVC application to use the reactive WebClient. You can still enforce your choice by setting the chosen application type to SpringApplication.setWebApplicationType(WebApplicationType.REACTIVE).

The “Spring WebFlux Framework”

So how to implement WebClient

@Retryable(maxAttempts = 4,
       value = java.net.ConnectException.class,
       backoff = @Backoff(delay = 3000, multiplier = 2))
public ResponseEntity<String> getResponse(String url) {
    return webClient.get()
            .uri(url)
            .exchange()
            .flatMap(response -> response.toEntity(String.class))
            .block();
}

This is the easiest and the most less intrusive implementation. You ofcourse need to build a proper webclient in maybe a @Bean and autowire it into its class.




回答2:


The first thing to understand is if you are needing to call .block() you might as well stick with RestTemplate, using WebClient will gain you nothing.

You need to start thinking in reactive terms if you want to gain from using WebClient. A reactive process is really just a sequence of steps, the input of each step being the output of the step before it. When a request comes in, your code creates the sequence of steps and returns immediately releasing the http thread. The framework then uses a pool of worker threads to execute each step when the input from the previous step becomes available.

The benefit is a huge gain in capacity to accept competing requests at the small cost of having to rethink the way you write code. Your application will only need a very small pool of http threads and another very small pool of worker threads.

When your controller method is returning a Mono or Flux, you have got it right and there will be no need to call block().

Something like this in it's simplest form:

@GetMapping(value = "endpoint", produces = MediaType.TEXT_PLAIN_VALUE)
@ResponseBody
@ResponseStatus(OK)
public Mono<String> controllerMethod() {

    final UriComponentsBuilder builder =
            UriComponentsBuilder.fromHttpUrl("http://base.url/" + "endpoint")
                    .queryParam("param1", "value");

    return webClient
            .get()
            .uri(builder.build().encode().toUri())
            .accept(APPLICATION_JSON_UTF8)
            .retrieve()
            .bodyToMono(String.class)
            .retry(4)
            .doOnError(e -> LOG.error("Boom!", e))
            .map(s -> {

                // This is your transformation step. 
                // Map is synchronous so will run in the thread that processed the response. 
                // Alternatively use flatMap (asynchronous) if the step will be long running. 
                // For example, if it needs to make a call out to the database to do the transformation.

                return s.toLowerCase();
            });
}

Moving to thinking in reactive is a pretty big paradigm shift, but well worth the effort. Hang in there, it's really not that difficult once you can wrap your head around having no blocking code at all in your entire application. Build the steps and return them. Then let the framework manage the executions of the steps.

Happy to provide more guidance if any of this is not clear.

Remember to have fun :)




回答3:


The first step is to build WebClient object with the baseUrl;

WebClient webClient = WebClient.builder()
    .baseUrl("http://localhost:8080/api") //baseUrl
    .defaultHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE)
    .build();

Then choose the method and append the path along with the request variables or body payload.

ResponseSpec responseSpec = webClient
    .get()
    .uri(uriBuilder -> uriBuilder.path("/findById") //additional path
        .queryParam("id", id).build())
    .retrieve()
    .onStatus(HttpStatus::is4xxClientError, response -> Mono.error(new CustomRuntimeException("Error")));

Wait for the response with block() function of bodyToMono. If you want response as String, you can convert it using google's gson library.

Object response = responseSpec.bodyToMono(Object.class).block();
Gson gson = new Gson();
String str = gson.toJson(response);

If you don't want to need to know the status of the api call, you can do like following.

webClient
    .post()
    .uri(uri -> uri.path("/save").build())
    .body( BodyInserters.fromObject(payload) )
    .exchange().subscribe();


来源:https://stackoverflow.com/questions/57355725/springboot-how-to-use-webclient-instead-of-resttemplate-for-performing-non-blo

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!