Socket transmission based on Netty framework

Source: Internet
Author: User

I. INTRODUCTION of Netty Framework

What is Netty? First look at the explanation of Baidu Encyclopedia:

Netty is a Java open source framework provided by JBoss. Netty provides asynchronous, event-driven network application frameworks and tools for rapid development of high-performance, high-reliability network servers and client programs. In other words, Netty is a NIO-based client, server-side programming framework that uses Netty to ensure that you can quickly and easily develop a network application, such as a client that implements some kind of protocol, a service-side application.       Netty quite simplifies and streamlines the programming of Web applications, for example, the development of socket services for TCP and UDP. "Fast" and "simple" are not used to create maintainability or performance problems. Netty is an implementation experience that absorbs a variety of protocols, including Ftp,smtp,http, various binary, text protocols, and quite well-designed projects, and ultimately, Netty successfully finds a way to ensure ease of development while also guaranteeing the performance of its applications, Stability and Scalability

Why are so many big companies using the Netty framework? It is mainly based on the following characteristics of the Netty framework:

1) robustness, 2) fully functional, 3) customizable, 4) extensibility

II. Advantages of the framework

Traditional RPC performance is poor, mainly because the client and the remote call to adopt the synchronous blocking IO thread, when the client's concurrency pressure increases, synchronization congestion due to frequent waiting causes the I/O thread blocked, the thread can not work efficiently, the IO processing power will naturally degrade. Three factors that affect performance: First, IO model, IO Model determines the performance of the framework to some extent. Second, the protocol, such as: HTTP, TCP/IP, protocol selection, performance model is different, usually, the performance of the internal private protocol is better, this is due to internal design decisions. Third, the thread, the data message receives, reads, encodes, decodes, and so on, the threading model is different, the performance also is different. Compared to the traditional RPC framework, the advantages of Netty are mainly reflected in the following aspects:

    1. API is simple to use, package is perfect, low development threshold
    2. Powerful features, pre-set a variety of encoding and decoding functions, a variety of protocol support
    3. Strong customization capability, flexible expansion of channelhandler to communication framework
    4. High performance, reactor threading model scheduling, Channelfuture-listener, proactive push results via Listener mechanism
    5. Version mature and stable, community active, version update fast, the bug will be quickly repaired, at the same time, the addition of heart function, experienced a large-scale commercial application test, the quality of the full verification. has been widely used in the Internet, big data, enterprise applications, telecommunications software, online games and other popular industries, he can meet different business standards.
Three, Netty structure analysis

Netty is a framework based on the three Layer network architecture model, and the three Layer network architecture analysis includes the scheduling layer, the chain transport layer and the business logic layer.

    1. Reactor communication scheduling layer, is a model,

NIO thread pool Component {

Listen for network read and write connections

Business scheduling processing

Nio,aio, with NIO channel Niosocketchannel components

}

Netty enables IO multiplexing through an internal select patrol mechanism, enabling multiple IO blocking to be multiplexed to the same select, enabling the system to process multiple requests simultaneously, even in single-threaded situations. This makes the Netty realize the advantage of IO multiplexing, which greatly reduces the overhead of the system compared with traditional multithreading, because the system does not have to create new threads and destroy threads, which reduces the maintenance difficulty of the system and saves resources.

Bytebuffer pooling support, do not manually switch the flag bit, to achieve 0 copies. Traditional socket read and write, basically use heap memory, that is, the JVM will be in advance to copy the heap memory into memory, and then write to the socket, and Netty is the direct buffers, do not need to go through the JVM memory copy, in the outside of the heap memory directly socket read and write, This reduces the memory copy of the buffer one time, thus achieving 0 copies.

2.Pipleline Responsibility Chain Transfer

Interception handles forward backward events, external incoming message packet objects, Pojo information abstraction, upper layer also only need to handle logic, similar to SPRINGIOC processing beandefince. Different handler nodes have different functions, usually encoding and decoding, and so on, it can complete the conversion of external protocols into internal Pojo objects, so that the upper layer only need to focus on business logic, do not need to know the underlying protocol and threading model, to achieve decoupling.

3. Building logical Business Processing units

The underlying protocol isolation, the upper processing logical framework does not need to be concerned about what the underlying protocol is. The layered design of the Netty framework allows the developer to focus on the implementation of the Protocol framework, simply by focusing on the business logic development of the service layer and simplifying it.

Previously, a project was implemented based on the traditional socket and thread pool technology, but when high concurrency was found to be insufficient for concurrency, the TPS was found to be less than ideal when measured, so after consideration, it was decided to use the Netty framework to solve the problem. Similarly, Netty framework is also divided into client and server, after finishing, first to write a demo of the Netty framework, the following is the implementation of the code process.

The first is the service side, the server contains two aspects, first, the main role of servers is through the secondary boot program, set up NiO connection method to handle client requests, by binding a specific port, set the decoding mode and monitoring to achieve the entire thread processing requests; The server-side handler needs to inherit the Channelinboundhandleradapter class, and the primary function of the handler class is to read the client data, handle the business, throw exceptions, and respond to client requests. The code is as follows:

Service-Side server:

 Public classServer {Private StaticLog logger = Logfactory.getlog (Server.class);Private intPort; PublicServer (intPort) {        Super();  This. Port =Port;} Public  voidstart () {Serverbootstrap B=NewServerbootstrap ();//Bootstrap Helper ProgramEventloopgroup Group=NewNioeventloopgroup ();//to receive connections and process requests by NiO        Try{b.group (group); B.channel (Nioserversocketchannel.class);//set the Channnel of the NIO typeb.localaddress (NewInetsocketaddress (port));//Setting the Listening port//b.option (Channeloption.so_backlog, 2048);B.childhandler (NewChannelinitializer<socketchannel> () {//A channel is created when a connection arrives@Overrideprotected voidInitchannel (Socketchannel ch)throwsException {//Register Handlerch.pipeline (). AddLast (NewBytearraydecoder ()); Ch.pipeline (). AddLast (NewBytearrayencoder ()); Ch.pipeline (). AddLast (NewStringencoder (Charset.forname ("UTF-8"))); Ch.pipeline (). AddLast (NewServerhandler ()); }               });//. Option (Channeloption.so_backlog, 2048). Childoption (Channeloption.so_keepalive, true);Channelfuture F= B.bind (). sync ();//configuration complete, start binding server, block until bind succeeds by calling Sync Sync methodlogger.info (Server.class. GetName () + "Start listening:" +F.channel (). localaddress ()); F.channel (). Closefuture (). sync ();//The application waits until the channel is closed        } Catch(Exception e) {e.printstacktrace (); } finally {               Try {                      //Close Eventloopgroup, release all resources including the created threadgroup.shutdowngracefully (). sync (); } Catch(interruptedexception e) {e.printstacktrace (); }        }}}
View Code

Service-Side Handler

 Public classServerhandlerextendsChannelinboundhandleradapter {Private StaticLog Logger=logfactory.getlog (Serverhandler.class); @Override Public voidchannelactive (Channelhandlercontext ctx) {logger.info (Ctx.channel (). LocalAddress (). toString ()+ "Channel active ....");} @Override Public voidChannelinactive (Channelhandlercontext ctx)throwsException {logger.error (Ctx.channel (). LocalAddress (). toString ()+ "Channel inactive ....");}/*** * Read messages sent by clients*/@Override Public voidChannelread (Channelhandlercontext ctx, Object msg)throwsException {//Business Processing ClassLogger.info ("Start business processing ...."); NewSocketcontroller (ctx,msg). Run (); @Override Public voidExceptioncaught (Channelhandlercontext ctx, throwable cause)throwsException {//An exception occurs, closing the connectionLogger.error ("Server Side exception:" +cause.getmessage (), cause); Ctx.close ();} @Override Public voidChannelreadcomplete (Channelhandlercontext ctx)throwsException {logger.info ("Service-Side completion request!"); Ctx.flush ();}}
View Code

Client code

The client is mainly used to send data to the service side, also contains two aspects, first, the client mainly through the set port and IP and the server to establish a connection, the data packet encoding; second, ClientHandler need to inherit The Simplechannelinboundhandler<bytebuf> class, for different transmission modes, inherits different classes, and the handler class also handles business requests and responds to requests from the service side. The code is as follows:

Client clients:

 Public classClient {Private StaticLog Logger=logfactory.getlog (Client.class); PrivateString host; Private intPort;  PublicClient (String host,intPort) {        Super();  This. Host =host;  This. Port =Port; }     Public voidConnect () {Eventloopgroup WorkGroup=NewNioeventloopgroup (); Bootstrap Bootstrap=NewBootstrap ();        Bootstrap.group (WorkGroup); Bootstrap.channel (Niosocketchannel.class); Bootstrap.option (Channeloption.so_keepalive,true); Bootstrap.handler (NewChannelinitializer<socketchannel>() {@Overrideprotected voidInitchannel (Socketchannel ch)throwsException {logger.info ("The client triggers the connection ..."); Ch.pipeline (). AddLast (NewStringencoder (Charset.forname ("UTF-8"))); Ch.pipeline (). AddLast (NewClientHandler ());        }        }); //Client Start Connection        Try{logger.info ("Connect to Server ..."); Channelfuture Future=Bootstrap.connect (host,port). sync (); //wait for connection to closeFuture.channel (). Closefuture (). sync (); } Catch(interruptedexception e) {//TODO auto-generated Catch blockE.printstacktrace (); }finally{workgroup.shutdowngracefully (); }    }}
View Code

Client handler:

 Public classClientHandlerextendsSimplechannelinboundhandler<bytebuf> {    Private StaticLog Logger=logfactory.getlog (ClientHandler.class); /*** Send messages to the server*/@Override Public voidChannelactive (Channelhandlercontext ctx)throwsException {logger.info (Ctx.channel (). LocalAddress (). toString ()+ "Customer point active ..."); //write a string to the service sideLogger.info ("Client connection service side, start sending data ..."); String String= "Hello server! "; System.out.println ("Send data as:" +string); Bytebuf buf=ctx.alloc (). Buffer (string.length ());        Buf.writebytes (String.getbytes ());        Ctx.writeandflush (BUF); Logger.info ("Send Complete ..."); }        /*** Read messages returned by the server*/@Overrideprotected voidChannelRead0 (Channelhandlercontext arg0, bytebuf in)throwsException {logger.info ("Start receiving server data"); byte[] b=New byte[In.readablebytes ()];        In.readbytes (b); String String=NewString (b); Logger.info ("The data sent by the server is:" +string);    In.release (); } @Override Public voidExceptioncaught (Channelhandlercontext ctx, throwable cause)throwsException {logger.info ("Client Exception:" +cause.getmessage (), cause); } @Override Public voidChannelreadcomplete (Channelhandlercontext ctx)throwsException {logger.info ("Client completion request ....");        Ctx.flush (); }}
View Code

Service-side startup:

 Public class Servermain {privatestatic Log logger=logfactory.getlog (servermain.  Class); Private Static Server server =new server (55550);  Public Static void Main (string[] args) {        logger.info ("server start ....");        Server.start ();}}
View Code

Client Startup class:

 Public class Test {privatestaticnew Client ("127.0.0.1", 55550);  Public Static void throws unknownhostexception, IOException {        client.connect ();}}
View Code

Test results:

Service side:

Client:

Summarize:

The above is just a small demo of Netty framework, learning to use the Netty framework of the beginning, which involves a lot of technology and very many components, such as: Channels, callbacks, Futures, events and handlers, etc. Further learning is required, in addition, the encoding and decoding of messages, the way in which packets are wrapped, how they are split, the conversion of message formats, and the size limitations of message formats require further research and learning.

Socket transmission based on Netty framework

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.