A brief introduction to the core components of netty


A brief introduction to the netty core:


Channel can be understood as a socket connection. When the client connects to the server, a Channel is established, which is responsible for basic IO operations, such as:
bind(), connect(), read(), write(), etc.
The API provided by Netty's Channel interface greatly reduces the complexity of using the Socket class directly.
Connections with different protocols and different blocking types have different Channel types corresponding to them. Commonly used Channel types: 

With the Channel connection service, messages can flow between connections. If the messages sent by the server are called "outbound" messages, the messages received by the server are called "inbound" messages.
Then the "outbound"/"inbound" of the message will generate an event (Event).
For example: connection activated; data read; user event; exception event; open link; close link, etc.
With events, a mechanism is needed to monitor and coordinate events. This mechanism (component) is EventLoop.
Each Channel in Netty will be assigned to an EventLoop. An EventLoop can serve multiple Channels.
Each EventLoop will occupy a Thread, and this Thread will handle all IO operations and events that occur on the EventLoop. 

ChannelHandler is the most important component for users, because the writing of business logic for inbound and outbound data is done in ChannelHandler.
In the previous example, MyChannelHandler implements the channelRead method to obtain the data from the client.
For data outbound and inbound, there are different ChannelHandler types corresponding to it:
ChannelInboundHandler inbound event handler
ChannelOutBoundHandler outbound event handler 


In the data transfer process of the Channel, there are many business logics that need to be processed, such as: encoding and decoding processing, reading and writing operations, etc., then a
ChannelHandler is required for each business logic implementation, which means that a Channel Corresponding to multiple ChannelHandlers,
how to manage multiple ChannelHandlers and their execution order, this requires ChannelPipeline to manage.
A Channel contains a ChannelPipeline, and a ChannelPipeline maintains a list of ChannelHandlers.
The mapping relationship between ChannelHandler and Channel and ChannelPipeline is maintained by ChannelHandlerContext.

Bootstrap means bootstrap. Its function is to configure the entire Netty program, string together various components, and finally bind the port and start the Netty service.
Two types of bootstrap classes are provided in Netty, one for the client (Bootstrap) and the other (ServerBootstrap) for the server.
Their difference is:
ServerBootstrap will bind to a port because the server must listen for connections, while Bootstrap is used by client applications that want to connect to remote nodes.
Only one EventLoopGroup is needed to bootstrap a client, but two are needed for a ServerBootstrap.
Because the server needs two different sets of Channels.
The first set will contain only one ServerChannel, which represents the server's own listening socket bound to a local port.
The second group will contain all the ones created to handle incoming client connections. 

Futures provide a way to notify an application when an operation is complete. This object can be seen as a placeholder for the result of an asynchronous operation that will complete at some point in the future,
and provides access to its result.
JDK presets the interface java.util.concurrent.Future, but the implementation it provides only allows manual checking whether the corresponding operation has been completed,
or blocks until it is completed. This is very cumbersome, so Netty provides its own implementation - ChannelFuture, for use when performing asynchronous operations.
ChannelFuture provides several additional methods that allow us to register one or more ChannelFutureListener instances.
The callback method operationComplete() of the listener will be called when the corresponding operation is completed. The listener can then determine whether the operation completed successfully or with errors.
Each of Netty's outbound I/O operations will return a ChannelFuture, that is, none of them will block. So, Netty is completely asynchronous and event-driven.

Guess you like

Origin blog.csdn.net/u013558123/article/details/131257594