Spring 5 Webflux相互依赖的WebClient调用没有超时

时间:2019-06-25 08:13:01

标签: java spring spring-webflux spring-webclient

我有两个相互依赖的对不同api的webclient调用,当第一个webclient调用响应被延迟时,则出现readtimeout异常。但是,只要第一个呼叫成功并且第二个呼叫响应被延迟,它就会无限期地等待响应。

我尝试为每个调用创建单独的webclient实例。仍然存在。

HttpClient httpClient = HttpClient.create()
            .tcpConfiguration(client ->

                    client.doOnConnected(conn -> conn
                            .addHandlerLast(new ReadTimeoutHandler(10))
                            .addHandlerLast(new WriteTimeoutHandler(10))));


    return WebClient.builder().baseUrl(url).clientConnector(new ReactorClientHttpConnector(httpClient))
            .exchangeStrategies(ExchangeStrategies.withDefaults())
            .defaultHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_UTF8_VALUE)
            .filter(ExchangeFilterFunctions
                    .basicAuthentication(", "))
            .build();

下面有两个后续调用,其中,当第二个调用延迟响应时,不会引发readtimeout异常

request = request.flatMap(req ->
                tempService.getId(loggedInUser, token)
      .map(response -> {
                            req.setRetrieveClientIdentifier(response.getId());
                            return seRequest;
                        }))

                .zipWhen(request -> tempService.getIdFor(request.getIdentifier(), accountToken)).map(tuple -> {

                    tuple.getT1().setID(tuple.getT2().getId());
                    return tuple.getT1();
                });

0 个答案:

没有答案