Netty Introduction
Netty is an asynchronous Event-driven Network application framework
For rapid development of maintainable high performance protocol servers & clients.
Netty official website said. It probably means that Netty is an asynchronous event-driven network application framework that enables high-speed development of maintainable, high-performance network server and client applications.
Asynchronous asynchronous, a very large number of methods in Netty are asynchronous, and with the event-driven model you can handle many other requests.
The Netty consistency API has a very high user experience compared to the JDK API.
It is also very convenient to use. Netty allows us to not consider too many underlying issues and various bugs. Allows developers to focus more on business logic.
Netty Status
This is Netty on GitHub's homepage https://github.com/netty/netty. Now there's 5000+ star.
Many well-known companies and projects are using Netty, including large companies such as Facebook, IBM, Redhat, and Spark, Finagle, Nifty and other projects.
A lot of other adaptor are in http://netty.io/wiki/adopters.html.
At present, the main maintenance version number of Netty is 3.x, 4.x, 5.x.
I touch a lot more is 5.x, very many frameworks are based on 3.x development. 3 4 5 There are some differences between
I think the new version number has been developed in the past experience and lessons. The basic 5 used.
Netty do something
Http://netty.io/images/components.png
Netty encapsulates the JDK's NIO, providing developers with a consistent and well-developed API.
Netty provides a much better "JDK API" implementation. For example, its bytebuf.
What's the definition of fast?
Fast, I want to get a thing as soon as possible and I want to get all the things as soon as possible.
In Serverclient programming. The former can feel is low latency, lower delay, as soon as finished a request; And the latter is high throughput. Greater system throughput.
Scalability Scalability
We need the system to reduce performance in a large number of requests, and we can get a performance boost when we upgrade our hardware configuration.
Server with different paradigm1. Single thread mode
Handle assumes that it is not running in a new thread then the while loop will block on handle processing, only one request can be processed at a time.
</pre>
2. Multi-threaded operationA request for a corresponding thread. When a large number of requests emerge, the creation, destruction, and contextswitch of overhead all affect system performance.
ServerSocket serversocket = new ServerSocket (port), while (true) { socket socket = serversocket.accept (); New Thread () { @Override public void Run () { handle (socket); }
3. Thread poolThe thread pool did not solve the problem of a request thread. The only limit is to reduce the overhead of thread creation and the creation of control threads.
Executor Executor = executors.newfixedthreadpool (100); ServerSocket serversocket = new ServerSocket (port), while (true) { socket socket = serversocket.accept (); Executor.execute (New Runnable () { @Override public void Run () { handle (socket); } );}
4.JDK NIOThink for a second. The problem is on the handle (socket). The read write operation of InputStream and OutputStream byte-based reads and writes is a block operation, and when no bytes is able to read or write, the thread will block.
Graph from "Netty in Action"
JDK1.4 provides NIO implementations, and NiO had two sayings at the time, new IO and non blocking IO, which have been going on for so many years now. is no longer new, everyone calls non blocking IO.
Describes several core Java.nio packages that contain some buffer, the core being bytebuffer, whether the program interacts with the network layer or in the form of a byte stream. Bytebuffer has both heap buffer and direct buffer (non heap buffer). Head buffer is in the Java heap.
Using a byte array as its internal data structure, direct buffer is outside the Java heap memory.
There are two more important classes of channel and selector in Java.nio.channels. Channel represents a and can read and write the target of the channels, the implementation class has FileChannel, Serversocketchannel, Socketchannel, etc., selector used to register the channel of interest events. This allows us to implement asynchronous Event-driven, implementing a single thread that handles multiple requests, multiplexing (multiplexing)
Serversocketchannel Serverchannel = Serversocketchannel.open (); Serverchannel.bind (new inetsocketaddress (port)); Serverchannel.configureblocking (FALSE); Selector Selector = Selector.open (); Serverchannel.register (Selector, selectionkey.op_accept); while (true) { Selector.select (); set<selectionkey> Selectionkeyset = Selector.selectedkeys (); iterator<selectionkey> Iterator = Selectionkeyset.iterator (); while (Iterator.hasnext ()) {Selectionkey Selectionkey = Iterator.next (); Iterator.remove (); if (selectionkey.isacceptable ()) {Serversocketchannel Server = (Serversocketchannel) selectionkey.channel (); Socketchannel client = Server.accept (); Client.configureblocking (FALSE); Client.register (Selector, selectionkey.op_read| Selectionkey.op_write, Bytebuffer.allocate (buffer_size)); } if (Selectionkey.isreadable ()) {Socketchannel client = (Socketchannel) selectionkey.channel (); Bytebuffer buf = (bytebuffer) SelectIonkey.attachment (); Client.read (BUF); } if (Selectionkey.iswritable ()) {Socketchannel client = (Socketchannel) selectionkey.channel (); Bytebuffer buf = (bytebuffer) selectionkey.attachment (); Buf.flip (); Client.write (BUF); Buf.compact (); } }}
This CPU usage is more serious
5. Netty NiOIn order to demonstrate the ability to put in a block. Netty Our byte parsing business implementation can be implemented with Channelhandler, Channelhandler series in channelpipeline form a kind of plug-in form. By using the filter chain, each logic can be reused independently of each other.
int port = 8090; Eventloopgroup Bossgroup = new Nioeventloopgroup (); Eventloopgroup Workergroup = new Nioeventloopgroup (); Serverbootstrap serverbootstrap = new Serverbootstrap () try{serverbootstrap.group (Bossgroup, WorkerGroup). Chan Nel (nioserversocketchannel.class). Childhandler (New channelinitializer<io.netty.channel.socket.socketchannel& gt; () {@Override protected void Initchannel (Io.netty.channel.socket.SocketChannel ch) throws Except Ion {Ch.pipeline (). AddLast ("Echohandler", new Channelhandleradapter () {@Override public void Channelreadcomplete (Channelhandlercontext ctx) throws Exception {Ctx.flush (); } @Override public void Channelread (Channelhandlercontext ctx, Object ms g) throws Exception {Ctx.write (msg); } @Override public void Exceptioncaught (ChAnnelhandlercontext CTX, Throwable cause) throws Exception {cause.printstacktrace (); Ctx.close (); } }); } }); Channelfuture f = serverbootstrap.bind (new inetsocketaddress (port)). sync (); F.channel (). Closefuture (). sync (); catch (Interruptedexception e) {e.printstacktrace ();} finally {bossgroup.shutdowngracefully (); Workergroup.shutdowngracefully ();}
Not to be continued.
。
Continuning ...
Many other recommended materials
Netty in action
http://g.oswego.edu/
Netty Development tutorial (i)