Spring Web Flux - WebClient - exchange - block - 如何释放连接到连接池?
Spring Web Flux - WebClient - exchange - block - how to release the connection to the connection pool?
当前:
我正在使用 spring-webflux-5.2.8.RELEASE
并且正在运行 "fine":
httpStatus = webClient
.post()
.uri(someUri)
.headers(someHeaders)
.bodyValue(someBody)
.exchange()
.map(ClientResponse::statusCode)
.timeout(someTimeout)
.doOnError(doSomething())
.onErrorResume(ex -> Mono.empty())
.block();
问题:
当返回 错误 时,没有问题,因为连接已被破坏并且不会放回连接池中:
DEBUG r.n.resources.PooledConnectionProvider - [id: 0xa23f78ad,
L:/127.0.0.1:7524 ! R:localhost/127.0.0.1:8443] Channel closed, now 0
active connections and 0 inactive connections
但是当我得到一个成功的响应时,下一个post将fail/timeout:
java.util.concurrent.TimeoutException: Did not observe any item or
terminal signal within 10000ms in 'map' (and no fallback has been
configured)
因为我需要进行故障排除,所以我使用了一个只有 1 个连接的修复连接池,如下所示:
@Bean
public WebClient.Builder webClientBuilder(){
HttpClient httpClient = HttpClient.create(ConnectionProvider.create("pool", 1));
return WebClient.builder()
.clientConnector(new ReactorClientHttpConnector(httpClient));
}
我猜(我可能是错的,因为我对 webclient 和反应世界是全新的)问题是在成功获得响应,我认为这是由于 this.
When using a ClientResponse through the WebClient exchange() method,
you have to make sure that the body is consumed or released...
已经尝试过的:
我试着像这样做一个 releaseBody()(但这不起作用,因为下一个 post 仍然失败):
.map(clientResponse -> {
HttpStatus statusCode = clientResponse.statusCode();
clientResponse.releaseBody();
return statusCode;
})
P.S 我需要使用 block()。这无法更改,因为我需要响应才能继续,我只需要获取状态代码。我正在使用 WebClient,因为我在某处读到 rest 模板将被弃用,取而代之的是 ... WebClient。希望有人能帮忙。
更新 1:
我启用了指标,但确实没有释放连接:
reactor_netty_connection_provider_fixedPool_total_connections{id="1591603494",remote_address="localhost:8443",}
1.0 reactor_netty_connection_provider_fixedPool_active_connections{id="1591603494",remote_address="localhost:8443",}
1.0
更新 2
找到这个:https://github.com/spring-projects/spring-framework/issues/20474
!
更新 3
我尝试使用默认的连接数(即 500),我注意到活动连接数在每次 post 之后不断增加:- (
[reactor-http-nio-2] DEBUG r.n.resources.PooledConnectionProvider -
[id: 0x2316e048, L:/127.0.0.1:32787 - R:localhost/127.0.0.1:8443]
Channel connected, now 7 active connections and 0 inactive connections
这对我有用:
HttpStatus httpStatus = null;
Mono<HttpStatus> monoHttpStatus = null;
:
WebClient webClient = xxx.getWebClient();
:
try {
monoHttpStatus = webClient
.post()
.uri(someUri)
.headers(someHeaders)
.bodyValue(someBody)
.exchange()
.map(clientResponse ->
clientResponse.releaseBody().thenReturn(clientResponse.statusCode()))
.timeout(someTimeout)
.doOnError(Exception.class, e -> logger.error("An exception has occurred: ", e))
.onErrorResume(ex -> Mono.empty())
.block();
if(monoHttpStatus != null)
httpStatus = monoHttpStatus.block();
}
:
//SomeOtherClass
@Autowired
private WebClient.Builder webClientBuilder;
public WebClient getWebClient() {
:
webClient= webClientBuilder.baseUrl(baseUrl)
.build();
:
return webClient;
}
我现在在 post 完成后看到这个:
DEBUG reactor.util.Loggers$Slf4JLogger.debug[249] [reactor-http-nio-1]
[id: 0x6b0568a3, L:/127.0.0.1:10168 - R:localhost/127.0.0.1:8443]
Releasing channel
DEBUG reactor.util.Loggers$Slf4JLogger.debug[254]
[reactor-http-nio-1] [id: 0x6b0568a3, L:/127.0.0.1:10168 -
R:localhost/127.0.0.1:8443] Channel cleaned, now 0 active connections
and 1 inactive connections
当前:
我正在使用 spring-webflux-5.2.8.RELEASE
并且正在运行 "fine":
httpStatus = webClient
.post()
.uri(someUri)
.headers(someHeaders)
.bodyValue(someBody)
.exchange()
.map(ClientResponse::statusCode)
.timeout(someTimeout)
.doOnError(doSomething())
.onErrorResume(ex -> Mono.empty())
.block();
问题:
当返回 错误 时,没有问题,因为连接已被破坏并且不会放回连接池中:
DEBUG r.n.resources.PooledConnectionProvider - [id: 0xa23f78ad, L:/127.0.0.1:7524 ! R:localhost/127.0.0.1:8443] Channel closed, now 0 active connections and 0 inactive connections
但是当我得到一个成功的响应时,下一个post将fail/timeout:
java.util.concurrent.TimeoutException: Did not observe any item or terminal signal within 10000ms in 'map' (and no fallback has been configured)
因为我需要进行故障排除,所以我使用了一个只有 1 个连接的修复连接池,如下所示:
@Bean
public WebClient.Builder webClientBuilder(){
HttpClient httpClient = HttpClient.create(ConnectionProvider.create("pool", 1));
return WebClient.builder()
.clientConnector(new ReactorClientHttpConnector(httpClient));
}
我猜(我可能是错的,因为我对 webclient 和反应世界是全新的)问题是在成功获得响应,我认为这是由于 this.
When using a ClientResponse through the WebClient exchange() method, you have to make sure that the body is consumed or released...
已经尝试过的:
我试着像这样做一个 releaseBody()(但这不起作用,因为下一个 post 仍然失败):
.map(clientResponse -> {
HttpStatus statusCode = clientResponse.statusCode();
clientResponse.releaseBody();
return statusCode;
})
P.S 我需要使用 block()。这无法更改,因为我需要响应才能继续,我只需要获取状态代码。我正在使用 WebClient,因为我在某处读到 rest 模板将被弃用,取而代之的是 ... WebClient。希望有人能帮忙。
更新 1:
我启用了指标,但确实没有释放连接:
reactor_netty_connection_provider_fixedPool_total_connections{id="1591603494",remote_address="localhost:8443",} 1.0 reactor_netty_connection_provider_fixedPool_active_connections{id="1591603494",remote_address="localhost:8443",} 1.0
更新 2
找到这个:https://github.com/spring-projects/spring-framework/issues/20474
!
更新 3
我尝试使用默认的连接数(即 500),我注意到活动连接数在每次 post 之后不断增加:- (
[reactor-http-nio-2] DEBUG r.n.resources.PooledConnectionProvider - [id: 0x2316e048, L:/127.0.0.1:32787 - R:localhost/127.0.0.1:8443] Channel connected, now 7 active connections and 0 inactive connections
这对我有用:
HttpStatus httpStatus = null;
Mono<HttpStatus> monoHttpStatus = null;
:
WebClient webClient = xxx.getWebClient();
:
try {
monoHttpStatus = webClient
.post()
.uri(someUri)
.headers(someHeaders)
.bodyValue(someBody)
.exchange()
.map(clientResponse ->
clientResponse.releaseBody().thenReturn(clientResponse.statusCode()))
.timeout(someTimeout)
.doOnError(Exception.class, e -> logger.error("An exception has occurred: ", e))
.onErrorResume(ex -> Mono.empty())
.block();
if(monoHttpStatus != null)
httpStatus = monoHttpStatus.block();
}
:
//SomeOtherClass
@Autowired
private WebClient.Builder webClientBuilder;
public WebClient getWebClient() {
:
webClient= webClientBuilder.baseUrl(baseUrl)
.build();
:
return webClient;
}
我现在在 post 完成后看到这个:
DEBUG reactor.util.Loggers$Slf4JLogger.debug[249] [reactor-http-nio-1] [id: 0x6b0568a3, L:/127.0.0.1:10168 - R:localhost/127.0.0.1:8443] Releasing channel
DEBUG reactor.util.Loggers$Slf4JLogger.debug[254] [reactor-http-nio-1] [id: 0x6b0568a3, L:/127.0.0.1:10168 - R:localhost/127.0.0.1:8443] Channel cleaned, now 0 active connections and 1 inactive connections