认识Netty
Netty简介
Netty isan asynchronous event-driven network application frameworkfor rapid development of maintainable high performance protocol servers & clients.
Netty is a NIO client server framework which enables quick and easy development of network applications such as protocol servers and clients. It greatly simplifies and streamlines network programming such as TCP and UDP socket server.
Netty是一个异步事件驱动的网络应用框架,用于快速开发可维护的高性能协议服务器和客户端。
Netty是一款NIO客户端服务器框架,可以快速轻松地开发协议服务器和客户端等网络应用程序。它极大地简化并简化了TCP和UDP套接字服务器等网络编程。
Netty架构图如下图所示:
Netty的特性总结如下表:
为什么选择Netty
Netty是业界最流行的NIO框架之一,它的健壮性、功能、性能、可定制性和可扩展性在同类框架中都是首屈一指的,已经得到成百上千的商用项目验证。通过对Netty的分析,将它的优点总结如下:
API使用简单,开发门槛低;
功能强大,预置了多种编解码功能,支持多种主流协议;
定制能力强,可以通过ChannelHandler对通信框架进行灵活地扩展;
性能高,通过与其他业界主流的NIO框架对比,Netty的综合性能最优;
成熟、稳定,Netty修复了已经发现的所有JDK NIO BUG,业务开发人员不需要再为NIO的BUG而烦恼;
社区活跃,版本迭代周期短,发现的BUG可以被及时修复,同时,更多的新功能会加入;
经历了大规模的商业应用考验,质量得到验证。
核心概念
Netty is a non-blocking framework. This leads to high throughput compared to blocking IO.Understanding non-blocking IO is crucial to understanding Netty’s core components and their relationships.
Channel
Channelis the base of Java NIO. It represents an open connection which is capable of IO operations such as reading and writing.
Channel是Java NIO的基础。它表示一个开放的连接,能够进行IO操作,例如读写。
Future
Netty中的每个IO操作都是非阻塞的,这意味着每次操作都会在通话结束后立即返回。
标准Java库中有一个Future接口,但对Netty而言不方便 - 我们只能向Future询问完成操作或阻止当前线程,直到完成操作。这就是为什么Netty有自己的ChannelFuture接口,我们可以将回调传递给ChannelFuture,其将在操作完成时被调用。
Events and Handlers
Netty使用事件驱动的应用程序范例,因此数据处理的管道是通过处理程序的一系列事件。事件和处理程序可以与入站( )和出站( )数据流相关联。 (入站事件)可以如下所示:
Channel activation and deactivation
Read operation events
Exception events
User events
(出站事件)更简单,通常与打开/关闭连接( )和写入/清空数据( )有关。
Netty应用程序由几个网络和应用程序逻辑事件及其处理程序组成。通道事件处理程序的基础接口是 及其子接口 。
Netty提供了大量 实现的类。值得注意的是适配器只是空的实现,例如 和 。
Encoders and Decoders
当我们使用网络协议时,我们需要执行数据序列化和反序列化。为此,Netty为了能够解码传入数据引入了ChannelInboundHandler的扩展解码器,大多数解码器的基类 。对于编码输出数据,Netty同样提供了ChannelOutboundHandler的扩展编码器, 是大多数编码器实现的基础。
应用示例
Server Application
创建一个简单协议服务器的项目,它接收请求,执行计算并发送响应。
Dependencies
Data Model
Request Decoder
It should be noted that Netty works with socket receive buffer, which is represented not as a queue but just as a bunch of bytes.This means that our inbound handler can be called when the full message is not received by a server.
We must make sure that we have received the full message before processing. The decoder for RequestData is shown next:
It uses an implementation ofByteBufwhich throws an exception when there is not enough data in the buffer for the reading operation.
When the exception is caught the buffer is rewound to the beginning and the decoder waits for a new portion of data. Decoding stops when theoutlist is not empty afterdecodeexecution.
Response Encoder
Request Processing
Server Bootstrap
Client Application
The client should perform reverse encoding and decoding, so we need to have aRequestDataEncoderandResponseDataDecoder:
Also, we need to define aClientHandlerwhich will send the request and receive the response from server:
Now let’s bootstrap the client:
Now we can run the client’s main method and take a look at the console output. As expected, we got ResponseData with intValue equal to 246.
参考资源
Netty权威指南(第2版)-李林锋
Introduction to Netty
领取专属 10元无门槛券
私享最新 技术干货