编程知识 cdmana.com

Basic architecture of netty

Last class, we were very interested in Netty Have a preliminary understanding , In this class, we will look down on the whole Netty The context of , So that we can understand Netty There is an overall understanding of the principle of architecture ! The picture below is Netty The main framework of :( Welcome to your attention wx official account :【 Source code apprentice 】 Explore all kinds of open source code implementation together !)

Netty The main architecture of the

EventLoopGroup Basic knowledge

What we need to know EventLoopGroup, Netty Yes EventLoopGroup We've done a lot of extension implementation , Here's his family map :

image-20210421090036067

The case we used in the last lesson , It uses NioEventLoopGroup, He is NIO The implementation of the , It can be seen that he is MultithreadEventLoopGroup Subclasses of , It can be seen from the name ,NioEventLoopGroup Is a multithreaded event loop group , Here you can think of it as a thread pool , There are multiple threads inside (NioEventLoop), Every client connected to SocketChannel All correspond to one thread (NioEventLoop)!

We talk about it all the time NioEventLoop, We can see from the picture above , He is SingleThreadEventLoop Subclasses of , It can be seen from the name , It's a single threaded actuator ! We can see , His father's interface is actually inheritance and EventLoopGroup, in other words , although NIoEventLoop It's a single threaded event loop , But we're based on interfaces , You can also think of it as a thread pool , It's just that there is only one thread inside the thread pool !

\## Netty The communication channel of

What we need to know Netty Pipe types in , Here we focus on NIO How to implement :

image-20210421091642282

We can see ,Netty Of Socket There are two main ways to realize communication pipeline ,NioServerSocketChannel, NioSocketChannel, The two implementations are Netty For server channel , Different implementation from client channel , We are developing Netty When the server and the client of , Specifies the type of pipe to use ! There is a more important point , namely NioServerSocketChannel The parent class implementation of is AbstractNioMessageChannel, NioSocketChannel The parent class implementation of is AbstractNioByteChannel , These two implementations are follow-up NIO During the event cycle, it is an important means to judge whether to handle the connection or the data , Make an impression here , We will talk about it in detail later !

Netty Pipe flow of water

What we need to know Netty Business execution chain of enterprise , It's also called pipe flow ChannelPipeline

our Netty The code will be made up of code similar to this kind of logic :

image-20210423083409332

He is Netty The main ways we can focus on our business , His main implementation is a two-way linked list , Here is to add a Handler Business processor ,Handler There are roughly two kinds of species :

image-20210423083645396

As shown in the figure above , He has two ways to do it , One is ChannelInboundHandlerAdapter One is ChannelOutboundHandlerAdapter, The execution order of their calls in a business flow is as follows , Let's use a diagram to illustrate :

image-20210423083856807

When we call Socket Read data API when , From Socket When the pipeline reads data ,Pipeline It will be executed in the order you add it Inbound Handler, When we finish reading events , call write Method to write data to the channel , Pipe flow start call Outbound Handler Method , Reverse order call ! This reverse order call may not be easy to understand , Let's use a picture to illustrate :

image-20210423091810761

When calling read Method time , Sequential call Inbound node ! When calling write Method time , Reverse order call outbount Method !

Netty Of Handler Event callback type

1. ChannelInboundHandler

Method name Method function
handlerAdded The channel is added 1
channelRegistered JDK Call back all users after successful registration Handler Methods 2
channelActive jdkChannel Callback after activation 3
channelRead There is data to read in the channel 4
channelReadComplete Data read complete 5
channelInactive Call back the method after the channel is closed 6
channelUnregistered The channel is deregistered using this method 7
handlerRemoved Channel deleted 8
userEventTriggered If a user event is triggered , Call the method .
channelWritabilityChanged Writable state change
exceptionCaught Something is wrong

2. ChannelOutboundHandler

Method name Method function
bind Call after binding operation .
connect Call after connection operation .
disconnect After disconnecting operation, call .
close Call after closing operation .
deregister From currently registered EventLoop After the logout operation is invoked .
read Reading data
write Writing data
flush Refresh to pipeline

summary

Through this article , We can see Netty Some of the more important concepts in English ,EventLoopGroup Basic concepts of 、Netty The concept of channel 、Netty The concept of pipe flow in !

版权声明
本文为[huangfusuper]所创,转载请带上原文链接,感谢
https://cdmana.com/2021/06/20210630090213851I.html

Scroll to Top