Netty source code analysis of the five series: Netty multi-threaded model

introduction

We always said Nettyis a high-performance server, which in the end is why the high-performance applications it? Threading model directly affects the performance of the network applications, this article will Nettymultithreaded model starting unveiled its high-performance characteristics.

Multi-threading model

Question (1) traditional IO model

If we ourselves are Nettythe designers of network applications, you want to design a high-performance network applications, the first problem to face is how to solve the performance bottleneck of network programming. So what is the network application performance bottlenecks it? We all know that traditional network applications using a BIOmodel that is blocking IO. Among the networks program processing read()and write()operations will block the current thread. Therefore, in the conventional IOmodel among each of socketthe connection will have a separate thread for processing.
Here Insert Picture Description
But in the current age of the Internet, client connections may be millions-even billions-level, server could not create so many threads to handle client requests. This is the traditional IOperformance bottlenecks where the network connection.

(2) optimization model

As a content of the traditional IOperformance bottleneck model is needed for each server socketconnection allocating thread to meet the needs of the business process. Is there a way to not need to establish so many connections can also handle client requests it? If you want a thread to handle multiple requests, it BIOcan not be achieved. Therefore, we can use javathe NIOaccomplished optimize the operation of this section. NettyThe practice is to use Reactormode, a so-called Reactormode is a mode using I / O multiplexer synchronization mechanism nonblocking. There is no longer expand a description.

In the Nettyworld, EventLoopGroupit is a very important core concepts. The so-called EventLoopGroupis that EventLoopthe collection is the event loop. Which NioEventLoopis responsible for polling multiplexer, acquisition channel is already in the ready state, performing network connection, a client requests access, read and write related operations. bossGroupIt is used to process the connection request, and workerGroupis used to handle read and write requests. bossGroupAfter processing the connection request, the connection will be submitted to workerGroupbe processed, workerGroupwhich has a plurality of EventLoop, workerGroupthat is to handle the actual read and write operations. That new connection will be forwarded to which EventLoopto deal with it? This requires a load balancing algorithm Nettyis used in the polling algorithm. NettySupports multiple Reactormodes, such as single-threaded model, and a master model from a multi-thread multithreaded model, the user can set parameters to start the switching pattern corresponding to the actual scene.

(3) Analysis Code

a, single-threaded model

EventLoopGroup bossGroup = new NioEventLoopGroup(1);
try {
            ServerBootstrap b = new ServerBootstrap();
            b.group(bossGroup)
             .channel(NioServerSocketChannel.class)
...
             }
...

As can be seen the code, create a parameter 1 bossGroup, which is mainly used for receiving EventLoopGroup client connections, is the single-threading model.

b, multi-threading model


EventLoopGroup bossGroup = new NioEventLoopGroup(1);
EventLoopGroup workerGroup = new NioEventLoopGroup();
try {
      ServerBootstrap b = new ServerBootstrap();
      b.group(bossGroup, workerGroup)
       .channel(NioServerSocketChannel.class)
...
}
...

And different single-threaded mode, multi-threaded mode created workerGroupto handle the IOoperation, the default number is twice the number of CPU.

C, from the master model multithreading
In this model, a thread for receiving client connections no longer a single NIOthread, but rather a thread pool.

//创建boss线程组,处理客户端连接
EventLoopGroup bossGroup = new NioEventLoopGroup(2);
// 创建worker线程组用于SocketChannel 的数据读写
EventLoopGroup workerGroup = new NioEventLoopGroup();
		try {
			// 创建 ServerBootstrap 对象
			ServerBootstrap b = new ServerBootstrap();
			// 设置EventLoopGroup
			b.group(bossGroup, workerGroup)
			// 设置要被实例化的为 NioServerSocketChannel 类 
			.channel(NioServerSocketChannel.class)
			...
}
...

to sum up

This paper describes a Nettymulti-threaded model, which is used in Reactorthe model. Processing the connection request processing IOthreads isolated operation. Event-based poll monitoring, continuous access to the channel in the ready state. Wherein Bossafter the thread pool thread responsible for processing a connection request, accept the received event, the corresponding socketencapsulated generating NioSocketChannelobject and submit it to the workBossthread pool, the IO processing readand writeevents.

Published 88 original articles · won praise 49 · Views 100,000 +

Guess you like

Origin blog.csdn.net/Diamond_Tao/article/details/105031472