Nettyversion: 4.1.55.Final Traditional IO model web containers, such as the old version of Tomcat, need to continuously increase the number of system core threads or increase the system's request processing capacity by horizontally expanding the number of servers in order to increase system throughput. With NIO, one thread can handle multiple connection events. The Netty framework based on the multiplexing model not only reduces the complexity of using NIO, advantageNetty is a network application framework based on Java NIO and event-driven model that supports asynchronous and high concurrency
IO Modelselect, poll, and epoll The operating system kernel implements non-blocking IO based on these functions to implement a multiplexing model.
select
The main difference from select is that the limitation of select that can only monitor 1024 file descriptors is removed.
epool
Reactor Model1. Single Reactor Single Thread1) It is possible to monitor multiple connection requests through a blocking object 2) The Reactor object listens to client request events through select and distributes them through dispatch 3) If it is a request to establish a connection, the Acceptor processes the connection request through accept, and then creates a Handler object to handle various events after the connection is completed 4) If it is not a link request, the Reactor will dispatch and call the corresponding Handler for the link to handle it 5) Handler will complete the complete business process of Read->Business Process->Send 2. Single Reactor Multithreading1) The Reactor object listens to the client request event through select, and after receiving the event, it distributes it through dispatch 2) If it is a request to establish a connection, the Acceptor processes the connection request through accept, and then creates a Handler object to handle various events after the connection is completed 3) If it is not a link request, the Reactor will dispatch and call the corresponding Handler for the link to handle it 4) Handler is only responsible for event response and does not perform specific business processing 5) After reading the data through read, it is distributed to the worker thread pool for processing. After processing, it is returned to the Handler. After the Handler receives it, it returns the result to the client through send 3. Master-slave Reactor multithreading1) Reactor main thread MainReactor object listens to link events through select and processes them through Acceptor 2) After the Acceptor processes the link event, the MainReactor assigns the link to the SubReactor 3) SubReactor adds the link to the queue for monitoring and creates a Handler for event processing 4) When a new event occurs, SubReactor will call the corresponding Handler to handle it 5) The Handler reads the data through read and distributes it to the worker thread pool for processing. After processing, it returns it to the Handler. After receiving it, the Handler returns the result to the client through send 6) The Reactor main thread can correspond to multiple Reactor sub-threads Three modes to understand with real life cases1) Single Reactor single thread, the front desk receptionist and waiter are the same person, serving customers throughout the process 2) Single Reactor multi-threaded, 1 receptionist, multiple waiters, the receptionist is only responsible for reception 3) Master-slave Reactor multithreading, multiple receptionists, multiple waiters The Reactor model has the following advantages1) Fast response, no need to be blocked by a single synchronous event, although Reactor itself is still synchronous 2) It can avoid complex multi-threading and synchronization problems to the greatest extent, and avoid the switching overhead of multi-threads/processes 3) Good scalability, you can easily increase the number of Reactor instances to make full use of CPU resources 4) Good reusability. The Reactor model itself has nothing to do with the specific event processing logic and has high reusability Core Components1.Bootstrap A Netty application usually starts with a Bootstrap, which is mainly used to configure the entire Netty program and connect various components in series. Handler, in order to support various protocols and ways of processing data, the Handler component was born. Handler is mainly used to handle various events, and the events here are very broad, such as connection, data reception, exception, data conversion, etc. 2. ChannelInboundHandler is the most commonly used Handler. The function of this Handler is to handle events when data is received. In other words, our business logic is generally written in this Handler. ChannelInboundHandler is used to handle our core business logic. 3. ChannelInitializer When a link is established, we need to know how to receive or send data. Of course, we have various Handler implementations to handle it. Then ChannelInitializer is used to configure these Handlers. It will provide a ChannelPipeline and add the Handler to the ChannelPipeline. 4.ChannelPipeline A Netty application is based on the ChannelPipeline mechanism, which needs to rely on EventLoop and EventLoopGroup because all three of them are related to events or event processing. The purpose of EventLoops is to process IO operations for Channels. One EventLoop can serve multiple Channels. EventLoopGroup will contain multiple EventLoops. 5.Channel represents a Socket link, or other components related to IO operations. It is used together with EventLoop to participate in IO processing. 6.Future In Netty, all IO operations are asynchronous. Therefore, you cannot know immediately whether the message is processed correctly, but we can wait for it to complete or directly register a listener. The specific implementation is through Future and ChannelFutures. They can register a listener, which will be automatically triggered when the operation succeeds or fails. ExampleThrough a simple example, first understand how to develop a communication program based on Netty, including the service and the client: Server: Client: test: Ecology
Similar technologiesMina, Netty, Grizzly otherProactor non-blocking asynchronous network model refer tohttps://mp.weixin.qq.com/s?__biz=MzUxNDA1NDI3OA==&mid=2247492766&idx=2&sn=b5df49147561e467fa5677b5b b09dacb&chksm=f9496577ce3eec61383994499d96a7f2b091b5eb8ee1ac47ad021f78072ae710f41d38257406&scene=27 https://blog.csdn.net/a745233700/article/details/122660246 |
<<: Interesting explanation of bearer: PTN and IPRAN in one article
>>: How much do you know about the development of Wi-Fi?
ChangeIP is a foreign VPS hosting company registe...
DogYun has launched a promotion during this year&...
Canadian hosting provider VMISS currently offers ...
Network monitoring can take many forms, depending...
Despite repeated popularization of knowledge, man...
To celebrate the traditional Chinese New Year, sp...
background Every time the bell of the Double 11 g...
On April 23, 2021, the 79th China Educational Equ...
Xi'an, a world-renowned ancient capital and c...
Edge computing focuses on placing computing and s...
Wireshark is a very popular packet sniffer. It ca...
Preface The previous article "Whether it is ...
In order to comprehensively display the developme...
Network analysis is critical for monitoring, secu...
[51CTO.com original article] The Internet of Thin...