18

In my MicroService I have to fetch data from places. Some of the URL's are fixed but some are not . So do I need to create WebClient again and again if my base URL changes. If not then is the below way correct to create WebClient.

WebClient.create(); and later is changing the URI again and again whenever I make a call. As per my understanding creating a WebClient must be some heavy operation.

ReactorClientHttpConnector connector = new ReactorClientHttpConnector(
                options -> options.option(ChannelOption.CONNECT_TIMEOUT_MILLIS, requestTimeout).compression(true)
                        .afterNettyContextInit(ctx ->  ctx.addHandlerLast(new ReadTimeoutHandler(readTimeout, TimeUnit.MILLISECONDS))));
        return WebClient.builder()
                        .clientConnector(connector)
                        .baseUrl(hostURL)
                        .build();
Manuel Jordan
  • 15,253
  • 21
  • 95
  • 158
naval jain
  • 353
  • 3
  • 4
  • 14
  • 1
    There's an answer https://stackoverflow.com/a/49107545/10264430 – Alexander Pankin Dec 19 '18 at 06:00
  • thanks @AlexanderPankin But found it after posting it here :) – naval jain Dec 19 '18 at 06:18
  • 1
    Possible duplicate of [Right way to use Spring WebClient in multi-thread environment](https://stackoverflow.com/questions/49095366/right-way-to-use-spring-webclient-in-multi-thread-environment) – Brian Clozel Dec 19 '18 at 09:50
  • @BrianClozel My problem is a bit different . My base URI is going to change most of the time.Explanation provided would still be valid in that case. – naval jain Dec 19 '18 at 12:01
  • I am creating ReactorClientHttpConnector for every request. and passing it to WebClient.create().clientConnector(ReactorClientHttpConnector ).baseURL("new URL ").build() – naval jain Dec 19 '18 at 12:03

1 Answers1

24

WebClient instances are meant to be reusable. The only reason you'd need to have different client instances is for specific needs: instrumentation for observability, specific authentication client filters, specific connection/read/write timeouts.

Different base URIs is not a strong reason to create different instances. It's perfectly fine to create an instance and not set a base URI, it's merely a convenience to avoid duplication when calling the same host over and over again.

This is perfectly fine:

WebClient webClient = WebClient.builder()
                               .build();

Mono<Resource> resource = webClient.get()
                                   .uri("http://example.org/resource")
                                   .retrieve()...;

Mono<Project> project = webClient.get()
                                 .uri("http://spring.io/projects/spring-boot")
                                 .retrieve()...;

Note that if you use Spring Boot, you should instead consider building your WebClient instance using the provided WebClient.Builder (see WebClient section for more details).

Manuel Jordan
  • 15,253
  • 21
  • 95
  • 158
Brian Clozel
  • 56,583
  • 15
  • 167
  • 176