Spring Cloud Gateway 自定义过滤器:WebClient.create().post() 在测试时导致挂起
Spring Cloud Gateway Custom Filter : WebClient.create().post() causes hanging when testing
所以我创建了一个自定义过滤器,当访问它时,它会创建一个 webflux 客户端和 post 到预先确定的 url。这在 运行 时似乎工作正常,但在测试此代码时测试挂起(直到我取消测试)。所以我觉得除了无法完成测试以确保这条路线正常工作之外,还可能存在内存泄漏。如果我将 WebClient
方法切换为 get()
,则过滤器的结果测试工作正常。带有 post()
的东西我不确定缺少什么。
@Component
class ProxyGatewayFilterFactory: AbstractGatewayFilterFactory<ProxyGatewayFilterFactory.Params>(Params::class.java) {
override fun apply(params: Params): GatewayFilter {
return OrderedGatewayFilter(
GatewayFilter { exchange, chain ->
exchange.request.mutate().header("test","test1").build()
WebClient.create().post()
.uri(params.proxyBasePath)
.body(BodyInserters.fromDataBuffers(exchange.request.body))
.headers { it.addAll(exchange.request.headers) }
.exchange()
.flatMap {
println("the POST statusCode is "+it.statusCode())
Mono.just(it.statusCode().is2xxSuccessful)
}
.map {
exchange.request.mutate().header("test", "test2").build()
println("exchange request uri is " + exchange.request.uri)
println("exchange response statusCode is "+ exchange.response.statusCode)
exchange
}
.flatMap(chain::filter)
}, params.order)
}
取自文档,如果使用 exchange
,您有义务使用 body。
Unlike retrieve(), when using exchange(), it is the responsibility of the application to consume any response content regardless of the scenario (success, error, unexpected data, etc). Not doing so can cause a memory leak. The Javadoc for ClientResponse lists all the available options for consuming the body. Generally prefer using retrieve() unless you have a good reason for using exchange() which does allow to check the response status and headers before deciding how to or if to consume the response.
Spring framework 5.2.9 Webclient
这个api已经在最新版本的spring框架5.3.0中改变了现在spring会强制你消费body,因为开发者没有'实际阅读文档。
所以我创建了一个自定义过滤器,当访问它时,它会创建一个 webflux 客户端和 post 到预先确定的 url。这在 运行 时似乎工作正常,但在测试此代码时测试挂起(直到我取消测试)。所以我觉得除了无法完成测试以确保这条路线正常工作之外,还可能存在内存泄漏。如果我将 WebClient
方法切换为 get()
,则过滤器的结果测试工作正常。带有 post()
的东西我不确定缺少什么。
@Component
class ProxyGatewayFilterFactory: AbstractGatewayFilterFactory<ProxyGatewayFilterFactory.Params>(Params::class.java) {
override fun apply(params: Params): GatewayFilter {
return OrderedGatewayFilter(
GatewayFilter { exchange, chain ->
exchange.request.mutate().header("test","test1").build()
WebClient.create().post()
.uri(params.proxyBasePath)
.body(BodyInserters.fromDataBuffers(exchange.request.body))
.headers { it.addAll(exchange.request.headers) }
.exchange()
.flatMap {
println("the POST statusCode is "+it.statusCode())
Mono.just(it.statusCode().is2xxSuccessful)
}
.map {
exchange.request.mutate().header("test", "test2").build()
println("exchange request uri is " + exchange.request.uri)
println("exchange response statusCode is "+ exchange.response.statusCode)
exchange
}
.flatMap(chain::filter)
}, params.order)
}
取自文档,如果使用 exchange
,您有义务使用 body。
Unlike retrieve(), when using exchange(), it is the responsibility of the application to consume any response content regardless of the scenario (success, error, unexpected data, etc). Not doing so can cause a memory leak. The Javadoc for ClientResponse lists all the available options for consuming the body. Generally prefer using retrieve() unless you have a good reason for using exchange() which does allow to check the response status and headers before deciding how to or if to consume the response.
Spring framework 5.2.9 Webclient
这个api已经在最新版本的spring框架5.3.0中改变了现在spring会强制你消费body,因为开发者没有'实际阅读文档。