与Netty和NIO高度并发的HTTP

我正在使用示例Netty HTTP客户端代码 ,以便在并发的线程环境中发出http请求。

但是,我的系统在相当低的吞吐量下完全破坏(有一些例外)。

在几乎伪代码中:

ClientBootstrap bootstrap = new ClientBootstrap(new NioClientSocketChannelFactory()) bootstrap.setPipelineFactory(new HttpClientPipelineFactory()); ChannelFuture future = bootstrap.connect(new InetSocketAddress(host, port)); Channel channel = future.awaitUninterruptibly().getChannel(); HttpRequest request = new DefaultHttpRequest(); channel.write(request); 

在示例中,为了发出请求,我创建了一个ClientBootstrap,并从那里(通过一些箍)一个Channel来编写HTTPRequest。

这一切都有效,也很好。

但是,在同时发生的情况下,每个请求是否应该通过相同的环节? 我认为这就是我现在正在打破的事情。 我应该以完全不同的方式重用连接或构建我的客户端吗?

另外:我在Clojure中这样做,如果这有任何区别的话。

不,你做得对。 但是,您必须保留对Channel实例的引用。 一旦拥有该通道,只要它是打开的,您就不需要创建另一个引导程序。 (如果这就是你正在做的事。)

这是我在最近的一个项目中使用的:

class ClientConnection (构造函数)

 // Configure the client. bootstrap = new ClientBootstrap( new NioClientSocketChannelFactory( Executors.newCachedThreadPool(), Executors.newCachedThreadPool() ) ); // Set up the pipeline factory. bootstrap.setPipelineFactory( new ChannelPipelineFactory() { public ChannelPipeline getPipeline() throws Exception { return Channels.pipeline( // put your handlers here ); } } ); 

class ClientConnection.connect(String host,int port)

 if (isConnected()) { throw new IllegalStateException("already connected"); } // Start the connection attempt. ChannelFuture future = bootstrap.connect(new InetSocketAddress(host, port)); channel = future.awaitUninterruptibly().getChannel(); // Wait until the connection is closed or the connection attempt fails. channel.getCloseFuture().addListener(new ChannelFutureListener() { @Override public void operationComplete(ChannelFuture future) throws Exception { new Thread(new Runnable() { public void run() { // Shut down thread pools to exit // (cannot be executed in the same thread pool! bootstrap.releaseExternalResources(); LOG.log(Level.INFO, "Shutting down"); } }).start(); } }); 

所以,基本上,我只保留对bootstrapchannel的引用,但是前者几乎没有在这些代码行之外使用。

注意:您应该只执行bootstrap.releaseExternalResources(); 一次,当申请退出时。 在我的情况下,客户端发送一些文件然后关闭通道并退出。

一旦你有一个连接的Channel实例,你只需要使用那个实例,直到你再次关闭它。 关闭后,您可以重新调用bootstrap以再次创建新的Channel

就个人而言,我一开始觉得Netty有点难以理解,但是一旦掌握了它的工作原理,它就是Java中最好的NIO框架。 IMO。