Practice for Netty.
- Channel. -> Socket .屏蔽了底层实现,提供了统一的编程模型。
- EventLoop.
- ChannelFuture.
- ChannelHandler.业务逻辑与网络处理代码的分离。
- ChannelPipeline.
- ChannelHandlerContext
Channel --+
... |
Channel --+-> EventLoop(Single Thread) --+
|
Channel --+ |--> EventLoopGroup
| |
Channel --+-> EventLoop(Single Thread) --+
... |
Channel --+
Channel 1-->1 ChannelPipeline 1-->n ChannelHandler 1-->1 ChannelHandlerContext
I/O Request
via {@link Channel} or
{@link ChannelHandlerContext}
|
+---------------------------------------------------+---------------+
| ChannelPipeline | |
| \|/ |
| +---------------------+ +-----------+----------+ |
| | Inbound Handler N | | Outbound Handler 1 | |
| +----------+----------+ +-----------+----------+ |
| /|\ | |
| | \|/ |
| +----------+----------+ +-----------+----------+ |
| | Inbound Handler N-1 | | Outbound Handler 2 | |
| +----------+----------+ +-----------+----------+ |
| /|\ . |
| . . |
| ChannelHandlerContext.fireIN_EVT() ChannelHandlerContext.OUT_EVT()|
| [ method call] [method call] |
| . . |
| . \|/ |
| +----------+----------+ +-----------+----------+ |
| | Inbound Handler 2 | | Outbound Handler M-1 | |
| +----------+----------+ +-----------+----------+ |
| /|\ | |
| | \|/ |
| +----------+----------+ +-----------+----------+ |
| | Inbound Handler 1 | | Outbound Handler M | |
| +----------+----------+ +-----------+----------+ |
| /|\ | |
+---------------+-----------------------------------+---------------+
| \|/
+---------------+-----------------------------------+---------------+
| | | |
| [ Socket.read() ] [ Socket.write() ] |
| |
| Netty Internal I/O Threads (Transport Implementation) |
+-------------------------------------------------------------------+
- 唯一 直接 与Channel交互的Buffer: ByteBuffer
- Java NIO's selectors allow a single thread to monitor multiple channels of input.
- Performance is enhanced by allowing the CPU to move on to other tasks while data copies proceed in parallel in another part of the machine. Also, zero-copy operations reduce the number of time-consuming mode switches between user space and kernel space.
- 目前只有在使用NIO和Epoll传输时才可以使用。input streams can support zero-copy through the java.nio.channels.FileChannel's transferTo() method if the underlying operating system also supports zero copy.
- wikipedia
- Channel: A channel represents an open connection to an entity such as a hardware device, a file, a network socket, or a program component that is capable of performing one or more distinct I/O operations, for example reading or writing.
- Buffer: A buffer is a linear, finite sequence of elements of a specific primitive type.
- OutputStream: This abstract class is the superclass of all classes representing an output stream of bytes. An output stream accepts output bytes and sends them to some sink.
- InputStream: This abstract class is the superclass of all classes representing an input stream of bytes.
- stream 将连接和操作,数据类型全部耦合在了一起;而nio中将连接,操作,数据通过channel,(WritableByteChannel/ReadableByteChannel),Buffer解耦,更加符合SRP的理念。
- Pipeline中包含ChannelHandlerContext的双向链表,每个ChannelHandlerContext封装了一个Handler
- Outbound是package的发送者:即write、read等请求包的发送者,Inbound是package的接受者
Outbound并不是write,同理Inbound也不是read - public interface ChannelHandlerContext
extends AttributeMap, ChannelInboundInvoker, ChannelOutboundInvoker
所以ChannelHandlerContext可以传播请求,即Invoker,如同Pipline:
public interface ChannelPipeline extends ChannelInboundInvoker, ChannelOutboundInvoker, Iterable<Entry<String, ChannelHandler>> - Invoker 命令的发起者
- io.netty.channel.DefaultChannelPipeline
/**
* The default {@link ChannelPipeline} implementation. It is usually created
* by a {@link Channel} implementation when the {@link Channel} is created.
*/
public class DefaultChannelPipeline implements ChannelPipeline {
...
protected DefaultChannelPipeline(Channel channel) {
this.channel = ObjectUtil.checkNotNull(channel, "channel");
succeededFuture = new SucceededChannelFuture(channel, null);
voidPromise = new VoidChannelPromise(channel, true);
tail = new TailContext(this);
head = new HeadContext(this);
head.next = tail;
tail.prev = head;
}
...
}
- io.netty.channel.Channel.Unsafe: Unsafe operations that should never be called from user-code. These methods are only provided to implement the actual transport