`
xpenxpen
  • 浏览: 703260 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

Netty初步

 
阅读更多
1.入门文档
如果是入门的话,官网的文档已经相当好了。里面的例子程序得仔细阅读,这里就不再重复转载了。参见http://netty.io/wiki/user-guide.html

2.为什么需要netty
2.1 主要是scalibity和performance

2.2 另外Netty In Action有一些说明,笔记如下:

2.2.1 传统的异步编程有2种模式:callback和Future

2.2.2 3种API比较
BIO: N个连接N个Thread
NIO: N个连接1个Selector-->1个Thread
NIO2:CompletionHandler避免了Selector的轮询(JDK7)

这张图是BIO和NIO的比较。


2.2.3 JDK的问题:
1.跨平台:linux上OK,windows上却出问题
2.ByteBuffer无法扩展
3.scattering gathering可能有内存泄露(直到java6的后期update版本或者Java7才解决)
4.epoll bug,可导致CPU 100%。  https://github.com/netty/netty/issues/327
Netty解决了以上4点问题。


3.Netty3,4,5 API不同点

本文写作时,各版本的最新版如下:
netty33.9.0
netty44.0.18
netty55.0.0 Alpha1


3.1 ChannelHandler的变化
首先有一些术语需要理解,请参见下面的表格和3张图,从中可以看到netty3,4,5之间的很大的不同。

收消息上行,入站 InputStream.read(byte[])
发消息下行,出站 OutputStream.write(byte[]), Socket.connect(SocketAddress), Socket.close()


Netty3使用了上行下行的概念


Netty4使用了入站出站的概念


Netty5则干脆取消了两者的划分,统一为一个概念


Netty3: ChannelHandler有两个子接口ChannelUpstreamHandler,ChannelDownstreamHandler,
上行和下行,这里的上行和下行和我们一般理解上的上下行不太一样,为何如此可以参考上面的3张图。
相应的类有SimpleChannelUpstreamHandler,SimpleChannelDownstreamHandler,以及一个同时实现两个接口的SimpleChannelHandler




Netty4: 接口变成了ChannelInboundHandler ChannelOutboundHandler,可能是为了避免原来的上下行造成误解,所以改成入站和出站了。
相应的类有ChannelInboundHandlerAdapter,ChannelOutboundHandlerAdapter,我们只要选择一个继承就可以了。




Netty5: 取消了进站、出站的划分,统一为继承ChannelHandlerAdapter,原来的ChannelInboundHandlerAdapter,ChannelOutboundHandlerAdapter被废弃。



ChannelHandler的API,从中可以看到netty3,4,5之间的很大的不同。


3.2 BootStrap的变化

3.2.1 netty4构造ServerBootstrap时采用了构建者模式,使得代码更优雅。
EventLoopGroup bossGroup = new NioEventLoopGroup();
EventLoopGroup workerGroup = new NioEventLoopGroup();
ServerBootstrap b = new ServerBootstrap();
b.group(bossGroup, workerGroup)
 .channel(NioServerSocketChannel.class)
 .childHandler(new ChannelInitializer<SocketChannel>() {
    @Override
    public void initChannel(SocketChannel ch) throws Exception {
        ch.pipeline().addLast(new DiscardServerHandler());
    }
})
 .option(ChannelOption.SO_BACKLOG, 128)
 .childOption(ChannelOption.SO_KEEPALIVE, true);

而netty3则是用最平常的setter。
ChannelFactory factory = new NioServerSocketChannelFactory(
    Executors.newCachedThreadPool(),
    Executors.newCachedThreadPool());

ServerBootstrap bootstrap = new ServerBootstrap(factory);

bootstrap.setPipelineFactory(new ChannelPipelineFactory() {
    public ChannelPipeline getPipeline() {
          return Channels.pipeline(new DiscardServerHandler());
    }
});
    
bootstrap.setOption("child.tcpNoDelay", true);
bootstrap.setOption("child.keepAlive", true);


3.2.2 对比一下netty3和4,我们发现netty4因为采用了泛型的写法(.channel(NioServerSocketChannel.class)),所以NioServerSocketChannelFactory就不需要透露给用户了,这个工厂也被取消了。

3.2.3 另外netty4引入了ChannelOption的常量定义,注意这个类是泛型的(ChannelOption<T>),用了这个技巧从而可以确保赋值安全,使得SO_BACKLOG只能传一个int进来,SO_KEEPALIVE只能传一个boolean进来。
public static final ChannelOption<Integer> SO_BACKLOG
public static final ChannelOption<Boolean> SO_KEEPALIVE


Bootstrap的API,netty4相比netty3有比较大的变化,而netty5和netty4比则基本相同。

作为初步,暂时先分析到这里。其余不同点有待以后继续分析。
  • 大小: 7.1 KB
  • 大小: 6 KB
  • 大小: 6.1 KB
  • 大小: 6.1 KB
  • 大小: 10.1 KB
  • 大小: 8.2 KB
  • 大小: 3.4 KB
分享到:
评论
1 楼 wang23109203 2016-05-05  

相关推荐

Global site tag (gtag.js) - Google Analytics