In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-19 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 what the Netty distributed client access process is, 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 article on the Netty distributed client access process. Let's take a look.
Section 1: initialize NioSockectChannelConfig to create channel
Before analyzing the access process, let's add the knowledge of the first chapter about the creation of channel:
We analyzed the creation of channel in the first chapter, and there is a construction method in NioServerSocketChannel:
Public NioServerSocketChannel (ServerSocketChannel channel) {super (null, channel, SelectionKey.OP_ACCEPT); config = new NioServerSocketChannelConfig (this, javaChannel (). Socket ());}
At that time, we did not analyze the relevant knowledge of config. We first make a supplement to this in this chapter. Here we see that every NioServerSocketChannel has a config property, which stores the relevant configuration of NioServerSocketChannel. Here, a NioServerSocketChannelConfig object is created, and the underlying socket object corresponding to the current channel and channel is passed in. NioServerSocketChannelConfig is actually the inner class of NioServerSocketChannel.
We follow in the constructor of the NioServerSocketChannelConfig class:
Private NioServerSocketChannelConfig (NioServerSocketChannel channel, ServerSocket javaSocket) {super (channel, javaSocket);}
Let's continue to follow the constructor of its parent class DefaultServerSocketChannelConfig:
Public DefaultServerSocketChannelConfig (ServerSocketChannel channel, ServerSocket javaSocket) {super (channel); if (javaSocket = = null) {throw new NullPointerException ("javaSocket");} this.javaSocket = javaSocket;}
Here we continue to call the constructor of its parent class, save the underlying socket object of jdk, and call the constructor of its parent class DefaultChannelConfig
Follow public DefaultChannelConfig (Channel channel) {this (channel, new AdaptiveRecvByteBufAllocator ()); in the constructor of its parent class DefaultChannelConfig
Here you call your own constructor, passing in channel and an AdaptiveRecvByteBufAllocator object
AdaptiveRecvByteBufAllocator is a buffer allocator that allocates a buffer Bytebuf. The relevant information about Bytebuf will be explained in detail in later chapters. This can be briefly introduced as an understanding, just as a preview of later knowledge.
Bytebuf is equivalent to ByetBuffer of jdk. Netty re-encapsulates it to read and write byte streams in channel. Students who are familiar with Nio should be familiar with this. AdaptiveRecvByteBufAllocator is the buffer allocator used to allocate ByetBuff in netty. According to the name, it is not difficult to see that this buffer is a variable size byte buffer.
Let's follow up on the construction method of AdaptiveRecvByteBufAllocator:
Public AdaptiveRecvByteBufAllocator () {/ / DEFAULT_MINIMUM: minimum buffer length 64 bytes / / DEFAULT_INITIAL: initial capacity 1024 bytes / / maximum capacity 65536 bytes this (DEFAULT_MINIMUM, DEFAULT_INITIAL, DEFAULT_MAXIMUM);}
Here, you call your own constructor and pass in three attributes, which have the following meanings:
DEFAULT_MINIMUM: represents a buffer length of at least 64 bytes to be allocated
DEFAULT_INITIAL: represents the initial capacity of the buffer to be allocated is 1024 bytes
DEFAULT_MAXIMUM: represents a maximum buffer capacity of 65536 bytes to be allocated
We follow the this (DEFAULT_MINIMUM, DEFAULT_INITIAL, DEFAULT_MAXIMUM) method
Public AdaptiveRecvByteBufAllocator (int minimum, int initial, int maximum) {/ / ignore the validation code / / the subscript of the minimum capacity in table int minIndex = getSizeTableIndex (minimum); if (SIZE_ table [minIndex]
< minimum) { this.minIndex = minIndex + 1; } else { this.minIndex = minIndex; } //最大容量在table中的下标 int maxIndex = getSizeTableIndex(maximum); if (SIZE_TABLE[maxIndex] >Maximum) {this.maxIndex = maxIndex-1;} else {this.maxIndex = maxIndex;} this.initial = initial;}
Here, three properties are initialized, which are:
MinIndex: the subscript of the minimum capacity in size_table
MaxIndex: the subscript of the maximum capacity in table
Initial: initial capacity of 1024 bytes
The size_table here is an array, which contains a collection of memory sizes that can be allocated by byteBuf. Whether the allocated bytebuf is expanded or contracted, the memory size belongs to the elements in size_table, so how the array is initialized, we follow this attribute:
Private static final int [] SIZE_TABLE
We see a static member variable modified by final, and we follow it to the static block to see its initialization process:
Static {/ / List collection List sizeTable = new ArrayList (); / / starting at 16, each increment is added to List until it is greater than or equal to 512 for (int I = 16; I)
< 512; i += 16) { sizeTable.add(i); } //从512开始, 倍增添加到List中, 直到内存溢出 for (int i = 512; i >0; I > 1; int a = SIZE_ table [mid]; int b = SIZE_ table [mid + 1]; if (size > b) {low = mid + 1;} else if (size < a) {high = mid-1;} else if (size = = a) {return mid;} else {return mid + 1 }}}
Here is a binary search to get the following table
If (SIZE_ table [minIndex] < minimum) here determines whether the memory size of the minimum capacity subscript is less than the minimum value, and if it is less than the minimum value, the subscript + 1
The principle of obtaining the maximum capacity subscript is the same as above, to determine whether the memory size of the maximum capacity subscript is greater than the maximum value, and if so, the subscript-1
Let's go back to the construction method of DefaultChannelConfig:
Public DefaultChannelConfig (Channel channel) {this (channel, new AdaptiveRecvByteBufAllocator ());}
After dissecting the creation process of AdaptiveRecvByteBufAllocator () just now, let's move on to this ():
Protected DefaultChannelConfig (Channel channel, RecvByteBufAllocator allocator) {setRecvByteBufAllocator (allocator, channel.metadata ()); this.channel = channel;}
We see that channel is initialized here, and before channel initialization, the setRecvByteBufAllocator (allocator, channel.metadata ()) method is called. As the name implies, this is the method used to set the buffer allocator. The first parameter is the newly created AdaptiveRecvByteBufAllocator object we just analyzed, and the second is the ChannelMetadata object bound to channel. What is the ChannelMetadata object?
We follow up to the metadata () method, and since channel is NioServerSocketChannel, we call the metadata () method of NioServerSocketChannel:
Public ChannelMetadata metadata () {return METADATA;}
A member variable METADATA is returned, followed by the member variable:
Private static final ChannelMetadata METADATA = new ChannelMetadata (false, 16)
Here you create a ChannelMetadata object and pass in false and 16 in the constructor
Continue to public ChannelMetadata (boolean hasDisconnect, int defaultMaxMessagesPerRead) {/ / omit the verification code / / false this.hasDisconnect = hasDisconnect; / / 16 this.defaultMaxMessagesPerRead = defaultMaxMessagesPerRead;} in the constructor of ChannelMetadata
What you do here is very simple, initializing only two properties:
HasDisconnect=false
DefaultMaxMessagesPerRead=16
DefaultMaxMessagesPerRead=16 represents a maximum of 16 loops when reading each other's link or channel byte stream (whether server or client), as you will see in the following explanation
After dissecting the creation of the ChannelMetadata object, we go back to the constructor of DefaultChannelConfig:
Protected DefaultChannelConfig (Channel channel, RecvByteBufAllocator allocator) {setRecvByteBufAllocator (allocator, channel.metadata ()); this.channel = channel;}
Follow the setRecvByteBufAllocator (allocator, channel.metadata ()) method:
Private void setRecvByteBufAllocator (RecvByteBufAllocator allocator, ChannelMetadata metadata) {if (allocator instanceof MaxMessagesRecvByteBufAllocator) {((MaxMessagesRecvByteBufAllocator) allocator) .maxMessagesPerRead (metadata.defaultMaxMessagesPerRead ());} else if (allocator = = null) {throw new NullPointerException ("allocator");} rcvBufAllocator = allocator;}
First of all, it will determine whether the incoming buffer allocator is of type MaxMessagesRecvByteBufAllocator, which is true because AdaptiveRecvByteBufAllocator implements the MaxMessagesRecvByteBufAllocator interface.
Then convert it to the MaxMessagesRecvByteBufAllocator type
Then call its maxMessagesPerRead (metadata.defaultMaxMessagesPerRead ()) method
Here you will go to the maxMessagesPerRead (int maxMessagesPerRead) method of its subclass DefaultMaxMessagesRecvByteBufAllocator
The parameter metadata.defaultMaxMessagesPerRead () returns the property defaultMaxMessagesPerRead of ChannelMetadata.
That's 16.
Follow to the maxMessagesPerRead (int maxMessagesPerRead) method:
Public MaxMessagesRecvByteBufAllocator maxMessagesPerRead (int maxMessagesPerRead) {/ / ignore validation code / / initialize to 16 this.maxMessagesPerRead = maxMessagesPerRead; return this;}
Here, set its own property maxMessagesPerRead to 16, and then return itself
Go back to the construction method of DefaultChannelConfig private void setRecvByteBufAllocator (RecvByteBufAllocator allocator, ChannelMetadata metadata) {if (allocator instanceof MaxMessagesRecvByteBufAllocator) {((MaxMessagesRecvByteBufAllocator) allocator) .maxMessagesPerRead (metadata.defaultMaxMessagesPerRead ());} else if (allocator = = null) {throw new NullPointerException ("allocator");} rcvBufAllocator = allocator;}
After setting the maxMessagesPerRead property of the memory allocator, we finally set the member variable rcvBufAllocator of DefaultChannelConfig itself to our initialized allocator object
This is the end of the article on "what is the Netty distributed client access process?" Thank you for reading! I believe you all have a certain understanding of "what is the Netty distributed client access process". If you want to learn more, 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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.