Basic introduction to Netty components
- 2021-10-15 10:29:05
- OfStack
Introduction to Netty
netty is an asynchronous event-based framework, which is mainly aimed at developing highly concurrent applications for clients under tcp protocol.
netty is essentially an nio framework.
IO model
Java supports three io models: BIO, AIO, NIO
BIO
Synchronous blocking, one thread for one connection, when there is a connection request, the server needs to start one thread for processing. If this thread doesn't do anything, it will cause unnecessary overhead. When the number of concurrency is large, a large number of threads need to be created to handle the connection.
NIO
Synchronous non-blocking, one thread can handle multiple operations.
NIO has three core parts, Channel, Buffer and selector
Relationship among selector, channel and buffer
Each channel corresponds to one buffer selector corresponds to one thread, and one thread corresponds to multiple channel Which channel the program switches is determined by events. event selector switches on each channel according to different events Buffer is a memory block with an array at the bottom Data can be read and written through buffer, which can be read or written. The method of switching between the two is flip channel is bidirectionalBuffer Buffer
A memory block that can read and write data can be understood as a container object. It is responsible for reading data from channel and encapsulating it into Buffer objects. Buffer provides implementation classes for every 1 basic data type. It has three main attributes:
capacityFixed size value of memory block
positionIndex of the next location to read and write
limitIn write mode, limit = position, and in read mode, it means that you can read at most the maximum value written before
Important methods
allocate (int count) initializes buffer capacity
get () reads from position position
flip () switches from write mode to read mode
Channel channel
It acts like a stream, but it can be read and written simultaneously or asynchronously.
Channel is an interface in NIo, and its implementation classes are:
FileChannel is used to read and write files ServerSocketChannel TCP data reading and writing DatagramChannel is used to read and write UDP dataMain methods:
read reads data from the channel and puts it into a buffer write writes the data from the buffer to the channel transferfrom transfertoSelector selector
It can monitor whether there are events on multiple registered channels, and if there are events, get the events and hand them over to the channels for processing.
Main methods:
open Get 1 selector object SelectionKey register (seelctor, constant) registration channel, there are 4 kinds of constant 1, read read, write write, connect connection established, accept (new network connection can be accessed) select () monitors all registered channels, blockingProblems existing in native NIo
Class library and API are complex and difficult to develop, including bug
Netty model
netty abstracts two groups of thread pools, bossGroup is responsible for accepting client connections, and workGroup is responsible for reading and writing bossGroup and workerGroup are both NioEventLoopGroup Each NioEventLoopGroup corresponds to a group of event loops, each of which is an NIoEventLoop NIoEventLoop represents a thread that continuously loops through executing tasksNetty Core Module Components
Bootstrap,ServerBootstrap
Channel
ChannelFuture
All operations in netty are asynchronous, so we need a method that can determine the result. The notification is obtained through the addListener () method.
ChannelHandler
A container that handles inbound and outbound logic.
Life cycle method
handlerAdded Called when ChannelHandler is added to ChanelPipeLine
Called when handlerRemoved is removed
It mainly has two sub-interfaces, ChannelInboundHandler and ChannelOutBoundHandler
ChannelPipeLine
It is the container of ChannelHandler, which defines a set of ChannelHandler and is a set of filter chains
ChannelHandlerContext
Used for association between ChannelPipe and ChannelHandler, interaction with ChannelHandler in 1 ChannelPipeline.
ByteBuf
The data container of netty is the data structure of netty to realize data transmission.
Two different indexes, readIndex and writeIndex, are maintained internally. The data is divided into three regions by these two indexes. Read bytes, readable bytes, writable bytes. Compared with the native bytebuf of jdk, it has two pointers and does not need flip () for read-write conversion