从一个微服务到另一个微服务的WebClient构建器调用首次在Webflux中给出了错误的请求错误

时间:2020-02-17 16:38:12

标签: spring-boot webclient spring-webflow spring-webclient spring-reactive

在我们的项目中,我们正在使用WebClient Builder调用在微服务之间进行通信。它是基于springboot-thymleaf-webflux的应用程序,到目前为止一切都运行良好,但是客户端要求将GET调用更改为POST调用。 thymleaf控制器。在UI和后端进行了这些更改之后,Thymleaf调用可以正常工作,但是XHR之一(即来自UI的后调用)给Web客户端调用第二次微服务的第一次请求和第二次单击带来了错误的请求错误。比较标头和两次单击请求没有任何区别。我无法理解为什么webclient第一次表现异常,而第二次表现良好。下面是用于webclient调用的代码段,它通过说出http://second-microservice -eureka-address / endpoint-url

的BAD请求而路由到WebclientResponseException而不到达第二个微服务
     * Submitting xyz 
     * @param submitFlowRequest
     */
    @Override
    public Mono<ApiResponse<SubmitResponse>> submitFlow(SubmitFlowRequest submitFlowRequest,
            Map<String, String> headers) {
        long startTime = System.currentTimeMillis();
        String uri = propertyConfig.getAggregationService()
                + propertyConfig.getAggregationSubmitCCInfoURL();
        DefaultUriBuilderFactory factory = new DefaultUriBuilderFactory(uri);
        factory.setEncodingMode(DefaultUriBuilderFactory.EncodingMode.URI_COMPONENT);
        MultiValueMap<String, String> clientHeaders = buildHeaders(headers);
        return webClientBuilder.uriBuilderFactory(factory).build().post()
                .headers(httpHeaders -> httpHeaders.addAll(clientHeaders)).accept(MediaType.APPLICATION_JSON)
                .syncBody(submitFlowRequest).retrieve().bodyToMono(ApiResponse.class)
                .onErrorMap(ConnectException.class,
                        error -> new VzwRuntimeException(ErrorCodeEnum.V404.toString(), Constants.OPP_TC_SYSTEM_ERROR,
                                (Constants.CONNECTION_FAILURE_TEXT + Constants.AGGREGATION)))
                .onErrorMap(WebClientResponseException.class,
                        error -> new VzwRuntimeException(ErrorCodeEnum.V404.toString(), Constants.OPP_TC_SYSTEM_ERROR,
                                (Constants.CONNECTION_FAILURE_TEXT + Constants.AGGREGATION)))
                .flatMap(res -> {
                    Audit apiAudit = Audit.builder().apiUrl(uri).request(LoggerUtil.asJson(submitFlowRequest))
                            .response(LoggerUtil.asJson(res))
                            .executionTime(String.valueOf(System.currentTimeMillis() - startTime))
                            .headers(LoggerUtil.asJson(clientHeaders)).transactionType(res.getData()!=null?mapper.map(res.getData(), SubmitResponse.class).getTransactionType():"").build();
                    LoggerUtil.logExternalApiCalls(apiAudit);
                    return Mono.just((ApiResponse<SubmitResponse>) res);
                });
    }```

Please provide me some lead.I am tired of finding solution for this.

1 个答案:

答案 0 :(得分:0)

这是Netty的一个持续存在的问题。 Netty持有一些状态为HttpObjectEncoder的连接。当客户端从池中获得此类连接并将其用于向目标服务器发出请求时,该连接将立即失败,因为HttpObjectEncoder无法编码DefaultHttpRequest。 禁用连接池可以解决问题。