Netty source code analysis (1)

Today is the thirteenth chapter of Ape Lighthouse's "365 Original Projects."

 

In the following time, Beacon Jun continues to update the Netty series, a total of nine articles

 

Current: Netty source code analysis (1) begins

Netty source code analysis (2): Netty Channel

Netty source code analysis (3): Netty Future and Promise

Netty source code analysis (4): ChannelPipeline of Netty

Netty source code analysis (5): Netty's thread pool analysis

Netty source code analysis (6): Channel register operation

Netty source code analysis (7): NioEventLoop workflow

Netty source code analysis (eight): return to the channel register operation

Netty source code analysis (9): connect process and bind process analysis

 

Today! Dengta Jun told you:  

 

Netty source code analysis (1)


 

Foreword : This article will introduce Netty, the most widely used NIO package on the Java platform. It is a layer of encapsulation of the NIO implementation in the JDK, which allows us to develop NIO programs more conveniently. In fact, Netty is not just NIO, but basically everyone is coming to NIO.

 

Dengta Jun feels that the domestic bragging about Netty is a bit too much.It is mainly because many people rely on it for dinner, either training or publishing.I wish to blow Netty to the sky.This phenomenon is also very bad, but instead It makes beginners feel that Netty is the same as advanced technology.

 

The source code of Netty is not very simple, because it is more, and the relationship between the various categories is intricate.Many people say that its source code is very good. It's pretty, and every line is the essence, but they are different types, and there is nothing to compare.

 

Netty source code is good, because its interface is more flexible, often the interface is easy to use, the source code will not be too simple.

 

This article will be based on source code analysis, if the reader has some understanding of Netty, or used it, it would be better.

 

  • This article only introduces TCP related content, Netty's support for other protocols is beyond the scope of this article.
  • Unlike the source code analysis of concurrent packages, I cannot say source code line by line, so some exceptions will be skipped directly, unless I think it needs to be introduced.
  • The Netty source code has been updated and there are some differences between the versions. I introduced it according to the latest version 4.1.25.Final of 2018-09-06.

 

It is recommended that after reading this article, beginners can go through "Netty In Action" and the Chinese text version can also be found online.

 

 

1. Preparation


To learn the source code, it is definitely the preparation environment at the beginning.

Dengjun likes to use maven and also like Spring Boot, so I generally go to https://start.spring.io/ to prepare the simplest scaffolding.

Get the scaffolding in 10 seconds, and then import it into Intellij. If you use the new version of Spring Boot, you may also need to wait for the download dependencies. During this period, open https://mvnrepository.com/ and search for the maven dependencies you will use soon.

Netty is divided into several modules, including netty-handler, netty-buffer, netty-transport, netty-common, etc. There is also a netty-all, which contains all the modules.

Since we are analyzing the source code, it is natural to use the simplest one. Netty-all is not the best option, netty-example is:

1<dependency>2   <groupId>io.netty</groupId>3   <artifactId>netty-example</artifactId>4   <version>4.1.25.Final</version>5</dependency>

 

Not only can it solve our dependencies, but the examples in example are very suitable for us to learn to use.

 

 

2. Echo example


As a NIO library, Netty can naturally accept requests as a server or initiate requests as a client. Using Netty to develop a client or server is very simple. Netty does a good package. We usually only need to develop a Or multiple handlers are used to process our custom logic.

Next, let's take a look at an example that we often see. It is called Echo, that is, echo, what value is passed by the client, and what value is returned by the server as it is.

Open the source code of netty-example and copy the code below the echo package to play.
The left is the server code, the right is the client code

The code above is the basic template code each time it is a routine, we need to develop a unique part handler (...) and childHandler (...) method specified in each handler, such as EchoServerHandler and EchoClientHandler , of course, The Netty source code also provides us with a lot of handlers, such as the LoggingHandler above, which is provided for us in the Netty source code. It can be used directly when needed.

 

Let's take a look at some of the content involved in the above code:

  • The ServerBootstrap class is used to create server instances, and Bootstrap is used to create client instances.
  • Two EventLoopGroup: bossGroup and workerGroup, they are related to Netty's thread model. You can see that there are two groups on the server side and only one on the client side. They are the thread pool in Netty.
  • Channel in Netty does not directly use Java's native ServerSocketChannel and SocketChannel, but wraps NioServerSocketChannel and NioSocketChannel corresponding to it.
 Of course, there are also support for other protocols, such as NioDatagramChannel that supports the UDP protocol. This article only cares about TCP.
  • The handler (...) method on the left specifies a handler (LoggingHandler), which is used for processing when the server receives a new request, and the handler (...) method on the right specifies that the client needs to process the request. Used handlers.
If you want to specify multiple handlers in EchoServer, you can also use ChannelInitializer like EchoClient on the right
  • The childHandler (…) on the left specifies the childHandler. The handlers here are for newly created connections. We know that after accepting a connection, the ServerSocketChannel needs to create an instance of SocketChannel. The handler set in childHandler (…) is used for Handle newly created SocketChannel, not used to deal with ServerSocketChannel instance.
  • pipeline: handler can specify multiple (need the ChannelInitializer class above to assist), they will form a pipeline, they are actually similar to the concept of interceptors, now just remember that every NioSocketChannel or NioServerSocketChannel instance will have a pipeline instance pipeline The execution order of the handler is also involved.
  • ChannelFuture: This involves asynchronous programming in Netty, similar to the Future interface in the JDK.

 

For readers who don't know Netty, don't have any pressure. Lighthouse Jun will introduce them one by one. This article is mainly for novices. Lighthouse Jun finds it more difficult to understand or more important part, and will spend more space to introduce clearly.

The above source code does not show the processing of message sending and message receiving, this part of the lighthouse will be introduced after introducing the above content.

In the following, we will introduce these contents in blocks. In view of the fact that readers may have varying degrees of understanding of NIO or Netty, Dengta Jun needs to be verbose in order to take care of beginners in many places, so I hope that readers will read down section by section, and you can properly read the content you are familiar with. look forward to!

 

365 days of dry goods are constantly available, you can search for "Ape Lighthouse" on WeChat to read the first time, reply [Information] [Interview] [Resume] There are interview materials and resume templates for the first-line factories I prepared.

 

Guess you like

Origin www.cnblogs.com/yuandengta/p/12758736.html