11 Questions for Netty

Source: Internet
Author: User
Tags serialization

1. What Netty is.

Netty is an asynchronous communication framework based on the Java NiO Class library, which is characterized by asynchronous non-blocking, event-based driven, high-performance, high reliability and high customization.


2. What you can do with Netty.

Development of asynchronous, non-blocking TCP network applications;

Development of asynchronous, non-blocking UDP network applications;

Develop asynchronous file transfer applications;

Developing asynchronous HTTP server and client applications;

Provide integration of a variety of coding and decoding frameworks, including Google's protobuf, jbossmarshalling, Java serialization, compression codec, XML decoding, string encoding and decoding, these codec framework can be directly used by users;

Provide a variety of coding and decoding basic class library, can be very convenient to achieve the private protocol stack codec framework for two times customization and development;

Based on the pipeline-handler mechanism of responsibility chain mode, users can intercept and customize the network events very conveniently.

All IO operations are asynchronous, the user can use the Future-listener mechanism active get result or the IO thread operation completes after the active notify result, the user's business thread does not need to wait synchronously;

IP black and white list control;

Print message code stream;

flow control and shaping;

Performance statistics;

Heartbeat detection based on link idle event detection

......

3. Netty in which industries have been applied.

Internet industry: As the scale of the Web site continues to expand, the number of concurrent access to the system has become more and more high, the traditional web containers based on the vertical structure of the container has been unable to meet the needs, need to split the application for service to improve the development and maintenance efficiency. From the network situation, vertical architecture split, the system adopts distributed deployment, the need for remote service calls between the nodes, High-performance RPC framework is essential, Netty as an asynchronous high-performance communication framework, often as the basic communication components are used by these RPC frameworks.

Typical applications are: Ali Distributed service Framework Dubbo RPC Framework uses Dubbo protocol to communicate between nodes, Dubbo protocol uses Netty as the basic communication component to implement the internal communication between each process node by default. Its architectural diagram is as follows:

Figure 1-1 Call graph between Dubbo nodes


Between the service provider and the service consumer, the service provider, the service consumer, and the Performance statistics node use Netty for asynchronous/synchronous communication.

In addition to Dubbo, Taobao message middleware ROCKETMQ between the message producer and the message consumer, also uses Netty for high-performance, asynchronous communication.

In addition to Ali Department and Taobao department, many other large internet companies or electric dealers have also used a lot of netty to build high-performance, distributed network servers.

Gaming industry: The Java language has been used more and more widely, whether it is a hand tour service or a large network game. As a high-performance base communication component, Netty itself provides TCP/UDP and HTTP protocol stacks, which facilitates the customization and development of private protocol stacks. Account login server, Map server can easily through the Netty for high-performance communication, the schematic diagram is as follows:

Figure 1-2 Application of Netty in Game server architecture

Large data areas: the classic Hadoop high-performance communication and serialization component Avro RPC framework, by default Netty for cross node communication, its Netty service based on the Netty Framework two encapsulation implementation.

Large data computing often uses a number of compute nodes and A/N summary node for distributed deployment, there is a huge amount of data exchange between the nodes. Because the comprehensive performance of Netty is the highest in all mature nio frameworks, it is often chosen to be used as communication between nodes of large data.

Enterprise software: Enterprise and IT integration needs esb,netty, simplicity and high performance for Multi-Protocol support, private protocol customization is the preferred communication component of the ESB RPC framework. In fact, many enterprise bus vendors will choose Netty as the basic communication component for enterprise IT integration.

Communication industry: The advantages of Netty asynchronous high-performance, high reliability and high maturity, so that it in the communications industry has been a large number of applications.

4. Using a traditional socket development is quite simple, why should I switch to NIO for programming?

First we look at the traditional threading model diagram based on synchronous blocking IO (BIO):

Figure 1-3 Synchronous blocking io (BIO) threading Model Diagram

As we can see from the above diagram, there are several problems in the traditional synchronous blocking IO communication:

The threading model has a fatal flaw: A model connecting a thread causes the server to be unable to withstand the concurrent connections of a large number of clients;

Poor performance: Frequent thread context switching results in inefficient CPU utilization;

Poor reliability: Because all IO operations are synchronized, business threads are at risk of being synchronized blocking as long as they are IO, which can lead to poor reliability of the system, depending on the processing power of external components and network conditions.

With non-blocking io (NIO), the three defects of synchronous blocking IO will be solved:

NIO uses reactor mode, a reactor thread aggregates a multiplexer selector, which can register, listen, and poll hundreds of channel at the same time, an IO thread can concurrently process N client connections, threading model optimization to 1:N (n < The maximum number of handles available to the process) or m:n (M is usually the CPU kernel + 1, N < Maximum number of handles available for the process);

Due to the limited number of IO threads, there will be no context switching and competition between frequent IO threads, and high CPU utilization;

All IO operations are asynchronous, and even if business threads are directly IO-operated, they are not blocked by synchronization, and the system no longer relies on external network environments and the processing performance of external applications.

Since switching to NIO can result in significant reliability and performance improvements for the system, the current use of NIO for communication has become increasingly mainstream.

5. Why not directly based on JDK's NIO class library programming?

We answered this question through the JDK NiO server and the client's work sequence diagram:

Figure 1-4 JDK NIO service-side creation and communication sequence diagrams

Even without the code and the NIO class library complexity, a high-performance, highly reliable NIO service-side development and maintenance costs are very high, developers need to have a wealth of NIO programming experience and network maintenance experience, many times even need to grasp the package to locate the problem. It may take 1 months to develop a set of NIO programs, but its stability is likely to take 1 years or more, which is why I do not recommend an important reason for communication development directly using the JDK NiO class library.

Let's look at the communication sequence diagram for the JDK NIO client: It's also very complex.

Figure 1-5 JDK NIO client creation and communication sequence diagrams

6. Why choose the Netty frame.

Netty is one of the industry's most popular NIO frameworks, and its robustness, functionality, performance, customization, and scalability are all the same in similar frameworks, and it has been validated by hundreds of commercial projects, such as the RPC framework for Hadoop Avro using Netty as a communications framework. Many other industry-leading RPC and distributed service frameworks also use Netty to build high-performance asynchronous communication capabilities.

The advantages of Netty are summarized as follows:

API simple to use, low development threshold;

Powerful, preset a variety of codec functions, support a variety of mainstream protocols;

With strong customization ability, the communication framework can be extended flexibly through Channelhandler.

With high performance, the Netty is optimal in comparison with other major NIO frameworks in the industry.

Mature, stable, netty fixes all the JDK NiO bugs that have been discovered, and business developers don't need to worry about nio bugs anymore;

Community active, version iteration cycle short, found bugs can be repaired in time, while more new features will be added;

Has experienced a large-scale commercial application test, the quality has been validated. In the Internet, large data, network games, enterprise applications, telecommunications software and many other industries have been successful business, proved that it fully meet the commercial standards of different industries.

Because of these advantages, Netty is becoming the preferred framework for Java NIO programming.

7. I heard that Netty versions of the API changes more frequently, how do I choose the version.

In fact, the API changes between versions of Netty are not as scary as some people say, and the biggest change is 3. The X-series to 4.x/5.x changes, Netty not only refactored the package path, but optimized and modified the class library, which had previously been wanted to change but was not modified in the light of forward compatibility. The main reason for this change is that Netty is independent of JBoss development, which is a good thing for the long-term development of Netty.

In my opinion, the architecture and API design of the netty4.x series is more reasonable, and it provides more new features. Therefore, I personally suggest that users can choose 4. X-Series versions to prevent future upgrades from encountering difficulties and problems.

For already used 3. Users of the X-series version, if the existing functionality already meets the requirements, there is no need to upgrade for the short term. If you need to use more new features and features, it is recommended that you upgrade after a full evaluation, which may require some effort.

Since the latest version of Netty5 is still in the beta phase and can be tried from a learning and research perspective, Netty5 is more likely to be forward compatible than Netty4, so it will be easier for future users to upgrade to Netty5.

8. Netty and Mina, which one should I choose?

According to my experience, whichever you choose is the right choice. Both have their own advantages, Netty in memory management more than a chip, comprehensive performance is also better. However, the management and compatibility of API changes is not doing too well. Compared to Netty,mina compatibility, cohesive maintainability features more, such as JMX integration, performance statistics, state machines and so on.

It is suggested that users can make the best choice according to their familiarity with the two and the actual project requirements. If you lock the two, it means that you have made the right choice and that you don't have to dwell on which one to choose and quarrel with the leader and colleagues.

9. is Netty easy to use?

Netty's basic development and application is very simple, the development of an echo server requires only 28 lines of code, the development of the corresponding ECHO client requires only 26 lines of code.

However, if you want to use it for private stack development, HTTP server and client development, you still need to learn some advanced Netty libraries and functions to understand the design principle of netty. Only in this way can the proper use of Netty, for the project and the company to bring greater value.

10. There is no netty related books for study and reference.

2014 5-June, China's first learning Netty textbook, "Netty Authority Guide" will be published by the electronic Industry publishing house Bowen perspective.

The book consists of 23 chapters, from the historical evolution of Java IO, including the introduction of NiO basics, Netty Basics, the use and development of Netty codec framework, UDP development, asynchronous file transfer, Netty based asynchronous HTTP protocol stack development and application, customization and use of half-packet decoder, Private protocol stack design and development, industry application, architecture analysis, core class library function explanation and source analysis.

Whether you are a beginner or a master of NiO, you can draw nutrients from this book and provide fast access to Netty and even proficient in it.


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.