Zbus. NET network communication module (i)

Source: Internet
Author: User

Project Address Https://git.oschina.net/rushmore/zbusCoreAPIThere are three characters:

Dispatcher -- responsible for -nio Network event Selector engine management, load Balancing for Selector engine

Ioadaptor- Network event processing, server and client sharing, responsible for reading and writing, message subcontracting packets, etc.

Session-- represents the network link, can read and write messages

For most of these three characters, there is only ioadaptor, the business logic, that needs to be handled individually.

servers, client tools, Server\clientthe above three core roles are created. Http module implements the HTTP protocol codec with the client, the code is very small, the entire Zbus . NET~ 50K, but its function is very powerful, the expansibility is flexible and changeable. (theproxy module is an example, even the entire Zbus project is NETapplication of the module)

1. warm up first, write a server. Not HelloWorld, a server code framework with high performance features.

The first step, the thing to do is very simple, what do you want to do business, directly cut into the theme, personalized a ioadaptor

In order to be able to demonstrate quickly and to see the effect through browsing, we implement an HTTP service thathttp://localhost/hello Called to return hello.

messageadaptor Span style= "; font-size:14px;font-family: ' Arial '" is an extension of ioadaptor Span style= "font-family: Arial" > complete http Codec of a suitable base class. We just need to derive a business application on top of this base class.

public class Myserveradaptor extends messageadaptor{public myserveradaptor () {uri ("/hello", New Messageproce            Ssor () {@Override public message process (message request) {request.setbody ("Hello");            return request;      }        }); } }

Step Two, let this ioadaptor run in Zbus. . NET engine.

Then create this engine (multiple servers can share an engine, so the design is separated)

Final Dispatcher Dispatcher  =  New   Dispatcher ();

Final Server Server  =  New Server (dispatcher);

take the first step of Ioadaptor is registered to this engine (Server control), it is easy to listen to multi-port (cool? )

Ioadaptor Ioadaptor  =  New myserveradaptor ();

Server . Registeradaptor ( ioadaptor);

Server . Registeradaptor (8080, ioadaptor);

The third step, let's run this engine (Server control) up

Server . Start ();

This time you will be able to access through the browser directly, of course, can also be accessed through the Client .

Simple? stress test look, general. I7box can be on 5w+ of the QPS .

Let's take a look at the source code.

Sample all source code (can go to Zbus Project test directory to play more examples) package org.zbus.net;import org.zbus.net.core.dispatcher;import  org.zbus.net.core.ioadaptor;import org.zbus.net.http.message;import  Org.zbus.net.http.message.messageprocessor;import org.zbus.net.http.messageadaptor;public class  myserveradaptor extends messageadaptor{ public myserveradaptor () {  uri ("/ Hello ",  new messageprocessor ()  {  @Overridepublic  message process (message  Request)  { request.setbody ("Hello"); return request;});   }  @SuppressWarnings ("resource") Public static void main (String[] args)  throws exception {   final dispatcher dispatcher = new  dispatcher ();    final server server = new server (Dispatcher);// The same business processing logic can easily listen to multiple addresses ioadaptor ioadaptor = new myserveradaptor (); Server.registeradaPtor (80, ioadaptor); Server.registeradaptor (8080, ioadaptor);     server.start ();  }}



Zbus. NET network communication module (i)

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.