I am using Spring boot Webflux 2.4.4 (latest) and trying to invoke a backend URL using WebClient. WebClient always responds above 20 secs. If I hit the URL directly it responds in milliseconds.
Pom Looks below:
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-webflux</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.projectreactor</groupId>
<artifactId>reactor-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
Code looks as Below:
@RestController
public class Controller {
@GetMapping("/test")
public Mono<String> test() {
long startMillis = System.currentTimeMillis();
return webClient().get().uri("https://www.google.com").exchangeToMono(response -> {
System.out.println("Elapsed Time is: " + (System.currentTimeMillis() - startMillis));
if (response.statusCode().equals(HttpStatus.OK)) {
return Mono.just("OK");
}
return Mono.just("OK");
});
}
private WebClient webClient() {
return WebClient.builder()
.clientConnector(new ReactorClientHttpConnector(HttpClient
.create().secure().responseTimeout(Duration.ofMillis(1000))
.option(ChannelOption.CONNECT_TIMEOUT_MILLIS, 3000).doOnConnected(c -> c
.addHandlerLast(new ReadTimeoutHandler(3)).addHandlerLast(new WriteTimeoutHandler(3)))))
.build();
}
}
- The timeouts which I have configured seem no effect.
- Why this takes 20 seconds to respond always.
Kindly suggest what I am missing or doing wrong.