Netty4:向不同的服务器发送多个请求
Netty4: Sending multiple request to different servers
我有一个客户端发送多个请求。每个请求都将发送到不同的服务器。因此,200 个请求发送到 200 个不同的服务器。
我为不同的连接创建了一个具有不同 bootstrap 的偶数循环组。
我应该为 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());
}
}
});
}
每个频道都连接到不同的端点和不同的服务器,这意味着您将需要不同的频道。
我有一个客户端发送多个请求。每个请求都将发送到不同的服务器。因此,200 个请求发送到 200 个不同的服务器。
我为不同的连接创建了一个具有不同 bootstrap 的偶数循环组。
我应该为 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());
}
}
});
}
每个频道都连接到不同的端点和不同的服务器,这意味着您将需要不同的频道。