如何确保rector netty http client创建到http服务器的修复连接

lpwwtiir  于 2021-07-14  发布在  Java
关注(0)|答案(1)|浏览(548)

我使用下面的代码创建reactor netty http客户机,并使用此客户机发送请求。

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端口上侦听)(此连接很快关闭):
netstat-nap | grep“5230”输出

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客户端只创建到http服务器的10个连接?
版本:

jdk 1.8.0_201
reactory-netty 1.0.3
netty 4.15.9.Final
n3ipq98p

n3ipq98p1#

在violeta georgieva的帮助下 response() 方法将设置http客户端关闭连接的情况,因此http客户端创建许多到服务器的连接:创建连接->发送请求->关闭连接
以下代码按预期工作:

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();

相关问题