当请求包含多个部分时,预请求过滤器(使用webclient进行REST调用)失败

时间:2020-03-08 23:48:28

标签: spring-cloud gateway spring-cloud-gateway reactor-netty spring-webclient

我有一个用例,对于某些请求,网关需要调用其余端点(中间服务),并从响应中复制标头,然后将请求添加到最终下游服务(最终服务)。 流量image

我在Spring Cloud Gateway中实现了针对它的预请求过滤器。

虽然对于正常的请求来说可以正常工作,但是当我尝试使用此流程上传文件并出现以下错误时,它就会失败:

2020-03-09 00:27:15.381 ERROR 32440 --- [ctor-http-nio-3] a.w.r.e.AbstractErrorWebExceptionHandler : [b5048ec0-5]  500 Server Error for HTTP POST "/upload"

reactor.netty.http.client.PrematureCloseException: Connection prematurely closed BEFORE response
    Suppressed: reactor.core.publisher.FluxOnAssembly$OnAssemblyException: 
Error has been observed at the following site(s):
    |_ checkpoint ⇢ org.springframework.cloud.gateway.filter.WeightCalculatorWebFilter [DefaultWebFilterChain]
    |_ checkpoint ⇢ HTTP POST "/upload" [ExceptionHandlingWebHandler]
Stack trace:

2020-03-09 00:11:38.247 ERROR 32440 --- [ctor-http-nio-3] a.w.r.e.AbstractErrorWebExceptionHandler : [b5048ec0-4]  500 Server Error for HTTP POST "/upload"

io.netty.handler.codec.EncoderException: java.lang.IllegalStateException: unexpected message type: PooledSlicedByteBuf
    at io.netty.handler.codec.MessageToMessageEncoder.write(MessageToMessageEncoder.java:107) ~[netty-codec-4.1.45.Final.jar:4.1.45.Final]
    Suppressed: reactor.core.publisher.FluxOnAssembly$OnAssemblyException: 
Error has been observed at the following site(s):
    |_ checkpoint ⇢ org.springframework.cloud.gateway.filter.WeightCalculatorWebFilter [DefaultWebFilterChain]
    |_ checkpoint ⇢ HTTP POST "/upload" [ExceptionHandlingWebHandler]
Stack trace:

样品 所有服务都使用2.2.5.RELEASE引导版本。

https://github.com/dhananjay12/spring-cloud/tree/master/spring-routing/spring-cloud-gateway-filters。 详细信息在README文件中给出。

网关application.yml:

spring:
  cloud:
    gateway:
      routes:
        - id: end-service
          uri: http://localhost:8090
          predicates:
            - Path=/**
          filters:
            - name: RequestSize
              args:
                maxSize: 5000000
      default-filters:
        - MiddleServiceFilter
app:
  middle-service-url: http://localhost:8085/check

如果我删除过滤器,一切正常。通过WebClient.builder()WebClient.builder().clientConnector(new ReactorClientHttpConnector( HttpClient.newConnection().compress(true)))之类的多种方式尝试了Webclient初始化,但是没有运气。

过滤器是否需要做一些特别的事情?

0 个答案:

没有答案