如何在新的 Springboot 2.1.4.Release 中禁用 Webclient 中的连接池?

Vis*_*swa 5 spring-boot reactor-netty

我正在使用 springboot webclient 从远程服务器调用 rest api。第一个请求工作正常。如果我在一段时间后发出后续请求,服务器会抛出 500 服务器错误。我得到的错误是“onError(java.io.IOException:一个现有的连接被远程主机强行关闭)”。

我想通过禁用连接池来测试行为,因为我相信它使用以前的连接。你能帮我在创建 webclient 时如何禁用连接池吗?

TcpClient tcpClient = TcpClient.create()
        .option(ChannelOption.CONNECT_TIMEOUT_MILLIS, 30000)
        .option(ChannelOption.SO_KEEPALIVE, false)
        .doOnConnected(connection ->
                connection.addHandlerLast(new ReadTimeoutHandler(30))
                        .addHandlerLast(new WriteTimeoutHandler(30))
        );

ReactorClientHttpConnector httpConnector = new ReactorClientHttpConnector(HttpClient.from(tcpClient));

final WebClient webClient = WebClient
        .builder()
        .clientConnector(httpConnector)
        .baseUrl("http://customer.service.api.internal.cloud.qa.intranet.pagseguro.uol")
        .exchangeStrategies(strategies)
        .build()
Run Code Online (Sandbox Code Playgroud)

Vio*_*eva 8

您可以使用以下代码禁用连接池

TcpClient tcpClient = TcpClient.newConnection()
Run Code Online (Sandbox Code Playgroud)

使用wiretap(true)来检查服务器和客户机之间的流量。

您还可以为 Reactor Netty 启用日志记录并跟踪连接是否被重用(在连接池场景中)

logging.level.reactor.netty=debug
Run Code Online (Sandbox Code Playgroud)

如果连接被重用,你会看到

2019-04-11 18:52:10.049 DEBUG 98105 --- [ctor-http-nio-5] r.n.resources.PooledConnectionProvider   : [id: 0x897584fa, L:/<IP>:<PORT> - R:/<IP>:<PORT>] Channel acquired, now 1 active connections and 0 inactive connections

Run Code Online (Sandbox Code Playgroud)

还可以使用频道 IDid: 0x897584fa跟踪频道发生的情况。