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

Distributed Netty source code analysis

2025-03-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article mainly introduces the relevant knowledge of distributed Netty source code analysis, the content is detailed and easy to understand, the operation is simple and fast, and has a certain reference value. I believe you will gain something after reading this distributed Netty source code analysis article. Let's take a look at it.

Server-side demo

Take a look at a simple Netty server-side example

Public static void main (String [] args) {EventLoopGroup bossGroup=new NioEventLoopGroup (1); EventLoopGroup workerGroup = new NioEventLoopGroup (); try {ServerBootstrap serverBootstrap=new ServerBootstrap () ServerBootstrap.group (bossGroup,workerGroup) .channel (NioServerSocketChannel.class) .option (ChannelOption.SO_BACKLOG) ChildHandler (new ChannelInitializer () {@ Override protected void initChannel (SocketChannel ch) throws Exception {ch.pipeline () .childHandler (new LengthFieldBasedFrameDecoder) Ch.pipeline () .addLast (new StringDecoder (Charset.forName ("UTF-8"); ch.pipeline () .addLast (new TcpServerHandler ());}}); ChannelFuture f=serverBootstrap.bind (8080) .sync () F.channel (). CloseFuture (). Sync ();} catch (InterruptedException e) {e.printStackTrace ();} finally {workerGroup.shutdownGracefully (); bossGroup.shutdownGracefully ();}}

Let's start with a brief description of the classes encountered above:

EventLoopGroup introduction

It mainly includes two functions, registering Channel and performing some Runnable tasks.

Function 1: let's first take a look at the registration Channel

Register Channel with Selector, and Selector dispatches events related to Channel, such as read, write, Accept, and so on.

The design of EventLoopGroup is that it contains multiple EventLoop (each EventLoop usually contains a thread). In the process of performing the above registration, you need to select one of the EventLoop to perform the above registration behavior. Here, there is a problem of choosing a policy. The choice policy interface is EventExecutorChooser, and you can also customize an implementation.

As can be seen from the above, most of the work done by EventLoopGroup is some overall work, such as initializing the above-mentioned multiple EventLoop, EventExecutorChooser, etc., and the specific registration of Channel is to be realized by its internal EventLoop.

Function 2: perform some Runnable tasks

EventLoopGroup inherits EventExecutorGroup,EventExecutorGroup is also a collection of EventExecutor, EventExecutorGroup is also in charge of the initialization of EventExecutor, EventExecutorGroup for the execution of Runnable tasks is to choose an internal EventExecutor to do specific implementation work.

Many tasks in netty are executed asynchronously. Once the current thread wants to perform related operations on an EventLoop, such as registering Channel to an EventLoop, if the current thread and the internal thread of the EventLoop you want to operate are not the same, then the current thread will only submit a registration task to the EventLoop and return a ChannelFuture to the outside.

Summary: EventLoopGroup contains the above two functions, it is more of a collection, but the specific functional implementation is to select an internal item element to perform related tasks. The internal item elements here usually implement both EventLoop and EventExecutor, such as NioEventLoop, etc.

ChannelPipeline introduction

The above EventLoopGroup can register a Channel with the Selector of an internal EventLoop, and then Netty specially designs a ChannelPipeline to deal with events such as reading and writing of this Channel. Each Channel has a ChannelPipeline to handle events such as reading and writing of the Channel.

Bind process

The bind process of the above serverBootstrap is as follows:

Create the NioServerSocketChannel you specified, and then initialize some Socket parameters

Configure a ChannelHandler for the ChannelPipeline of the above Channel. The function of the ChannelHandler is to initialize some logic, that is, to execute some logic in the initChannel method, when the Channel is successfully registered on the Selector. The logic is to add a new ChannelHandler, ServerBootstrapAcceptor, to the ChannelPipeline.

Then start registering the Channel with the above EventLoopGroup bossGroup, and the EventLoopGroup bossGroup will select an internal EventLoop to perform the actual registration behavior (that is, the current thread and the operating EventLoop are not the same thread, that is, the process is asynchronously submitting a Runnable). Once the registration is completed, execute the initChannel method of the above ChannelHandler

At this point, the entire bind process is completed. Once the Selector within the EventLoop detects the arrival of a new connection in the NioServerSocketChannel, it will be handed over to the ChannelPipeline of the NioServerSocketChannel to handle it. The key point is that the above ServerBootstrapAcceptor,ServerBootstrapAcceptor in the ChannelPipeline does the following:

1 configure the ChannelHandler specified by the childHandler in the above code for the ChannelPipeline of the new Channel

2 registers the new Channel with the above EventLoopGroup workerGroup

Sync introduction

The bind method returns a ChannelFuture, and we know from the above that the process is asynchronous, while the sync method waits until the asynchronous process is finished.

Take a look at f.channel (). CloseFuture (). Sync () this method

Each ChannelFuture is bound to a Channel, so you can get the corresponding bound Channel object through ChannelFuture.

Every Channel object has a CloseFuture closeFuture object. Instead of executing the close method, the above closeFuture method gets the CloseFuture closeFuture object, and then calls its sync method to wait for the end of the Future. Normally, the end method of this Future will not be called. Only when there are problems with the above process or other processes, such as failure to register with EventLoop, the end method of this Feture will be called. So normally, the main thread will always block on the sync method of CloseFuture closeFuture.

Misunderstanding

The above problem with the creation of bossGroup.

We all know that bossGroup is used to accept connections, and then bind the connection to workerGroup. In general, bossGroup can set the number of threads to 1 (basically only 1). We also know that multiple acceptor threads can be used in the Ractor model to perform accept operations to speed up accept.

If you want to speed up the speed of accept, want to open multi-thread to accept, at this time you want to set the number of threads of bossGroup to more than one, it is a big mistake, it is useless.

Combined with the above principle, a ServerSocketChannel is created only when the bind port is used, and then registered to an EventLoop inside the bossGroup. The single thread is still responsible for the accept work of the ServerSocketChannel, while the multithreading in the bossGroup only serves multiple ports of the bind.

Let's take a look at how tomcat allows multiple acceptor threads to perform accept operations:

1 creates a ServerSocketChannel serverSock and bind to a port

2 start multiple acceptor threads, and each thread logic is the accept method that executes the above serverSock

Instead of using Selector to perform accept operations, the accept method of the above serverSock can be executed concurrently by multiple threads.

Once Selector is used, it is basically equivalent to binding ServerSocketChannel serverSock to all Selector online processes (Selector is not thread-safe and can only be scheduled for execution in one thread)

This is the end of this article on "distributed Netty source code analysis". Thank you for reading! I believe that everyone has a certain understanding of the knowledge of "distributed Netty source code analysis". If you want to learn more knowledge, you are welcome to follow the industry information channel.

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

*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

Development

Wechat

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

12
Report