netty4:向不同的服务器发送多个请求

xxhby3vn  于 2021-07-09  发布在  Java
关注(0)|答案(1)|浏览(395)

我有一个客户端正在发送多个请求。每个请求都将发送到不同的服务器。所以,200个请求被发送到200个不同的服务器。
我已经为不同的连接创建了一个偶数循环组,它具有不同的引导。
我应该使用200个通道处理200个请求还是使用一个通道。下面是我的代码,我现在使用的是单通道:

public HttpClientDemo(int serverPort)
    {
        this.serverPort = serverPort;
        this.pipelineFactory = new HTTPClientInitializer();
        this.workerGroup =  new NioEventLoopGroup();
    }

    public void connect(String address, int timeout) {

            connectAsync(address).syncUninterruptibly();
    }   

    private ChannelFuture connectAsync(final String address)
    {
        return new Bootstrap()
                .group(workerGroup)
                .channel(NioSocketChannel.class)
                .handler(pipelineFactory).connect(address,serverPort).addListener(new ChannelFutureListener() {
                    @Override
                    public void operationComplete(ChannelFuture future) {
                        if(future.isSuccess())
                        {
                            log.info("Client is able to connect to: " + address);
                        }
                        else
                        {
                            log.error("Client not able to connect to: " + address + " " +  future.cause());
                        }
                    }
                });
    }
xmakbtuz

xmakbtuz1#

每个通道都连接到不同的端点和不同的服务器,这意味着您将需要不同的通道。

相关问题