2

In the last lesson, we have a preliminary understanding of Netty. In this lesson, we will look at the entire Netty context so as to have an overall understanding of Netty's architecture and principles! The following figure is the main framework of Netty: (Welcome to pay attention to the wx public number: [source code apprentice] to explore various open source code implementations together!)

Netty的主要架构图

Basic understanding of EventLoopGroup

What we need to know about EventLoopGroup , Netty has made a lot of extensions to EventLoopGroup, the following picture is his family map:

image-20210421090036067

The case we used in the previous lesson was NioEventLoopGroup , which is an implementation of NIO. It can be seen that it is MultithreadEventLoopGroup . As you can see from the name, NioEventLoopGroup is a multi-threaded event loop group. Here you can put It is regarded as a thread pool, there are multiple threads (NioEventLoop) inside, and each client connected SocketChannel corresponds to a thread (NioEventLoop)!

We have been talking about NioEventLoop above. Based on the above figure, we can see that it is SingleThreadEventLoop . As can be seen from the name, it is a single-threaded executor! We can see that his parent interface is in fact inherited from EventLoopGroup , that is to say, although NIoEventLoop is a single-threaded event loop, we can also regard it as a thread pool based on the interface, but the internal thread pool Only one thread!

\## Netty communication channel

We need to understand the types of pipelines in Netty. Here we focus on the implementation of NIO:

image-20210421091642282

We can see that there are two main implementations of Netty's Socket communication pipeline, NioServerSocketChannel and NioSocketChannel . These two implementations are Netty's different implementations of the server channel and the client channel. We are developing Netty's server and client At the end, the type of pipe used will be specified! There is a more important point here, that is, the parent implementation of NioServerSocketChannel is AbstractNioMessageChannel , and the parent implementation of is 160dc40b06f208 AbstractNioByteChannel . These two implementations are important to determine whether to process the connection or process the data in the subsequent NIO event loop. Means, leave an impression here, we will talk about it in detail later!

Netty's pipeline flow

We need to understand the Netty business execution chain, also called pipeline flow ChannelPipeline :

Our Netty code will consist of code similar to this logic:

image-20210423083409332

It is the main way that Netty allows us to focus on the business. His main implementation is a doubly linked list. Here is to add a Handler service processor at the end of the linked list. There are two types of Handlers:

image-20210423083645396

As shown in the figure above, it has two implementation methods, one is ChannelInboundHandlerAdapter other is ChannelOutboundHandlerAdapter . The execution sequence of their calls in a business flow is as follows, we borrow an illustration to illustrate:

image-20210423083856807

When we call the Socket's data reading API, that is, when reading data from the Socket pipe, the Pipeline will execute Inbound Handler order of your addition. When we finish reading the event, call the write method to write data into the channel At that time, the pipeline flow starts to call the Outbound Handler method, in reverse order! This reverse call may be difficult to understand, but we still use a diagram to illustrate:

image-20210423091810761

When the read method is called, the Inbound nodes are called sequentially! When calling the write method, call the outbount method in reverse order!

Netty's Handler event callback types

1. ChannelInboundHandler

Method nameMethod effect
handlerAddedChannel is added 1
channelRegisteredMethod to call back all Handlers after successful JDK registration 2
channelActiveCallback after jdkChannel is activated 3
channelReadThere is data readable in the channel 4
channelReadCompleteData read 5
channelInactiveCall back this method after the channel is closed 6
channelUnregisteredThe channel is unregistered back to call the method 7
handlerRemovedChannel is deleted 8
userEventTriggeredIf a user event is triggered, this method is called.
channelWritabilityChangedWritable state change
exceptionCaughtAn exception occurred

2. ChannelOutboundHandler

Method nameMethod effect
bindCalled after binding operation.
connectCalled after connection operation.
disconnectCalled after disconnecting operation.
closeCalled after closing operation.
deregisterCalled after logout operation from the currently registered EventLoop.
readRead data
writeWrite data
flushFlush to pipeline

to sum up

Through this article, we can learn about several important concepts in Netty, the basic concepts of EventLoopGroup, the channel concept in Netty, and the concept of pipeline flow in Netty!


huangfusuper
10 声望1 粉丝