introduction
We always said Netty
is 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 Netty
multithreaded model starting unveiled its high-performance characteristics.
Multi-threading model
Question (1) traditional IO model
If we ourselves are Netty
the 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 BIO
model that is blocking IO
. Among the networks program processing read()
and write()
operations will block the current thread. Therefore, in the conventional IO
model among each of socket
the connection will have a separate thread for processing.
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 IO
performance bottlenecks where the network connection.
(2) optimization model
As a content of the traditional IO
performance bottleneck model is needed for each server socket
connection 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 BIO
can not be achieved. Therefore, we can use java
the NIO
accomplished optimize the operation of this section. Netty
The practice is to use Reactor
mode, a so-called Reactor
mode is a mode using I / O multiplexer synchronization mechanism nonblocking. There is no longer expand a description.
In the Netty
world, EventLoopGroup
it is a very important core concepts. The so-called EventLoopGroup
is that EventLoop
the collection is the event loop. Which NioEventLoop
is responsible for polling multiplexer, acquisition channel is already in the ready state, performing network connection, a client requests access, read and write related operations. bossGroup
It is used to process the connection request, and workerGroup
is used to handle read and write requests. bossGroup
After processing the connection request, the connection will be submitted to workerGroup
be processed, workerGroup
which has a plurality of EventLoop
, workerGroup
that is to handle the actual read and write operations. That new connection will be forwarded to which EventLoop
to deal with it? This requires a load balancing algorithm Netty
is used in the polling algorithm. Netty
Supports multiple Reactor
modes, 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 workerGroup
to handle the IO
operation, 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 NIO
thread, 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 Netty
multi-threaded model, which is used in Reactor
the model. Processing the connection request processing IO
threads isolated operation. Event-based poll monitoring, continuous access to the channel in the ready state. Wherein Boss
after the thread pool thread responsible for processing a connection request, accept the received event, the corresponding socket
encapsulated generating NioSocketChannel
object and submit it to the workBoss
thread pool, the IO processing read
and write
events.