In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-03 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)05/31 Report--
In this article, the editor introduces "Reactor model analysis" in detail, the content is detailed, the steps are clear, and the details are handled properly. I hope this "Reactor model analysis" article can help you solve your doubts.
1. Problems with native NIO
NIO's class library and API are complicated and troublesome to use: you need to be proficient in Selector, ServerSocketChannel,
SocketChannel, ByteBuffer, etc.
Additional skills are required: to be familiar with Java multithreaded programming, since NIO programming involves Reactor mode, you must
Very familiar with multithreading and network programming in order to write high-quality NIO programs.
The development workload and difficulty are very large: for example, the client is faced with disconnection and reconnection, network flash disconnection, half-packet read and write, failure cache, network support.
The solution of plug and abnormal flow and so on.
JDK NIO's Bug: the infamous Epoll Bug, which causes Selector empty polling, resulting in CPU 100%. Until
The problem in JDK 1.7 still exists and has not been fundamentally addressed.
(Epoll Bug: whether there is a current IO event through Selector polling in NIO, according to JDK NIO api, the select side of Selector
The method blocks until the IO event reaches or times out, but there are sometimes problems here on the Linux platform, in some fields
In the scene, the select method returns directly, even if there is no timeout and no IO event arrives. This is the famous epoll.
Bug, which is a serious bug, can cause threads to fall into a dead loop and cause CPU to rise to 100%.
The reliability of the response system, so far, JDK has not fully dealt with this problem. )
2.netty details
Netty is an open source Java framework provided by JBOSS. Netty provides asynchronous, event-driven network application boxes
Frame, for the rapid development of high-performance, high-reliability network IO programs. Netty is a network programming framework based on NIO, using the
Netty can help you develop a web application quickly and easily, which is equivalent to simplifying and streamlining the development process of NIO. As
At present, the most popular NIO framework, Netty, has been achieved in the Internet field, big data distributed computing field, game industry, communication industry and so on.
A wide range of applications, the well-known Elasticsearch, Dubbo framework are the internal use of Netty.
The power of Netty: zero-copy, extensible event model; support for TCP, UDP, HTTP, WebSocket and other protocols; provide secure transmission, compression, large file transfer, codec support and so on.
It has the following advantages:
Elegantly designed, blocking and non-blocking Socket; provides a flexible and extensible event model; a highly customizable threading model.
With higher performance and greater throughput, zero-copy technology is used to minimize unnecessary memory replication and reduce resource consumption.
Provide secure transmission features.
Support a variety of mainstream protocols; preset a variety of codec functions to support customers to develop private protocols
3. Thread model
(1) basic details of threading model
Different thread modes have a great impact on the performance of the program. Before learning the Netty thread mode, we will first explain each thread mode.
Finally, take a look at the advantages of the Netty threading model. The existing threading models are:
Traditional blocking Ipaw O service model
Reactor model
Depending on the number of Reactor and the number of threads in the resource pool, there are three typical implementations
Single Reactor single thread
Single Reactor multithreading
Master-slave Reactor multithreading
(2) the traditional blocking Istroke O service model.
The blocking IO mode is used to obtain the input data. Each connection requires an independent thread to complete the data input, business resolution and data return.
Work.
There is a problem:
When the number of concurrency is large, a large number of threads will be created, which will take up a lot of system resources.
After the connection is created, if the current thread has no data to read temporarily, the thread will block the read operation, resulting in a waste of thread resources.
(3) Reactor model
Reactor mode, in which one or more inputs are passed to the service resolver at the same time, and the server-side program solves the incoming multiple
Requests and dispatches them synchronously to the corresponding resolution thread, so the Reactor pattern is also called the Dispatcher pattern. Use of Reactor mode
IO reuses listening events. After receiving the events, it distributes them to a thread (process). This is the key to solving the high concurrency problem of network servers.
Single Reactor single thread:
Selector enables applications to listen for multiconnection requests through a blocking object.
The Reactor object monitors the client request event through Selector. After receiving the event, distributing it through Dispatch is a connection request event, then Acceptor solves the connection request through Accept, and then creates a Handler object to solve the subsequent business solution after the connection is completed.
Handler will complete the Read → business to solve the complete business process of → Send
Advantages: the model is simple, there are no problems of multithreading, process communication and competition, and they are all done in one thread.
Disadvantages:
Performance problem: with only one thread, you can't fully utilize the performance of multicore CPU. When Handler solves the business on a certain connection
The whole process cannot resolve the remaining connection events, which can easily lead to performance bottlenecks.
Reliability problem: if the thread terminates unexpectedly or enters a dead loop, the communication module of the whole system will be unavailable and cannot be received and located.
Handle external messages, causing node failure
Single Reactor multithreading
The Reactor object monitors the client request event through selector, and when the event is received, it is distributed through dispatch.
If the connection request is established, the right Acceptor resolves the connection request through accept
If it is not a connection request, then the reactor distribution calls the handler corresponding to the connection to solve the problem.
Handler is only responsible for responding to events and does not make specific business solutions. After reading data through read, it will be distributed to a thread in the subsequent worker thread pool to solve the business.
The worker thread pool allocates separate threads to complete the real business and returns the results to handler
After handler receives the response, it returns the result to client through send.
Advantages:
Can make full use of the solving power of multicore cpu
Disadvantages:
Multi-threaded data sharing and access is more complex, reactor solves all event monitoring and response, running in a single thread, in
High concurrency scenarios are prone to performance bottlenecks
Master-slave Reactor multithreading
The MainReactor object of the Reactor main thread listens for client connection events through select. After receiving the events, the client connection events are resolved through Acceptor.
When Acceptor finishes resolving the client connection event (establishing a Socket connection with the client), MainReactor assigns the connection to SubReactor. (that is, MainReactor is only responsible for listening to client connection requests and establishing a connection with the client
The connection is then handed over to SubReactor to listen for subsequent IO events.)
SubReactor listens the connection to its own connection queue and creates Handler to solve all kinds of events. when a new event occurs on the connection, SubReactor will call the corresponding Handler solution.
Handler reads the request data from the connection through read and distributes the request data to the Worker thread pool for business resolution.
The Worker thread pool allocates separate threads to complete the real business solution and returns the result to Handler. Handler sends response data to the client through send
A MainReactor can correspond to multiple SubReactor, that is, a MainReactor thread can correspond to multiple
SubReactor thread
Advantages:
The data interaction between the MainReactor thread and the SubReactor thread is simple and the responsibility is clear, as long as the MainReactor thread
The new connection is received and the SubReactor thread completes the subsequent business resolution.
The data interaction between the MainReactor thread and the SubReactor thread is simple, and the MainReactor thread only needs to make a new connection.
Passed to the SubReactor thread, the SubReactor thread does not need to return data
Multiple SubReactor threads can handle higher concurrent requests
Disadvantages:
The disadvantage of this mode is the high programming complexity. However, because of its remarkable advantages, it is widely used in many projects, including
Nginx, Memcached, Netty, etc. This mode is also called the 1+M+N threading mode of the server, even if it is turned on.
The sent server contains one (or more, 1 just means relatively few) connection establishment thread + M IO thread + N business solution
Thread. This is a mature server programming pattern in the industry.
After reading this, the article "Reactor Model Analysis" has been introduced. If you want to master the knowledge points of this article, you still need to practice and use it yourself to understand it. If you want to know more about related articles, you are welcome to follow the industry information channel.
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.