Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use threading model Netty

2025-04-13 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

How to use the threading model Netty, I believe that many inexperienced people do not know what to do, so this paper summarizes the causes of the problem and solutions, through this article I hope you can solve this problem.

one。 Preface

As we all know, the reason why netty is high performance is that it uses NIO, but this is only one of the reasons, which is determined by its IO model. On the other hand, due to the design of its thread model, good thread model design can reduce thread context switching, reduce or even avoid the cost of lock competition (lock-free design).

II. Reactor mode

Reactor pattern is a software programming pattern, which was released by Jim Coplien and Douglas C. Schmidt in 1995 and is mainly used to handle requests initiated by one or more clients.

Look at the Reactor mode from the two processes in which a client connects to the log server and then sends the request. There are three components:

Dispacther: distribute the request event of the client

Acceptor: accept client connection events

Handler: handles request events initiated by the client

1. The client connects to the log server

Log server registers Acceptor to Dispatcher

The log server calls the handle_events method inside the dispatcher

The dispatcher starts waiting for client requests on multichannel listeners (usually OS's select, epoll)

The client connects to the log server

Dispatcher notifies Acceptor connection event

Accetor accepts a new connection

two。 The client sends a request for logging

The client sends a request for logging using the connection established in the previous section

The client dispenser notifies Handler of the time when the log is recorded.

The Handler processes the read request and then passes it internally to the next Handler to continue processing

Finally, Handler writes the response.

Return to the dispatcher, which continues the event loop and waits for the next event to be processed

The role of the above components and the pattern for handling connection and request events is the Reactor pattern.

III. Scalable IO in Java

The above Reactor pattern is only a simple design model, for each programming language design, still need to make some changes. How Java-based NIO uses this pattern to build high-performance scalable services, the concurrency god Doug Lea published a paper "Scalable IO in Java" on his website.

The main topic in this paper is how to build a high-performance scalable IO, which is based on the evolution of Reactor pattern.

The following components are involved:

Reactor: respond to the IO event and distribute to the appropriate Handlers

Handlers: perform non-blocking IO operations

1. Single thread Reactor mode

The client sends a request to the server, and the Reactor responds to its IO event.

If it is a request to establish a connection, it is distributed to the acceptor, which accepts the connection, and then registers it with the dispatcher.

If it is a read request, it is distributed to Handler, which reads out the content of the request, decodes the content, processes the calculation, encodes the response, and finally sends the response.

A single thread is used throughout the process, and only one thread is used for both the Reactor thread and subsequent Handler processing.

But single threading will undoubtedly degrade performance, so you need to add more threads to provide extensions.

two。 Multithreaded Reactor mode

In order to improve scalability, we need to add threads to the single-threaded model, mainly from two aspects to make use of multi-threading to give full play to the advantages of multi-core applications:

Worker Threads,Reactor should be able to trigger events quickly to prevent the delayed Reactor response processed by Handler from causing events to accumulate, resulting in a backlog of client connection requests, or even running out of handles on the server, and the service stops responding. So working multithreading is used in Handlers processing.

Multiple Reactor Threads, which uses multiple Reactor threads to respond to events initiated by the client, and can use multiple Reactor to share the load

In the above multithreaded Reactor processing mode, the Reactor thread is still single-threaded and is responsible for acceptor and IO read/send. However, work thread pool is responsible for the decoding of the request and the coding of the business processing and response.

3. Multi-Reactor mode

The above multithreading mode not only solves the problem that Handler reduces the response of Reactor, but also improves the processing efficiency of Handler. However, Reactor is still single-threaded, and it is still under load for a large number of network events. In order to be able to use multithreading to share stress, perform multiple Reactor:

Where the main Reactor responds to the user's connection event and then distributes it to the acceptor to create a new child Reactor. Multiple sub-Reactor handle their own IO events, such as read/write, and then hand them over to work thread pool for decoding, business processing, and encoding.

The multi-Reactor design separates TCP connection establishment and IO read/write events into different Reactor, thus sharing the pressure of a single Reactor and improving its response ability.

IV. Threading model in Netty

After understanding the Reactor design pattern and building a high-performance scalable IO based on Reactor, the threading model of netty is much simpler.

Netty's threading model design is just a variation of the Reactor pattern. The above three Reactor modes are well supported in netty. In netty, the above modes are switched mainly through parameter configuration.

There are two classes in netty, EventLoopGroup and EventLoop, which are the key to implementing Reactor. EventLoop, as its name suggests, contains a Selector selector and a piece of loop logic in the package. Get the ready events on the Selector through a continuous loop and then process them. An EventLoopGroup is a group that contains a set of EventLoop through which an EventLoop can be generated.

After reading the Demo given on the official website of netty, you can see that when you create a Server, you will create two EventLoopGroup, boss and work. The former is for user Main Reactor, while the latter is for Sub Reactor and WorkThreadPool.

Every time Main Reactor gets a request from the client to establish a connection through Selector, it gets an EventLoop from work EventLoopGroup, and then binds the Socket abstract SocketChannel corresponding to the established connection to EventLoop to form a new Sub Reactor.

After learning about the threading model of netty, let's first take a look at the parameter configuration of netty in various modes.

1. Single threaded Reactor configuration

By constructing an EventLoop and using it as Reactor and WorkThread, this is the single-threaded mode.

EventLoopGroup eventLoopGroup = new NioEventLoopGroup (); EventLoop bossLoop = eventLoopGroup.next (); EventLoop workLoop = reactorLoop;ServerBootstrap b = new ServerBootstrap (); b.group (reactorLoop, workLoop)

Boss and work use the same EventLoop to implement single-threaded Reactor.

two。 Multithreaded Reactor configuration

Reactor uses single threading, and then Work uses multithreading, which is the multithreaded model.

EventLoopGroup eventLoopGroup = new NioEventLoopGroup (); EventLoop bossLoop = eventLoopGroup.next (); EventLoopGroup workLoopGroup = new NioEventLoopGroup (); ServerBootstrap b = new ServerBootstrap (); b.group (reactorLoop, workLoopGroup); 3. Multi-Reactor mode

The above multi-threaded Reactor mode is the multi-Reactor mode. BossLoop is the master Reactor, which creates a TCP connection through an event loop, and then binds the SocketChannel abstraction of the connection to the EventLoop in the workLoopGroup to form a Sub Reactor.

It's just that Main Reactor is a single-threaded event loop. Although multithreading can also be constructed, it doesn't make any practical sense. Because when binding ports in netty, only one EventLoop in Group is used to bind to Selector, that is, EventLoopGroup is used.

Of course, for the same application, it makes sense to listen to multiple ports and use multiple ServerBootStrap to share a boss, so that Main Reactor is also multithreaded.

After reading the above, have you mastered how to use the threading model Netty? If you want to learn more skills or want to know more about it, you are welcome to follow the industry information channel, thank you for reading!

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: 222

*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.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report