问题描述
我使用下面的代码创建reactor netty http客户端并使用这个客户端发送请求.
I use bellow code to create reactor netty http client and use this client to send request.
ConnectionProvider connectionProvider = ConnectionProvider.builder("lead")
.maxConnections(10)
.pendingAcquireTimeout(Duration.ofSeconds(60))
.pendingAcquireMaxCount(10)
.maxLifeTime(Duration.ofSeconds(100))
.maxIdleTime(Duration.ofSeconds(60))
.build();
HttpClient httpClient = HttpClient.create(connectionProvider)
.keepAlive(true);
我循环发送请求:
for (; ; ) {
httpClient.get().uri("http://localhost:5230/test")
.response()
.subscribe();
}
我希望 http 客户端只创建 10 个到 http 服务器的连接,但结果不如预期,客户端创建了很多到 http 服务器的连接(服务器监听 5230 端口)(此连接很快关闭):
I hope http client only create 10 connection to http server,but the result not as expected,client create many connection to http server(server listen on 5230 port)(this connection soon closed):
netstat -nap |grep "5230";输出
netstat -nap |grep "5230" output
TCP 127.0.0.1:5230 0.0.0.0:0 LISTENING 1980
TCP 127.0.0.1:5230 127.0.0.1:51012 ESTABLISHED 1980
TCP 127.0.0.1:5230 127.0.0.1:51014 ESTABLISHED 1980
TCP 127.0.0.1:5230 127.0.0.1:51015 ESTABLISHED 1980
TCP 127.0.0.1:5230 127.0.0.1:51016 ESTABLISHED 1980
TCP 127.0.0.1:5230 127.0.0.1:51017 ESTABLISHED 1980
TCP 127.0.0.1:5230 127.0.0.1:51018 ESTABLISHED 1980
TCP 127.0.0.1:5230 127.0.0.1:51019 ESTABLISHED 1980
TCP 127.0.0.1:5230 127.0.0.1:51020 ESTABLISHED 1980
TCP 127.0.0.1:5230 127.0.0.1:51021 ESTABLISHED 1980
TCP 127.0.0.1:5230 127.0.0.1:51022 ESTABLISHED 1980
TCP 127.0.0.1:50393 127.0.0.1:5230 TIME_WAIT 0
TCP 127.0.0.1:50394 127.0.0.1:5230 TIME_WAIT 0
TCP 127.0.0.1:50395 127.0.0.1:5230 TIME_WAIT 0
TCP 127.0.0.1:50396 127.0.0.1:5230 TIME_WAIT 0
TCP 127.0.0.1:50397 127.0.0.1:5230 TIME_WAIT 0
TCP 127.0.0.1:50398 127.0.0.1:5230 TIME_WAIT 0
TCP 127.0.0.1:50399 127.0.0.1:5230 TIME_WAIT 0
TCP 127.0.0.1:50400 127.0.0.1:5230 TIME_WAIT 0
TCP 127.0.0.1:50401 127.0.0.1:5230 TIME_WAIT 0
.... there is many connection in TIME_WAIT status....
如何确保 http 客户端只创建 10 个到 http 服务器的连接?
How can i make sure http client only create 10 connection to http server?
版本:
jdk 1.8.0_201
reactory-netty 1.0.3
netty 4.15.9.Final
推荐答案
在 Violeta Georgieva 的帮助下,在反应堆 netty response()
方法中,http 客户端会关闭连接,所以 http 客户端会创建很多到服务器的连接:create connection ->发送请求 ->关闭连接.
With the help of Violeta Georgieva,in reactory netty response()
method will case http client close connection,so http client create many connection to server:create connection -> send request -> close connection。
以下代码按预期工作:
httpClient.get().uri("http://127.0.0.1:5230/test")
.responseSingle(new BiFunction<HttpClientResponse, ByteBufMono, Mono<String>>() {
@Override
public Mono<String> apply(HttpClientResponse response, ByteBufMono byteBufMono) {
return byteBufMono.asString();
}
})
.subscribe();
这篇关于如何确保 rector netty http 客户端创建到 http 服务器的修复连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!