I. Startup overview
The previous section looked at the common core components of Netty as a whole and compared them to the traditional IO model. In the process of comparison, find the traditional IO corresponding to Netty is how to achieve. Finally, we looked at the components that are commonly used in Netty.
With a look at these core components, this article takes a closer look at how components are created and used together throughout the server startup process. First of all, we will have a general understanding of the startup process of the server, and in the process of understanding, we will bring our own questions to explore the answers in the learning process.
1.1 Startup Overview
1.2 Startup Problems
How to set the non-blocking mode for the 0.NET TY server startup?
1. How is the event registered with selector after the server starts?
Two. Startup details
2.1 the channel to create
Once again, what’s going on in the channel creation process
- i: bind
- ii: initAndRegister
- Iii: The default constructor creates a channel
Specific call relationSteps 1,2 and 3 are easy to understand in the sequence diagram.
2.1.1 create channelFactory
From the reflection of the class we get the channel. Here’s a key point to make:
The channel is instantiated directly using the channelFactory. So let’s look at how the channelFactory was assigned.
In the figure, we step through the channelFactory path. Who calls channel(Class<? Extends C> channelClass) creates the channelFactory. Finally, we found that we set the channelFactory when bootstrap set the channel property.
By walking here we really get into the bootstrap setting NioServerSocketChannel. And created with a NioServerSocketChannel.
Let’s summarize these steps. The channelFactory is generated by setting the server NioServerSocketChannel by setting the Channel property of bootstrap. The channelFactory actively calls the incoming class to construct a channel.
2.2.2 NioServerSocketChannel Default constructor creates Channel
From the above we know that the constructor is initialized by calling the constructor of the class. From the code we know that the constructor is NioServerSocketChannel. So take a look at the NioServerSocketChannel construction process.
First, let’s look at the overall steps:
The underlying JDK channel is created using the default newSocket method. Then, the underlying CHANNEL id is configured to read and write the underlying channel’s unsafe component. Finally, set channel to non-blocking mode via configureBlocking. Step 3: Set some TCP layer Settings through channelConfig
The channel is now created
2.2 Channel Initialization
The entire channel has been created by 2.1 above. Step 2 On the basis of creating a channel, configure some properties for the channel.
The properties used for the above Settings correspond to the code shown below
Here are some of their own properties and configuration Settings, so the channel creation and initialization is complete. This configuration is used when the client creates a connection.
2.3 the selector to register
So let’s take a look at the whole selector process, how you register a channel with a selector.
Ps: The above code flow needs to be traced layer by layer through breakpoints.
Step 1: The AbstractBootStrap Register entry
! [insert picture description here] (img – blog. Csdnimg. Cn / 20210613141…
The above steps complete the creation and initialization of a channel, and the selector is mounted using the initAndRegister internal method. The second step: Call io.net ty. Channel. MultithreadEventLoopGroup# register (io.net ty. Channel. The channel) The next register method can be called by calling EventLoop. Call io.net ty. Channel. SingleThreadEventLoop# register (io.net ty. Channel. The channel)
The channel.unsafe. Register method is invoked to retrieve the channel’s underlying operation, and the unsafe register method is called
Step 4: Register the Channel with the Selector of the eventLoop by calling the Abstract Nio Channel [the answer to the second question] and associate the current Channel with the socket Channel as an attache.After complete the corresponding to the selector and the channel binding if there is a corresponding event callback event callback will be carried out in the operation, the corresponding necessary need to inherit ChannelInboundHandlerAdapter here method channelRegistered, ChannelActive.At this point, the NIO channel is bound to the specified selector through the Java underlying SocketChannel, completing the registration process of selector and NioChannel.
2.4 Port Binding
Basic steps
Step 1: Bind the underlying port channel
Step 2: Finally call the AbstractNioChannel doBeginRead method.
Activate a channel and register a read event with the Selector.
3. Summary
Learn about the startup process of the Netty server.
- 1. Create the channelFactory method using the channelFactory of NioServerSocketChannel
- ChannelFactory calls the nioServerSocketChannel constructor through reflection. Creating a Channel object
- 3. The channel by NioServerSocketChannelConfig the custom option, attr, handler setup complete.
- 4. Abstract Nio Channel calls the register method to bind the selector and Channel
- 5. Finally call the Java Channel for port binding and register the read event with selector
The Netty server is started successfully.