A powerful tool for building highly available distributed systems--netty

Source: Internet
Author: User
Tags epoll

Characteristics:

    • High-performance, event-driven, asynchronous non-blocking Java open source framework
    • NIO-based client, server-side programming framework
    • Stability and Scalability

It is often used to establish a TCP/IP underlying connection to establish a high-performance Http server.

Due to high performance, asynchronous non-blocking and other characteristics, many high-performance projects as the underlying communication base, such as Ali's Dubbo.

Main areas of activity:

    • High performance areas
    • Multithreading concurrency Domain
    • The field of asynchronous communication
IO communication

As for this piece, I have seen a very good article written in the past to explain:

About synchronous, asynchronous, parallel, concurrency, and more

The following is a brief introduction.

BIO

A thread is responsible for the connection.

is a thread responsible for listening, it receives the request from the client, creates a new thread for each request to process, after processing is completed through the output stream back to the client, a typical request to answer the request model .

This model lacks elastic scalability, the number of processing threads on the server and the number of concurrent accesses to the client are 1:1, and the number of threads is inflated resulting in a significant decrease in efficiency, even when the thread's stack overflows and the new thread cannot be created.

Pseudo-Asynchronous IO communication

Use the thread pool to be responsible for connections.

encapsulates a user's request into a task and then throws it into the thread pool for processing (which also uses the queue), corresponding to the M request N reply relationship .

Because the thread pool is controllable, there is generally no resource exhaustion at high concurrency, but the thread pool is blocked .

PS: All require a thread to do the listening, it will encapsulate the request, and post it to the thread pool.

NIO Communication

The key three words: Buffer, channel, multiplexer (Selector).

It will have an object with buffer buffers, containing some data to be written or read out,all of the data in the NIO library is handled in buffer , and in the flow operation of the programming layer, either read or write is in the operation buffer.

Channel channels and streams are different, it is bidirectional , and the stream can only be unidirectional, so the channel may be read, write operations, or both.

Multiplexer Selector, simply stated, it will constantly poll the channel on it, if read or write on a channel to indicate that the channel is in the ready state, and then through the Selector can get the ready state of the channel collection, Then proceed with the subsequent operation.

It has no limit on the maximum number of connections and is a huge step forward in the IO field.

AIO Communication

Connection registration Read-write events and callback functions, the read-write method is really asynchronous, will actively notify the program.

AIO uses the system's underlying model (the asynchronous socket channel corresponds to the event-driven IO in Unix, is the true asynchronous non-blocking IO), and actively notifies the process , so it does not require the multiplexer to achieve asynchronous read-write, simplifying the NIO programming model .

Compare the number of clients (client: Server):

Bio:1:1

Pseudo-Asynchronous Io:m:n

Nio:m:1

aio:m:0

Type:

BIO: Blocking synchronization

Pseudo-Asynchronous IO: Blocking synchronization

NIO: Non-blocking synchronization

AIO: Non-blocking asynchronous

Integrated use

Use difficulty: BIO < pseudo asynchronous IO < AIO < NIO

Debug Difficulty: BIO < pseudo asynchronous IO < NIO 约= AIO

Reliability: BIO < pseudo asynchronous IO < NIO 约= AIO

Throughput: BIO < pseudo asynchronous IO < NIO 约= AIO

Netty Getting Started

Native NIO's class library and API complex, need to master buffer, selector, channel; entry threshold high (also requires skilled Java multi-threading, network programming)

The workload and difficulty of the reliability capability is high, and there may be some bugs with JDK NIO.

and Netty,api simple, low entry threshold, high performance, mature and stable.

Here is a small program using Netty + WebSocket: GitHub address

WebSocket

HTML5 protocol specification, through the handshake mechanism, the client and the server to establish a TCP-like connection, convenient for client and server communication.

It is to solve the problem of real-time communication between the client and the server , sending a special request via HTTP or HTTPS, and then establishing a TCP connection for exchanging data after the handshake, and then the client and the server communicate in real-time through this connection.

Avoids the "real-time" effect by polling, saving communication overhead, because servers can proactively send data to clients, and they can push information to each other at any time.

When it is closed, the server shuts down the underlying TCP, and the client can also initiate TCP close.

About NIO

A better answer to the quote: https://www.zhihu.com/question/24322387/answer/282001188

NIO is not a unique Java concept, and NiO represents a term called IO multiplexing.

It is a system call provided by the operating system , the name of the earlier operating system call is SELECT, but the performance is low, and then gradually evolved into Linux under the Epoll and Mac Kqueue.

We are generally said to be epoll, because no one takes the Apple computer as a server to use external services. Netty is a set of frameworks based on Java NIO technology encapsulation.

Why encapsulation? Because native Java NIO is less convenient to use, and the infamous Bug,netty encapsulates it, it provides an easy-to-use usage pattern and interface that users can use more easily.

What exactly is NIO?

The full name of NIO is noneblocking io, non-blocking IO; the difference with Bio,bio is Blocking io, blocking IO.

What does this block mean?

    1. Accept is blocked, only the new connection comes, accept will return, the main thread to continue
    2. Read is blocked, only the request message comes, read can return, and the child thread can continue processing
    3. Write is blocked, only the client has received the message, write can return, and the child thread can continue to read the next request

so the traditional multi-threaded server is Blockingio mode, all the threads are blocked from beginning to end. These threads are just waiting, consuming the operating system's scheduling resources, doing nothing, and wasting.

So how does NIO do non-blocking? It uses the event mechanism .

It can use a thread to Accept, read and write operations, request processing logic all dry . If nothing is done, it does not die in cycles, it sleeps the thread until the next event comes up and then continues to work , a thread called the NIO thread.

while (true) {  takeEvents(fds);  // 获取事件,如果没有事件,线程就休眠  for (event : events) {    if (event.isAcceptable()) {      doAccept// 新链接来了    elseif (event.isReadable()) {      doRead// 读消息      if (request.isComplete()) {        doProcess();      }    elseif (event.isWriteable()) {      doWrite();  // 写消息    }  }}

The above paragraph is the pseudo-code of this process, and there are many differences with the real code.

About Netty

Netty is based on NIO, and Netty provides a higher level of abstraction over NiO.

In Netty, the Accept connection can be handled using a separate thread pool, and read and write operations are another thread pool to handle.

The Accept connection and read and write operations can also be processed using the same line pool. The request processing logic can be processed either with a separate thread pool or with a read-write thread. Each thread in a thread pool is a NIO thread. The user can assemble according to the actual situation, construct the concurrency model which satisfies the system requirement.

Netty provides a built-in common codec, including a line codec [one request], a prefix length codec [first N bytes to define the requested byte length], a replay decoder [record the status of a half-packet message],http codec, WebSocket message codec, etc.

Netty provides some column lifecycle callback interfaces, when a complete request arrives, when a connection is closed, when a connection is established, the user receives a callback event and then handles the logic.

Netty can manage multiple ports at the same time, and the NIO client model can be used, which is necessary for RPC services.

Netty can handle UDP sockets in addition to handling TCP sockets.

In the process of message reading and writing, it is necessary to use a lot of bytebuffer,netty to optimize and abstract the bytebuffer performance and usability.

In short, Netty is a must-have for Java programmers. If you know it, and want to know why, we must study the Netty. If you think Java is dull and pointless, Netty is the key to unlocking your Java interests.

A powerful tool for building highly available distributed systems--netty

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.