Netty Principle and use

Source: Internet
Author: User
Tags throwable

Performance Topic Netty Principle and use

The Netty is a high performance event-driven asynchronous, non-clogging IO (NIO) framework for establishing low-level connections such as TCP, based on which Netty can build high-performance HTTP servers. Support for HTTP, WebSocket, protobuf, Binary TCP | and Udp,netty have been used by many high-performance projects as their sockets underlying base, such as Hornetq Infinispan vert.x
Play Framework Finangle and Cassandra. Its competitors are: Apache Mina and Grizzly.

The traditional blocked IO reads are as follows:

InputStream is = new FileInputStream ("Input.bin");
int byte = Is.read (); The current thread waits for the result to arrive until the error

The use of NiO is as follows:

while (true) {
Selector.select (); Request events from multiple channels
Iterator it = Selector.selectedkeys (). Iterator ();
while (It.hasnext ()) {
Selectorkey key = (Selectionkey) it.next ();
Handlekey (key);
It.remove ();
}

Clogging and non-clogging principle

The traditional hardware is blocked as follows, reading the data from memory, and then writing to the disk, and the CPU waits until the disk writes, the disk writes is slow, this time the CPU is blocked does not play the efficiency.

Using non-clogging DMA such as: The CPU just sends out a write operation such as instructions, do some initialization work, DMA specific execution, read data from memory, and then write to the disk, when the completion of write a interrupt event to the CPU. During this time the CPU is idle and can do other things. This principle is called Zero.copy 0 copy.

Netty based on the 0-copy principle of the above Java NIO:

Comparison
    • Tomcat is a Web server that takes a request to a thread and consumes a lot of memory when there are 1000 of clients. Typically a thread will spend 256kb to 1MB of stack space.
    • node. JS is a thread that serves all requests and has limitations on error handling
    • Netty is a thread that serves a number of requests, such as when a selector event is obtained from Java NIO, the channel channels are activated.

Demonstrate

The use code for Netty is as follows:

Channel Channel = ...
Channelfuture CF = Channel.write (data);
Cf.addlistener (
New Channelfuturelistener () {
@Override
public void Operationcomplete (channelfuture) throws Exception {
if (!future.issuccess () {
Future.cause (). Printstacktrace ();
...
}
...
}
});
...
Cf.sync ();

By introducing observer monitoring, when there is data, the code in the listener is automatically activated.

We use Netty to create a server code:

Public class Echoserver {

Private Final int Port;

Public Echoserver (int port) {
this. Port = port;
}

Public voidRun ()throwsException {
Configure the server.
Eventloopgroup Bossgroup =NewNioeventloopgroup ();
Eventloopgroup Workergroup =NewNioeventloopgroup ();
Try{
Serverbootstrap B =NewServerbootstrap ();
B.group (Bossgroup, Workergroup). Channel (Nioserversocketchannel.class). Option (channeloption.So_backlog, 100)
. Handler (NewLogginghandler (LogLevel.INFO). Childhandler (NewChannelinitializer<socketchannel> () {
@Override
Public voidInitchannel (Socketchannel ch)throwsException {
Ch.pipeline (). AddLast (
New Logginghandler (Loglevel.info),
New Echoserverhandler());
}
});

Start the server.
Channelfuture f = b.bind (port). sync ();

Wait until the server socket is closed.
F.channel (). Closefuture (). sync ();
} finally {
Shut down all event loops to terminate all threads.
Bossgroup.shutdowngracefully ();
Workergroup.shutdowngracefully ();
}
}


}

This code call: start at Port 9999

New Echoserver (9999). Run ();

The code we need to complete is Echoserverhandler:

Public class Echoserverhandler extends channelinboundhandleradapter {

Private Static Final Logger Logger = Logger. GetLogger (Echoserverhandler. class. GetName ());

@Override
Public void Channelread (channelhandlercontext ctx, Object msg) throws Exception {
Ctx.write (msg);
}

@Override
Public void channelreadcomplete (Channelhandlercontext ctx) throws Exception {
Ctx.flush ();
}

@Override
Public void exceptioncaught (channelhandlercontext ctx, throwable cause) {
Close the connection when a exception is raised.
logger. Log (level. WARNING, "unexpected exception from downstream.", cause);
Ctx.close ();
}
}

Principle

The principle of a Netty server is as follows:

Each request in the graph is read by upstream, and then activates our service logic such as Echoserverhandler, while the server writes out the data, that is, the response is implemented through downstream. Channel channels each contain a pair of upstream and downstream, as well as our handlers (Echoserverhandler), as these are encapsulated by channel pipeline, The data flow flows in the pipeline, and each socket corresponds to a channelpipeline.

Channelpipeline is the key, and it resembles a Unix pipeline that has the following effects:

    • Keep channelhandlers for each channel, such as Echoserverhandler
    • All the events are going through it.
    • Constant modification: A Unix-like SH pipeline: echo "Netty is shit ..." | Sed-e ' s/is/is the/'
    • A channel corresponds to a channelpipeline
    • Includes protocol encoding, decoding, security verification, SSL/TLS, and application logic

Client code

Before we demonstrated the server-side code, here is the client code:

Public class echoclient {
Private Final String host;
Private Final int Port;
Private Final int firstmessagesize;

Public Echoclient (String host, int port, int firstmessagesize) {
this. Host = host;
this. Port = port;
this. firstmessagesize = Firstmessagesize;
}

Public voidRun ()throwsException {
Configure the client.
Eventloopgroup Group =NewNioeventloopgroup ();
Try{
Bootstrap B =NewBootstrap ();
B.group (Group). Channel (Niosocketchannel.class). Option (channeloption.Tcp_nodelay,true). Handler (NewChannelinitializer<socketchannel> () {
@Override
Public voidInitchannel (Socketchannel ch)throwsException {
Ch.pipeline (). AddLast (
New Logginghandler (Loglevel.info),
New Echoclienthandler(firstmessagesize));
}
});

Start the client.
Channelfuture f = b.connect (host, port). sync ();

Wait until the connection is closed.
F.channel (). Closefuture (). sync ();
} finally {
Shut down the event loop to terminate all threads.
Group.shutdowngracefully ();
}
}
}

Application logic Echoclienthandlerfor the client:

Public class Echoclienthandler extends channelinboundhandleradapter {

Private Static Final Logger Logger = Logger. GetLogger (Echoclienthandler. class. GetName ());

Private Final Bytebuf Firstmessage;

/**
* Creates a client-side handler.
*/
PublicEchoclienthandler (intFirstmessagesize) {
if(firstmessagesize <= 0) {
Throw NewIllegalArgumentException ("firstmessagesize:" + firstmessagesize);
}
Firstmessage = unpooled.Buffer(firstmessagesize);
for(inti = 0; I < firstmessage.capacity (); i++) {
Firstmessage.writebyte ((byte) (i);
}
}

@Override
Public void channelactive (Channelhandlercontext ctx) {
Ctx.writeandflush (Firstmessage);
System. out. Print ("active");
}

@Override
Public void Channelread (channelhandlercontext ctx, Object msg) throws Exception {
Ctx.write (msg);
System. out. Print ("read");
}

@Override
Public void channelreadcomplete (Channelhandlercontext ctx) throws Exception {
Ctx.flush ();
System. out. Print ("Readok");
}

@Override
Public void exceptioncaught (channelhandlercontext ctx, throwable cause) {
Close the connection when a exception is raised.
logger. Log (level. WARNING, "unexpected exception from downstream.", cause);
Ctx.close ();
}

}

Original address: https://www.jdon.com/concurrent/netty.html

Netty principle and use

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.