如何在Springboot2.6.2中禁用Webclient中的连接池



如何在Springboot 2.6.2中禁用Webclient中的连接池?

下面是我的示例代码。对吗?(WebClientBuilder一直作为单例使用。(

public class WebClientBuilder {
private static WebClient webClient;
public static WebClient getWebClient() throws Exception {
LOGGER.debug("getWebClient");
if (webClient != null) {
LOGGER.debug("webClient exist");
return webClient;
}
HttpClient httpClient = HttpClient.create().newConnection()
.option(ChannelOption.SO_KEEPALIVE, true);
ReactorClientHttpConnector reactorClientHttpConnector = new ReactorClientHttpConnector(httpClient);
LOGGER.debug("webClient was generated");
webClient = WebClient.builder().clientConnector(reactorClientHttpConnector).build();

return webClient;
}
}

webClient实例使用的代码如下,

...
return WebClientBuilder
.getWebClient()
.post()
...

不确定为什么要这样做,但可以使用ConnectionProvider.maxConnections控制池中的连接数

ConnectionProvider provider = ConnectionProvider.builder("http")
.maxConnections(1)
.build();
HttpClient httpClient = HttpClient.create(provider);
WebClient webClient = WebClient.builder()
.clientConnector(new ReactorClientHttpConnector(httpClient))
.build();

最新更新