netty的基础抽象概念

120 阅读1分钟

netty核心概念抽象

EventExecutor 和 EventExecutorGroup

EventExecutorGroup管理一组EventExecutor, 提交到某个eventExecutorGroup的事件,后续将由eventExecutorGroup选择一个eventExecutor去执行,eventExecutor和eventExecutorGroup类似于线程池和线程池中的线程。但是netty抽象出这2个概念,个人理解是为了屏蔽线程,使用netty时仅需要知道,对于一个事件,交由eventExecutor去执行就Ok了

EventLoopGroup 和 EventLoop

EventLoopGroup继承自EventExecutorGroup, 但是它重载next()方法,返回值为EventLoop, 即提供EventLoop管理功能,除此之外,还提供了将某个channel绑定到该group管理的EventLoop上的能力(register(Channel channel))方法。 EventLoop继承自EventExecutor,即它不仅提供执行事件的能力,还提供处理与之绑定的channel相关的IO操作

Channel

网络IO操作的连接器,channel依赖eventLoop完成IO操作,同时channel也会提交对应的IO操作事件给eventLoop。常用的channel如下

  • NioSocketChannel
  • NioSctpSocketChannel
  • NioServerSocketChannel

ChannelPipeline, ChannelHandler, ChannelHanderContext

channelPipeline采用责任链的设计模式封装channelHandler,形成一个双向链表 channelHandler负责处理IO读取或者写入的数据,channleHandler被封装在channleHandlerContext中,然后暴露给channelPipeline。形成简单类似下图的结构

image.png