Netty始终要写一篇来作为终结的,但是到了写的时候才发现无从下手,了解 的还是不够吧。无奈,从四处摘录了一大片东西,很多都是官网下来的,没有什么文字说明,权当参考了。
首先来一张总体架构图,这个是从Neety官网上摘下来的,描述了Netty的核心架构和总体功能。
1. BootStrap
Bootstrap : ChannelFactory, ChannelPipeline, ChannelPipelineFactory | 初始化channel的辅助类 为具体的子类提供公共数据结构 |
ServerBootstrap: bind() | 创建服务器端channel的辅助类 接收connection请求 |
ClientBootstrap: connect() | 创建客户端channel的辅助类 发起connection请求 |
ConnectionlessBootstrap: connect() , bind() | 创建无连接传输channel的辅助类(UDP) 包括Client 和Server |
2. Buffer

Buffer的作用在于取代nio中的java.nio.ByteBuffer,相比ByteBuffer,可以根据需要自定义buffertype。内置混合的buffertype, 以实现zero-copy。提供类似StringBuffer的动态dynamic buffer;不需要调用flip方法;推荐使用ChannelBuffers的静态工厂创建ChannelBuffer.
3. channel
org.jboss.netty.channel | channel核心api,包括异步和事件驱动等各种传送接口 |
org.jboss.netty.channel.group | channel group,帮助用户维护channel列表 |
org.jboss.netty.channel.local | 一种虚拟传输方式,允许同一个虚拟机上的两个部分可以互相通信 |
org.jboss.netty.channel.socket | TCP, UDP接口,继承了核心的channel API |
org.jboss.netty.channel.socket.nio | 基于nio的Socket channel实现 |
org.jboss.netty.channel.socket.oio | 基于老io的Socket channel实现 |
org.jboss.netty.channel.socket.http | 基于http的客户端和相应的server端的实现,工作在有防火墙的情况 |
Channel的核心包结构如下图所示:
4. handler
org.jboss.netty.handler | 处理器 |
org.jboss.netty.handler.codec | 编码解码器 |
org.jboss.netty.handler.execution | 基于Executor的实现 |
org.jboss.netty.handler.queue | 将event存入内部队列的处理 |
org.jboss.netty.handler.ssl | 基于SSLEngine的SSL以及TLS实现 |
org.jboss.netty.handler.stream | 异步写入大数据,不会产生outOfMemory也不会花费很多内存 |
org.jboss.netty.handler.timeout | 通过Timer来对读写超时或者闲置链接进行通知 |
5. Netty的事件模型
5.1. Netty Pipline
I/O Request
via Channel
or
ChannelHandlerContext
|
+----------------------------------------+---------------+
| ChannelPipeline | |
| \|/ |
| +----------------------+ +-----------+------------+ |
| | Upstream Handler N | | Downstream Handler 1 | |
| +----------+-----------+ +-----------+------------+ |
| /|\ | |
| | \|/ |
| +----------+-----------+ +-----------+------------+ |
| | Upstream Handler N-1 | | Downstream Handler 2 | |
| +----------+-----------+ +-----------+------------+ |
| /|\ . |
| . . |
| [ sendUpstream() ] [ sendDownstream() ] |
| [ + INBOUND data ] [ + OUTBOUND data ] |
| . . |
| . \|/ |
| +----------+-----------+ +-----------+------------+ |
| | Upstream Handler 2 | | Downstream Handler M-1 | |
| +----------+-----------+ +-----------+------------+ |
| /|\ | |
| | \|/ |
| +----------+-----------+ +-----------+------------+ |
| | Upstream Handler 1 | | Downstream Handler M | |
| +----------+-----------+ +-----------+------------+ |
| /|\ | |
+-------------+--------------------------+---------------+
| \|/
+-------------+--------------------------+---------------+
| | | |
| [ Socket.read() ] [ Socket.write() ] |
| |
| Netty Internal I/O Threads (Transport Implementation) |
+--------------------------------------------------------+
处理方式:
ChannelPipelinep = Channels.pipeline();
p.addLast("1", newUpstreamHandlerA());
p.addLast("2", newUpstreamHandlerB());
p.addLast("3", newDownstreamHandlerA());
p.addLast("4", newDownstreamHandlerB());
p.addLast("5", newUpstreamHandlerX());
Upstream: 1 –> 2 –> 5 顺序处理
Downstream: 4 –> 3 逆序处理
5.2. ChannelState
Direction | State | Value | Meaning |
Upstream | true | The channel is open. | |
Upstream | false | The channel is closed. | |
Upstream | The channel is bound to a local address. | ||
Upstream | null | The channel is unbound to a local address. | |
Upstream | The channel is connected to a remote address. | ||
Upstream | null | The channel is disconnected from a remote address. | |
Upstream | an integer | The channel interestOps has been changed. | |
Downstream | true | N/A | |
Downstream | false | Close the channel. | |
Downstream | Bind the channel to the specified local address. | ||
Downstream | null | Unbind the channel from the current local address. | |
Downstream | Connect the channel to the specified remote address. | ||
Downstream | null | Disconnect the channel from the current remote address. | |
Downstream | an integer | Change the interestOps of the channel. |
5.3. Upstreamevents
Event name | Event type and condition | Meaning |
"messageReceived" | MessageEvent | a message object (e.g. ChannelBuffer) was received from a remote peer |
"exceptionCaught" | ExceptionEvent | an exception was raised by an I/O thread or a ChannelHandler |
"channelOpen" | ChannelStateEvent | a Channel is open, but not bound nor connected |
"channelClosed" | ChannelStateEvent | a Channel was closed and all its related resources were released |
"channelBound" | ChannelStateEvent | a Channel is open and bound to a local address, but not connected |
"channelUnbound" | ChannelStateEvent | a Channel was unbound from the current local address |
"channelConnected" | ChannelStateEvent | a Channel is open, bound to a local address, and connected to a remote address |
"writeComplete" | WriteCompletionEvent | something has been written to a remote peer |
"channelDisconnected" | ChannelStateEvent | a Channel was disconnected from its remote peer |
"channelInterestChanged" | ChannelStateEvent | a Channel's interestOps was changed |
5.4. Downstreamevents
Event name | Event type and condition | Meaning |
"write" | Send a message to the Channel. | |
"bind" | ChannelStateEvent | Bind the Channel to the specified local address. |
"unbind" | ChannelStateEvent | Unbind the Channel from the current local address. |
"connect" | ChannelStateEvent | Connect the Channel to the specified remote address. |
"disconnect" | ChannelStateEvent | Disconnect the Channel from the current remote address. |
"close" | ChannelStateEvent | Close the Channel. |
6. Netty VS Mina
Netty基于Pipeline处理,Mina基于Filter过滤
Netty的事件驱动模型具有更好的扩展性和易用性
Https,SSL,PB,RSTP,Text&Binary等协议支持
Netty中UDP传输有更好的支持
官方测试Netty比Mina性能更好