In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly explains "how to understand and master Netty". The content of the explanation is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "how to understand and master Netty".
What can Netty do?
To put it simply, it is used to deal with network programming and write a server and client program that can communicate with the network. Without Netty, how do you deal with network programming in the world of Java?
The tools that come with Java are: the java.net package, which is used to handle network traffic, and later Java provides the NIO toolkit to provide non-blocking communication.
Mina, a third-party toolkit at the same level as Netty, is slightly different from Netty in design, but at its core is the ability to provide network communication.
Traditional network communication model
Before we talk about Netty, let's talk about what traditional network programming looks like. The traditional Socket programming development step is very simple, only need to use the Socket class to create the client and server.
But why doesn't anyone use it now? The main reason is that it is based on the thread model of synchronous blocking IO, which can not meet the needs of production at all, so it is naturally Out.
Synchronous blocking IO model
The problem with the synchronous blocking thread model is that a request must be bound to a thread to process, and all requests are synchronous operations, which means that the connection will not be released until the request is processed. If the concurrency is high, it will inevitably lead to too much pressure on the system.
New threading model of Netty
Based on this, Java adds a new non-blocking IO operation package NIO, and the thread model of NIO adopts Reactor mode, that is, asynchronous non-blocking mode, which solves the problem caused by synchronous blocking.
The full name of NIO is NoneBlocking IO, non-blocking IO, the difference from BIO,BIO is Blocking IO, blocking IO.
What does this blockage mean?
The Accept is blocked, and only when a new connection comes, the Accept will return and the main thread can continue.
The Read is blocked, and only when the request message comes, the Read can return and the child thread can continue to process.
Write is blocked, and only when the client receives the message, the Write can return and the child thread can continue to read the next request.
At present, there are two main types of design patterns for servers to handle responses:
Thread driven
Event driven
Synchronous blocking is a thread-driven pattern, and the most obvious example is Tomcat;. For event-driven, there is no need to create a thread for each connection to maintain.
Referring to the observer mode, you can set up an event pool and use a single thread to loop to listen for completed events in the current pool, and if so, fetch the event.
Let's briefly talk about how the Reactor pattern solves the thread waiting problem: when waiting for IO, the thread can exit first without waiting for the IO operation.
But if you don't wait, who will be returned after IO processing is completed? the Reactor model uses an event-driven mechanism, which requires threads to register callback functions with event loop before exiting, so that event loop can call callback functions to complete data return after IO completion.
There are four roles in Reactor. The processing of all data inflows is called Channel, which is like a water pipe. The Reactor model splits each event into an event, and the same type of event is grouped into one category. This type of unified processing logic is called a handler.
So how do you get one or more threads to listen to all Channel? That's why there's Selector.
Selector is like a manager, you can register multiple Channel with a Selector thread, it will use a blocking method to capture whether there is an event on the current Channel, and if so, take out the event to the corresponding Handler to handle.
Netty is built on top of NIO, and Netty provides more high-level API encapsulation on NIO.
Why not use the NIO provided by JDK
JDK has provided us with NIO packages and asynchronous non-blocking patterns implemented using the Reactor model, so why don't we hear who directly uses NIO to develop network programming in our daily development?
In fact, the reason why people don't use it is that it is too difficult to control. The main features provided in the Java NIO class library include:
Buffer Buffer
Channel Channel
Multiplexer Selector
The buffer Buffer is actually an object, that is, all incoming or outgoing data exists in the Buffer.
The difference between the new IO and the old stream-oriented IO is that the old IO is directly oriented to the byte stream, while the new IO is oriented to the buffer, and the read and write data are read and written to the buffer first.
The buffer is essentially a byte array, and NIO provides the ability to maintain the read and write location of the buffer data.
The relationship between Buffer and Channel
Channel channel, all the data in Buffer will be upstream to Channel, the data will flow to the processing logic through Channel, and the processed data will be returned to the client through Channel.
So Channel is full-duplex and can support reading and writing, which is the difference between it and Stream. If you use Stream, you can only use InputStream to read data and OutputStream to write data.
To use the analogy of things in the real world, the traditional IO is like a water pipe, where water can only flow down the pipe; NIO is like a two-way road that can drive in both directions.
In addition, it is precisely because of the introduction of Buffer that we can arbitrarily control how much data is transmitted and read each time, and how much offset we should re-read if the last reading fails, which is incomparable to the traditional Imax O stream.
Selector selector, which is the core of NIO, a Selector is a thread, NIO allows a Selector to manage multiple Channel, that is, Channel is registered with the Selector.
Selector will listen to the registered Channel to see if any events are ready, and if so, take them out and process them.
Polling snooping of Selector
About the NIO code I will not write, it is a huge pile, we can see Baidu. In short, network programming based on this idea is definitely the best way to face today's traffic peak. And it just so happens that the encapsulation at the bottom of Netty based on NIO has shielded you from this large pile of operations.
Another problem in network programming is cross-platform. The bottom layer of NIO depends on the IO API of the system, and different systems may have different implementations of IO API. Here, how to use NIO requires consideration of system compatibility.
Another problem is that NIO has a famous bug,JDK NIO underlying layer implemented by epoll. If the polling result of Selector is empty and there is no wakeup or new message processing, empty polling occurs and the CPU utilization is 100%.
This Bug official statement has been fixed, in fact it has not been Fix, but the probability of occurrence will be reduced.
Netty also deals with the Bug: the Select operation cycle of the Selector is counted, and each empty Select operation is counted. If N times of empty polling occur continuously in a certain period, the Epoll dead cycle Bug is triggered.
At this time, the Selector is rebuilt to determine whether the reconstruction request is initiated by another thread. If not, the original SocketChannel is de-registered from the old Selector, re-registered with the new Selector, and the original Selector is closed.
What should be paid attention to in network programming
Since we want to learn Netty, which is based on NIO encapsulation for network communication, what should we pay attention to when writing a piece of code for network communication? By figuring out these problems, we probably know what Netty has done.
When talking about the network, we can't avoid talking about the OSI 7 layer model and the TCP / IP 4 layer model:
OSI Model and corresponding Network Protocol
Java network programming mainly uses Socket socket programming, network programming based on layer 4 protocol, that is, encapsulation based on TCP/ UDP protocol.
What are the steps in writing an Socket communication?
Create a ServerSocket, listen to and bind a port.
A series of clients request this port.
The server uses Accept to get a Socket connection object from the client.
Start a new threading connection: ① reads Socket to get the byte stream; ② decoding protocol to get the HTTP request object; ③ processes the HTTP request to get a result and encapsulates it into a HTTPResponse object; ④ encoding protocol serializes the result byte stream; ④ writes Socket to send the byte stream to the client.
Continue to cycle through step 3.
According to the above data transfer process, we can ask some questions:
How to format the byte stream length to ensure that the byte stream read each time is up-to-date and will not be repeated last time.
The problem of codec of transmission byte stream.
A server must have multiple client links, how to manage a large number of client links, such as how to maintain disconnected reconnection, connection timeout and closing mechanism.
We will find the answers to the above questions in the next Netty study.
Netty core components
Before we get started with Netty, let's find out what kinds there are in Netty, so that we can have a clear target, and then we will learn to keep these key messages in order to avoid confusion.
① Bootstrap 、 ServerBootstrap
A Netty application usually starts with a Bootstrap, and its main function is to configure the whole Netty program and concatenate each component. The Bootstrap class in Netty is the boot class of the client program, and ServerBootstrap is the boot class of the server.
② Future 、 ChannelFuture
All IO operations in Netty are asynchronous, and you don't immediately know whether an event is finished or not.
However, you can wait for its execution to complete or register a listener directly. The specific implementation is to register a listener through Future and ChannelFutures. When the operation succeeds or fails, the listener automatically triggers the registered listening event.
③ Channel
A component of Netty network communication that can be used to perform network I-sign O operations.
Channel provides users with:
The status of the channel for the current network connection (for example, whether it is open, whether it is connected).
Configuration parameters for the network connection (such as the receive buffer size).
Provide asynchronous network Iamp O operations (such as establishing connections, reading and writing, binding ports). An asynchronous call means that any Imax O call will be returned immediately, and there is no guarantee that the requested Imax O operation will be completed at the end of the call.
The call immediately returns an ChannelFuture instance. By registering the listener to the ChannelFuture, you can call back to notify the caller when the operation succeeds, fails, or cancels.
Support associating Icano operations with corresponding handlers.
Different protocols and different blocking types of connections have different Channel types corresponding to them.
Here are some common Channel types:
NioSocketChannel, asynchronous client-side TCP Socket connection.
NioServerSocketChannel, asynchronous server-side TCP Socket connection.
NioDatagramChannel, asynchronous UDP connection.
NioSctpChannel, asynchronous client-side Sctp connection.
NioSctpServerChannel, asynchronous Sctp server-side connections, these channels cover UDP and TCP network IO as well as file IO.
④ Selector
Netty implements Channel O multiplexing based on Selector objects, and can listen for Channel events of multiple connections through a thread of Selector.
When a Channel is registered with a Selector, the Selector internal mechanism can automatically and continuously query (Select) whether these registered Channel have ready Channel O events (such as readable, writable, network connection completion, etc.), so that the program can easily use a single thread to efficiently manage multiple Channel.
⑤ NioEventLoop
A queue of threads and tasks is maintained in NioEventLoop, which supports asynchronous submission and execution of tasks. When a thread starts, it will call the run method of NioEventLoop to execute Imax O tasks and non-Imax O tasks:
The processSelectedKeys O task, that is, ready events in selectionKey, such as accept, connect, read, write, and so on, is triggered by the processSelectedKeys method.
Non-IO tasks, tasks added to the taskQueue, such as register0, bind0, and so on, are triggered by the runAllTasks method.
The execution time ratio of the two tasks is controlled by the variable ioRatio, which defaults to 50, which means that the execution time allowed for non-IO tasks is equal to that for IO tasks.
⑥ NioEventLoopGroup
NioEventLoopGroup, which mainly manages the life cycle of eventLoop, can be understood as a thread pool, which maintains a set of threads. Each thread (NioEventLoop) is responsible for handling events on multiple Channel, while a Channel only corresponds to one thread.
⑦ ChannelHandler
The ChannelHandler is an interface that handles the Imax O event or intercepts the Imax O operation and forwards it to the next handler in its ChannelPipeline (business processing chain).
ChannelHandler itself does not provide many methods, because this interface has many methods to implement and can inherit its subclasses during ease of use:
ChannelInboundHandler is used to handle the inbound Ithumb O event.
ChannelOutboundHandler is used to handle outbound Istroke O operations.
Or use the following adapter classes:
ChannelInboundHandlerAdapter is used to handle the inbound Ithumb O event.
ChannelOutboundHandlerAdapter is used to handle outbound Istroke O operations.
ChannelDuplexHandler is used to handle inbound and outbound events.
⑧ ChannelHandlerContext
Save all context information related to Channel and associate a ChannelHandler object.
⑨ ChannelPipline
Save the List of ChannelHandler, which is used to handle or intercept inbound events and outbound operations of Channel.
It implements an advanced form of interception filter mode that gives users complete control over how events are handled and how the various ChannelHandler in the Channel interact with each other.
There is one and only one ChannelPipeline for each Channel in Netty.
Thank you for your reading, the above is the content of "how to understand and master Netty". After the study of this article, I believe you have a deeper understanding of how to understand and master Netty, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.