netty4.0 研究 ing ... ...
netty4.0 研究 ing ... ...
石头哥哥 发表于5年前
netty4.0 研究 ing ... ...
  • 发表于 5年前
  • 阅读 2420
  • 收藏 2
  • 点赞 0
  • 评论 0

新睿云服务器60天免费使用,快来体验!>>>   

之前做过一个基于netty3.X版本的server,性能测试很不错,现在netty4.0已经出beta2版本beta3的快照也出来了,相信不久就会发布,先尝鲜,总结了一些东西:


#The shutdown process of a typical network application is composed of the following three steps:
#
#Close all server sockets if there are any,
#
#Close all non-server sockets (i.e. client sockets and accepted sockets) if there are any, and
#
#Release all resources used by ServerBootstrap or Bootstrap.


action:
#2. How do I incorporate my blocking application code with the non-blocking NioEventLoopGroup?
#
#NioEventLoopGroup contains n Threads that handle the IO of all the registered Channels.
#
#If your applicaiton's handler blocks such as (reading from a database) or is CPU intensive, the worker thread pool maybe exhausted and performance will degrade.
#
#We recommend that you implement your blocking application code in another thread pool. You can do this by adding your handler with an extra EventExecutor to the ChannelPipeline.
#
#  1 public static void main(String[] args) throws Exception {
#  2         final EventExecutor executor = new DefaultEventExecutor(8);
#
#  4         ServerBootstrap bootstrap = new ServerBootstrap();
#            bootstrap.group(new NioEventLoopGroup(), new NioEventLoopGroup());
#  6         bootstrap.childHandler(new ChannelInitializer<Channel>() {
#                @Override
#  8             public void initChannel(Channel channel) {
#                    ChannelPipeline pipeline = channel.pipeline();
# 10                 pipeline.addLast("decoder", new HttpRequestDecoder());
#                    pipeline.addLast("aggregator", new HttpChunkAggregator(65536));
# 12                 pipeline.addLast("encoder", new HttpResponseEncoder());
#                    pipeline.addLast("chunkedWriter", new ChunkedWriteHandler());
# 14
#                    // MyHandler contains code that blocks so add it with the
# 16                 // EventExecutor to the pipeline.
#                    pipeline.addLast(executor, "handler", new MyHandler());
# 18             }
#            });
# 20         ...
#
# 22         sb.bind(socketAddress);
#
# 24         // Other code
#
# 26     }
#
# 28
#        public class MyHandler extends SimpleChannelUpstreamHandler {
# 30         // Your blocking application code
#    }





netty4变化较大,不管是线程池管理还是buffer,新添加了AIO,但是建议还是用nio(个人观点),但是根据开发者帮助文档,基本上也可以快速实现一个稳定高效的服务端(TCP/IP)socket长连接。

有关netty4.0 持续关注中... ...  有时间的话 将会很快写一个基于netty4.0的server。

http://netty.io/4.0/guide/#faq.3,在上面可以找到很多资料,并且快速上手,如果你对netty3足够熟悉的话。

http://netty.io/4.0/xref/(source)

  • 打赏
  • 点赞
  • 收藏
  • 分享
共有 人打赏支持
粉丝 297
博文 187
码字总数 120455
作品 2
×
石头哥哥
如果觉得我的文章对您有用,请随意打赏。您的支持将鼓励我继续创作!
* 金额(元)
¥1 ¥5 ¥10 ¥20 其他金额
打赏人
留言
* 支付类型
微信扫码支付
打赏金额:
已支付成功
打赏金额: