Java NIO illustrates the process of starting the Netty server | JD Cloud Technical Team

1. Startup overview

After understanding the core components commonly used in Netty as a whole, and comparing the traditional IO mode. In the comparison process, I found out how traditional IO is implemented in Netty. Finally, we learned about the components commonly used in netty.


On the premise of understanding these core components, this article further understands how components are created during the entire server startup process and how components are used together. First of all, we must first understand the startup process of the server, and in the process of understanding, we will bring our own questions to explore the answers during the learning process.

1.1 Startup overview

1.2 Startup problem

  1. How does the netty server start in non-blocking mode?
  2. How is the event registered to the selector after the server starts?

2. Startup details

2.1 channel creation

It's still the same. First of all, what are the processes in the channel creation process?

  • bind
  • initAndRegister

  • The default constructor creates the channel

The specific call relationship

is easy to understand from steps 1, 2, and 3 in the sequence diagram.

2.1.1 Create channelFactory

Obtaining the channel from the reflection of the class Here is a key point that needs to be explained:


In the figure, the channelFactory is directly used to realize the instantiation of the channel. Then follow the diagram to find out when the channelFactory is assigned.

In the figure, we find the path of channelFactory step by step. Let's look at the third step who called
channel(Class<? extends C> channelClass) to create channelFactory. Finally, we found that we set channelFactory when bootstrap set the channel attribute.

Only here do we really get into the knowledge of setting NioServerSocketChannel in bootstrap. And use NioServerSocketChannel to create.

Let's summarize the above steps. Set the server-side NioServerSocketChannel by setting the Channel property of the bootstrap to generate a channelFactory. channelFactory to actively call the incoming class to construct the channel.

2.2.2 NioServerSocketChannel default constructor to create Channel

From the figure above, we know that the construction factory is initialized by calling the constructor of the class. From the code, we know that the construction class is NioServerSocketChannel. Then look at the NioServerSocketChannel construction process.

First come up or look at the overall steps:


The channel at the bottom of jdk is created through the default newSocket method, and then the id of the channel is configured through the Channel, corresponding to the unsafe component for reading and writing at the bottom of the channel, the logical processing pipeline corresponding to the channel, and finally the channel is set to non-blocking mode through configureBlocking [answered
] Our first question].
Step 3: Set some tcp level settings through channelConfig

So far the channel is created

2.2 channel initialization

Through the above 2.1, the entire channel has been created. The second big step is to configure some attributes for the channel on the basis of creating the channel.


The properties used for setting in the above figure correspond to the following code in the code


Here are some of their own properties and configuration settings, so far the creation and initialization of the channel is complete. The configuration here is used to set when the client creates a new connection.

2.3 selector registration

Let's take a look at the entire selector process first, how to register the channel to the selector.

ps: The above code flow needs to be tracked layer by layer through breakpoints.

Step 1: AbstractBootStrap register entry

The above steps complete the creation and initialization of the channel, and implement the selector mount through the initAndRegister internal method.

Step 2: Call

io.netty.channel.MultithreadEventLoopGroup#register(io.netty.channel.Channel)

In the register method, the next method can be used to obtain the next register method that can call EventLoop to call it

Step 3: Call

io.netty.channel.SingleThreadEventLoop#register(io.netty.channel.Channel)

Call the channel.unsafe.register method to obtain the underlying operation of the channel, and call the unsafe register method

Step 4: Register the channel to the selector of eventLoop by calling abstract Nio Channel [answer to the second question], and associate the current channel with the socket channel as an attache.

After completing the binding of the corresponding selector and channel, if there is a corresponding event callback, the event callback operation will be performed. If necessary, you need to inherit the corresponding methods channelRegistered and channelActive in ChannelInboundHandlerAdapter.

At this point, the nio channel is bound to the specified selector through the socket channel at the bottom of java, and the registration process between the selector and niochannel is completed.

2.4 Port Binding

The basic steps

Step 1: Bottom port channel binding

Step 2: Finally call the AbstractNioChannel doBeginRead method.

Activate the channel and register the read event with the selector.

3. Summary

Overall understanding of the startup process of the netty server.

  1. Create a channelFactory method through the channelFactory of NioServerSocketChannel

  2. channelFactory invokes the nioServerSocketChannel constructor via reflection. Create a channel object

  3. The channel sets the custom option, attr, and handler through NioServerSocketChannelConfig.

  4. abstract Nio Channel calls the register method to realize the binding of selector and channel

  5. Finally call the java channel for port binding and register the read event with the selector

The overall netty server startup is complete.

Author: JD Technology Chen Fanglin

Source: Reprinted by JD Cloud developer community, please indicate the source

Microsoft official announcement: Visual Studio for Mac retired The programming language created by the Chinese developer team: MoonBit (Moon Rabbit) Father of LLVM: Mojo will not threaten Python, the fear should be C++ The father of C++ Bjarne Stroustrup shared life advice Linus also Dislike the acronym, what TM is called "GenPD" Rust 1.72.0 is released, and the minimum supported version in the future is Windows 10 Wenxin said that it will open WordPress to the whole society and launch the "100-year plan" Microsoft does not talk about martial arts and uses "malicious pop-ups "Prompt users to deprecate Google's high-level, functional, interpreted, dynamic programming languages: Crumb
{{o.name}}
{{m.name}}

Guess you like

Origin my.oschina.net/u/4090830/blog/10105610