Netty Study 2

Source: Internet
Author: User
Tags ftp protocol

First, Netty layered design

The Netty is designed using a more typical three-layer network architecture, as shown in the logical architecture diagram:

#第一层, the Reactor communication scheduling layer, which is done by a series of auxiliary classes, including Reactor thread Nioeventloop and its parent class, Niosocketchannel/nioserversocketchannel, and its parent Classes, Bytebuffer, and various Buffer, Unsafe, and various internal classes derived from it. The main function of this layer is to listen to the network read and write and connect operations, is responsible for the network layer of data read into the memory buffer, and then trigger a variety of network events, such as connection creation, connection activation, read events, write events and so on, to trigger these events to PipeLine, by PipeLine Act as a chain of responsibilities for subsequent processing.

#第二层, responsibility chain PipeLine, it is responsible for the event in the chain of responsibility of the orderly dissemination, while responsible for the dynamic orchestration of responsibility chain, the responsibility chain can choose to listen and handle their own concerns of events, it can intercept processing and the backward/forward propagation events, different applications of the Handler Nodes are also functionally different, often developing codec hanlder for message encoding, which can convert external protocol messages into internal POJO objects, so that the upper business side only needs to be concerned with business logic, without the need to perceive underlying protocol differences and threading model differences, Enables layered isolation at the architectural level.

# The third layer , the business logic processing layer. Can be divided into two categories: pure business logic processing, such as order Processing, Application layer protocol management, such as HTTP protocol, FTP protocol, etc.

Second, Netty threading model

There are three commonly used reactor threading models, such as the following:

#Reactor单线程模型: Reactor single-threaded model, which means that all I/O operations are done on the same NIO thread. For some small-capacity scenarios, you can use a single-threaded model.

#Reactor多线程模型: The biggest difference between a rector multithreaded model and a single-threaded model is that there is a set of NIO threads that handle I/O operations. It is mainly used for high concurrency and large traffic scenarios.

#主从Reactor多线程模型: The main feature of the master-slave reactor threading model is that the server is no longer a 1 separate NIO thread for receiving client connections, but rather a separate NIO thread pool. With the master-slave NIO threading model, 1 server-side listener threads are unable to effectively handle all client connection performance problems.

In fact, the threading model of Netty is not fixed, and it is possible to support the above three reactor threading models by creating different Eventloopgroup instances in the boot helper classes and configuring them with the appropriate parameters.

In most scenarios, parallel multithreading can improve the concurrency performance of the system. However, if concurrent access to shared resources is handled improperly, it can lead to severe lock contention, which can eventually result in degraded performance. In order to avoid the performance loss of lock competition as much as possible, it can be accomplished by serialization design, that is, the processing of message is done in the same thread as possible, and the thread switching is not done, so the multi-threading competition and the synchronous lock are avoided.

To maximize performance, the Netty adopts a serial, non-locking design that performs serial operations within the I/O thread to avoid performance degradation caused by multi-threaded contention. On the surface, the serialization design seems to be low CPU utilization and not enough concurrency. However, by adjusting the thread parameters of the NIO thread pool, you can simultaneously start multiple serialized threads running concurrently, a locally unlocked serial threading design that performs better than one queue-multiple worker threading models.

Third, Netty design of scalability

With the extended nature of Netty, you can customize your security policy:

-The threading model can be extended

-Serialization mode is extensible

-Upper stack can be extended

-Provides a large number of network event slices to facilitate user function expansion

Netty's architectural scalability design concept is as follows:

1. Determine the extension point, pre-set the relevant expansion interface, to the user two times custom and extended use

2. The main function points are based on the interface programming, user-friendly customization and expansion.

Netty Study 2

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.