Springboot。响应式Web客户端。响应之前连接过早关闭

时间:2018-12-25 11:06:12

标签: spring spring-webflux project-reactor reactor-netty

我遇到了这个问题

  

池连接发现错误   react.netty.http.client.HttpClientOperations $ PrematureCloseException:   连接在响应之前过早关闭。”

我正在通过反应式Web客户端从石墨服务器收集请求的时间范围内的指标(以减少通过http传输的数据量,我将工作日分为24/4个部分),然后将响应合并到一个矩阵中并将其保存到csv文件->合并到另一个。 当天数增加时会出现问题(2或3可以正常工作,但是更多的天会出现更多因关闭连接而引起的错误)。尝试使用延迟,这会有所帮助,但又可以处理一天而不出错。

堆栈跟踪: ClosedConnectionStacktrace

发现了类似https://github.com/reactor/reactor-netty/issues/413的问题,但不确定。

以下是代码段:

discoveryMono.thenReturn(true) // discover metrics
            .flux()
            .flatMap(m -> Flux.fromIterable(dates) // process all days
                    .delayElements(Duration.ofSeconds(1L))
                    .flatMap(date -> Flux.range(0, 24 / intervalHours) // divide day into chunks
                            .delayElements(Duration.of(100L, ChronoUnit.MILLIS))
                            .flatMap(timeFraction -> Flux.fromIterable(sequentialTasks) // task to invoke webclient
                                    .flatMap(task -> {
                                        Instant from = date.plus(timeFraction * intervalHours, ChronoUnit.HOURS);
                                        Instant until = from.plus(intervalHours, ChronoUnit.HOURS);
                                        TaskParams taskParams = new TaskParams(itSystem, from, until, TaskParams.PollingType.FULLDAY);
                                        log.trace("workflow | from={}, until={}", from, until);
                                        return task.apply(taskParams)
    //                                            .doOnNext(m -> log.trace("Matrix: {}", m))
                                                .onErrorResume(err -> {
                                                    log.error("processFullDaysInChunks | Error: {}", err);
                                                    return Mono.empty();
                                                });
                                    }).flatMap(params -> Flux.fromIterable(fileTasks) // tasks to check/merge files, doesn't matter
                                            .flatMap(fileTask -> parTask.apply(params)
                                                    .onErrorResume(err -> {
                                                        log.error("processFullDaysInChunks | Error: {}", err);
                                                        return Mono.empty();
                                                    })
                                            )
                                    )
                            )
                    )
            ).subscribeOn(fullDayScheduler).subscribe();

和使用网络客户端调用的任务的一部分:

    private Flux<GraphiteResultDTO> getGraphiteResults(ITSystem itSystem, Instant from, Instant until) {
        String fromStr = FROM_PARAMETER + Long.valueOf(from.getEpochSecond()).toString();
        String untilStr = UNTIL_PARAMETER + Long.valueOf(until.getEpochSecond()).toString();
        String uri = RENDER_URI + TARGET_PARAMETER + "{targetParam}" + fromStr + untilStr + FORMAT_JSON_PARAMETER;
        WebClient webClient = getGraphiteWebClient(itSystem.getDataSource());
        Set<String> targetParams = storage.getValueByITSystemId(itSystem.getId()).getSecond();
        Flux<GraphiteResultDTO> result = Flux.fromIterable(targetParams)
                .delayElements(Duration.of(10, ChronoUnit.MILLIS))
                .flatMap(targetParam -> {
                    Map<String, String> params = Map.ofEntries(entry("targetParam", targetParam));
                    if (log.isTraceEnabled()) {
                        log.trace("getGraphiteResults | Uri={}, TargetPatam: {}", uri, targetParam);
                    }
                    return webClient.get()
                            .uri(uri, params)
                            .retrieve()
                            .onStatus(HttpStatus::isError, clientResponse -> {
                                log.trace("clientResponse | transforming body");
                                clientResponse.bodyToMono(String.class)
                                        .doOnNext(errorString -> log.error("retrieve(), error={}", errorString));
//                                                    .flatMap(s -> Flux.error(clientResponse.bodyToFlux(WebClientException.class)));
                                return Mono.empty();
                            })
                            .bodyToFlux(GraphiteResultDTO.class)
                            .onErrorResume(throwable -> {
                                log.error("webclient | bodyToFlux error={}", throwable.getMessage());
                                return Flux.empty();
                            });
                });
        return result;
    }

1 个答案:

答案 0 :(得分:0)

解决了我的问题,即用预取为1的concatMap替换了flatMap运算符并限制了速率(limitRate运算符)。现在,所有请求均按顺序逐个处理。因此,现在无需使用时间延迟。