小编pri*_*pri的帖子

为什么 WebFlux-WebClient 超时不起作用?

我正在使用 Spring boot Webflux 2.4.4(最新)并尝试使用 WebClient 调用后端 URL。WebClient 总是响应超过 20 秒。如果我直接点击 URL,它会以毫秒为单位响应。

Pom 看起来如下:

<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>
Run Code Online (Sandbox Code Playgroud)

代码如下所示:

@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 …
Run Code Online (Sandbox Code Playgroud)

java netty spring-boot spring-webflux spring-webclient

2
推荐指数
1
解决办法
324
查看次数