首页 > 解决方案 > (Reactor Netty) ConnectionProvider 每次创建新连接

问题描述

我是 Reactor Netty 的新手,并试图通过使用连接池来实现 TCP 连接。

我按照https://projectreactor.io/docs/netty/snapshot/reference/index.html#_connection_pool上的步骤进行操作

@PostConstruct
    public void init() {
    connectionProvider = ConnectionProvider.builder("provider")
            .maxConnections(16)
            .maxIdleTime(Duration.ofSeconds(20))
            .maxLifeTime(Duration.ofSeconds(60))
            .build();
}

public void sendTcp(String log, TargetServer targetServer) {


    TcpClient tcpClient = TcpClient
            .create(connectionProvider)
            .host(targetServer.getIp())
            .port(targetServer.getPort())
            .doOnConnect(conn -> {
            })
            .doOnConnected(conn -> {
            })
            .doOnDisconnected(conn -> {
            })
            .doOnChannelInit(((connectionObserver, channel, socketAddress) -> {
                channel.pipeline().addFirst(new TcpTransmissionHandler());
            }))
            .handle((in, out) -> in.receive().then());

    Connection connection = tcpClient.connectNow();
System.out.println("connection : " + connection);
    connection
            .outbound()
            .sendString(Mono.just(log), CharsetUtil.UTF_8)
            .then()
            .subscribe(null, null, connection::dispose);

}

sendTcp() 方法每秒触发一次,我打印每个 Connection 对象。

我将 maxConnections 选项设置为 16,所以我预计连接对象有 16 个不同的 id。但是,每次我打印连接对象时,它都有不同的 id,我猜这个连接不是来自连接池。谁能给我一些提示?

 connection : ChannelOperations{PooledConnection{channel=[id: 0x99628e23, 
L:/127.0.0.1:60717 - R:/127.0.0.1:10005]}}
    connection : ChannelOperations{PooledConnection{channel=[id: 0x04d206d7, L:/127.0.0.1:60718 - R:/127.0.0.1:10005]}}
    connection : ChannelOperations{PooledConnection{channel=[id: 0xddf9418f, L:/127.0.0.1:60719 - R:/127.0.0.1:10005]}}
    connection : ChannelOperations{PooledConnection{channel=[id: 0x820cf497, L:/127.0.0.1:60720 - R:/127.0.0.1:10005]}}
    connection : ChannelOperations{PooledConnection{channel=[id: 0xf28284c9, L:/127.0.0.1:60721 - R:/127.0.0.1:10005]}}
    connection : ChannelOperations{PooledConnection{channel=[id: 0xc4e10ce6, L:/127.0.0.1:60722 - R:/127.0.0.1:10005]}}
    connection : ChannelOperations{PooledConnection{channel=[id: 0xde2342ee, L:/127.0.0.1:60723 - R:/127.0.0.1:10005]}}
    connection : ChannelOperations{PooledConnection{channel=[id: 0x27a8f802, L:/127.0.0.1:60724 - R:/127.0.0.1:10005]}}
    connection : ChannelOperations{PooledConnection{channel=[id: 0x1e4041c9, L:/127.0.0.1:60725 - R:/127.0.0.1:10005]}}
    connection : ChannelOperations{PooledConnection{channel=[id: 0xb2f6a891, L:/127.0.0.1:60726 - R:/127.0.0.1:10005]}}
    connection : ChannelOperations{PooledConnection{channel=[id: 0x6b659a54, L:/127.0.0.1:60727 - R:/127.0.0.1:10005]}}
    connection : ChannelOperations{PooledConnection{channel=[id: 0xdc986de4, L:/127.0.0.1:60728 - R:/127.0.0.1:10005]}}
    connection : ChannelOperations{PooledConnection{channel=[id: 0xf0d572b4, L:/127.0.0.1:60729 - R:/127.0.0.1:10005]}}
    connection : ChannelOperations{PooledConnection{channel=[id: 0x0fe9084a, L:/127.0.0.1:60730 - R:/127.0.0.1:10005]}}
    connection : ChannelOperations{PooledConnection{channel=[id: 0xac19696a, L:/127.0.0.1:60731 - R:/127.0.0.1:10005]}}
    connection : ChannelOperations{PooledConnection{channel=[id: 0x6aa917a9, L:/127.0.0.1:60732 - R:/127.0.0.1:10005]}}
    connection : ChannelOperations{PooledConnection{channel=[id: 0xa17fbcb0, L:/127.0.0.1:60733 - R:/127.0.0.1:10005]}}
    connection : ChannelOperations{PooledConnection{channel=[id: 0xc8425b37, L:/127.0.0.1:60734 - R:/127.0.0.1:10005]}}
    connection : ChannelOperations{PooledConnection{channel=[id: 0x8045141b, L:/127.0.0.1:60735 - R:/127.0.0.1:10005]}}
    connection : ChannelOperations{PooledConnection{channel=[id: 0x0e6ccf6e, L:/127.0.0.1:60736 - R:/127.0.0.1:10005]}}
    connection : ChannelOperations{PooledConnection{channel=[id: 0x87d9de2e, L:/127.0.0.1:60737 - R:/127.0.0.1:10005]}}
    connection : ChannelOperations{PooledConnection{channel=[id: 0x8307fbd4, L:/127.0.0.1:60738 - R:/127.0.0.1:10005]}}
    connection : ChannelOperations{PooledConnection{channel=[id: 0x4edf0c70, L:/127.0.0.1:60739 - R:/127.0.0.1:10005]}}
    connection : ChannelOperations{PooledConnection{channel=[id: 0x612051e7, L:/127.0.0.1:60740 - R:/127.0.0.1:10005]}}
    connection : ChannelOperations{PooledConnection{channel=[id: 0x32e3a756, L:/127.0.0.1:60741 - R:/127.0.0.1:10005]}}
    connection : ChannelOperations{PooledConnection{channel=[id: 0xab251fef, L:/127.0.0.1:60742 - R:/127.0.0.1:10005]}}
    connection : ChannelOperations{PooledConnection{channel=[id: 0xc48a33ad, L:/127.0.0.1:60743 - R:/127.0.0.1:10005]}}
    connection : ChannelOperations{PooledConnection{channel=[id: 0x65c698aa, L:/127.0.0.1:60744 - R:/127.0.0.1:10005]}}
    connection : ChannelOperations{PooledConnection{channel=[id: 0x18c10a31, L:/127.0.0.1:60745 - R:/127.0.0.1:10005]}}
    connection : ChannelOperations{PooledConnection{channel=[id: 0x305053bb, L:/127.0.0.1:60746 - R:/127.0.0.1:10005]}}
    connection : ChannelOperations{PooledConnection{channel=[id: 0x11b5cfea, L:/127.0.0.1:60747 - R:/127.0.0.1:10005]}}
    connection : ChannelOperations{PooledConnection{channel=[id: 0x7ca4cc38, L:/127.0.0.1:60748 - R:/127.0.0.1:10005]}}
    connection : ChannelOperations{PooledConnection{channel=[id: 0xf6c8fe31, L:/127.0.0.1:60749 - R:/127.0.0.1:10005]}}

标签: reactor-netty

解决方案


推荐阅读