IO series (2) detailed explanation of netty components and thread model


Introduction: Netty is a NIO client server framework that can quickly and easily develop network applications, such as protocol servers and clients. It greatly simplifies and simplifies network programming, such as TCP and UDP socket servers.

Netty usage scenarios:

Internet industry: In a distributed system, remote service calls are required between various nodes, and a high-performance RPC framework is indispensable. As an asynchronous high-performance communication framework, Netty is often used by these RPC frameworks as a basic communication component. Typical applications are: Ali's distributed service framework Dubbo's RPC framework uses the Dubbo protocol for inter-node communication, and the Dubbo protocol uses Netty as the basic communication component by default for implementation. Internal communication between process nodes. The bottom layer of Rocketmq also uses Netty as the basic communication component.
Game industry: Whether it is a mobile game server or a large-scale online game, the Java language has been more and more widely used. As a high-performance basic communication component, Netty itself provides TCP/UDP and HTTP protocol stacks.

Detailed explanation of netty model:

  1. Netty abstracts two groups of thread pools, BossGroup and WorkerGroup. BossGroup is responsible for receiving client connections, and WorkerGroup is responsible for network reads and writes.
  2. Both BossGroup and WorkerGroup types are NioEventLoopGroup
  3. NioEventLoopGroup is equivalent to an event loop thread group, this group contains multiple event loop threads, each event loop thread is NioEventLoop
  4. Each NioEventLoop has a selector, which is used to monitor the network communication of the socketChannel registered on it
  5. There are 3 steps in each Boss NioEventLoop thread internal loop execution
  • Process the accept event, establish a connection with the client, and generate a NioSocketChannel
  • Register the NioSocketChannel to the selector on a worker NIOEventLoop to process the tasks of the task queue, namely runAllTasks
  1. Steps executed by each worker NIOEventLoop thread loop
  • Polling the read and write events of all NioSocketChannels registered on its selector
  • Process I/O events, namely read and write events, and process business in the corresponding NioSocketChannel
  • runAllTasks handles the tasks of the task queue TaskQueue, and some time-consuming business processing can generally be put into the TaskQueue to be processed slowly, so as not to affect the flow of data in the pipeline.
  1. When each worker NIOEventLoop processes the NioSocketChannel business, it uses the pipeline (pipeline), and many handler processors are maintained in the pipeline to process the data in the channel

Netty core module components

1. Bootstrap、ServerBootstrap

Bootstrap means booting. A Netty application usually starts with a Bootstrap. The main function is to configure the entire Netty program and connect various components. The Bootstrap class in Netty is the startup guide class for the client program, and ServerBootstrap is the server startup guide class

2. Future、ChannelFuture

All IO operations in Netty are asynchronous, and it is not immediately known whether the message is processed correctly. But you can wait for it to finish executing or register a listener directly. The specific implementation is through Future and ChannelFutures. They can register a listener. When the operation is executed successfully or fails, the listener will automatically trigger the registered listener event.

3. Channel

A component of Netty network communication that can be used to perform network I/O operations. Channel provides users with:

  1. The status of the current network connection channel (for example, is it open? Is it connected?)
  2. Configuration parameters of the network connection (for example, the size of the receiving buffer)
  3. Provides asynchronous network I/O operations (such as establishing a connection, reading and writing, and binding ports). Asynchronous calls mean that any I/O call will return immediately, and there is no guarantee that the requested I/O operation has been completed at the end of the call. carry out
  4. The call immediately returns a ChannelFuture instance. By registering a listener to the ChannelFuture, the caller can be called back to notify the caller when the I/O operation succeeds, fails, or cancels.
  5. Supports associated I/O operations and corresponding handlers.
    Different protocols and connections of different blocking types have different channel types corresponding to them
1 NioSocketChannel,异步的客户端TCP Socket 连接。
2 NioServerSocketChannel,异步的服务器端TCP Socket 连接。
3 NioDatagramChannel,异步的UDP连接。
4NioSctpChannel,异步的客户端 Sctp 连接。
5 NioSctpServerChannel,异步的 Sctp 服务器端连接,这些通道涵盖了UDP和TCP网络IO以及文件IO。

4. Selector

Netty implements I/O multiplexing based on the Selector object. Through the Selector, one thread can monitor the Channel events of multiple connections. When a Channel is registered to a Selector, the internal mechanism of the Selector can automatically and continuously query (Select) whether these registered Channels have ready I/O events (such as readable, writable, network connection complete, etc.), so The program can simply use one thread to efficiently manage multiple channels

5. NioEventLoop

NioEventLoop maintains a thread and task queue, which supports asynchronous submission of execution tasks. When the thread starts, the run method of NioEventLoop is called to execute I/O tasks and non-I/O tasks: I/O tasks, that is, the ready event in the selectionKey, Such as accept, connect, read, write, etc., triggered by the processSelectedKeys method.
Non-IO tasks, tasks added to taskQueue, such as register0, bind0 and other tasks, are triggered by the runAllTasks method.

6. NioEventLoopGroup

NioEventLoopGroup, which mainly manages the life cycle of eventLoop, can be understood as a thread pool. A set of threads is maintained internally. Each thread (NioEventLoop) is responsible for processing events on multiple Channels, and a Channel corresponds to only one thread.

7. ChannelHandler

ChannelHandler is an interface that processes I/O events or intercepts I/O operations and forwards it to the next handler in its ChannelPipeline (business processing chain)

8. ChannelPipline

Save the List of ChannelHandler, which is used to process or intercept Channel's inbound events and outbound operations. ChannelPipeline implements an advanced form of interception filter mode, allowing users to fully control how events are handled and how each ChannelHandler in the Channel interacts with each other. In Netty, each Channel has and only one ChannelPipeline corresponding to it. A Channel contains a ChannelPipeline, and the ChannelPipeline maintains a doubly linked list composed of ChannelHandlerContext, and each ChannelHandlerContext is associated with a ChannelHandler. The read event (inbound event) and the write event (outbound event) are in a doubly linked list. The inbound event will be passed from the head of the linked list to the last inbound handler, and the outbound event will be passed from the tail to the front of the linked list. The first outbound handler, the two types of handlers do not interfere with each other

Reference website:

Official website: https://netty.io/

User manual: https://netty.io/wiki/user-guide.html

《Scalable IO in Java》:http://gee.cs.oswego.edu/dl/cpjslides/nio.pdf

Guess you like

Origin blog.csdn.net/qq_38130094/article/details/104789790