An RPC that can communicate with each other between ASP. NET and Asp.netcore

Source: Internet
Author: User
Tags zookeeper

First, the characteristics

1. Cross-platform

2. Provide load Balancing algorithm

3. Support ZK Service Coordination

4. Provides JSON, Binaryserializer, and custom serialization methods

5, the client provides socket connection pool, so as to facilitate fast interaction, to prevent similar large file upload will block

6. Support Async Method

7. Support communication between. NET and. Netcore

8, support the use of MongoDB collect request information (using double buffer queue, the limit does not affect the transmission speed)

9, provide the request log monitoring backstage (not all complete)

II. Structure of the project

The compiler uses vs2017.

Nrpcitem is the NRPC source code under the framework. Netcorerpcitem is the source code under. Netcore. Nrpcserializer is a serialization method written by itself, and the speed and JSON serialization comparisons are slightly slower, but the speed can be received.

It is noteworthy that the Nrpcitem uses AUTOFAC as the IOC container, and the server code must be based on AUTOFAC to implement dependency injection.

Nrpc.adminmanage is a log monitoring background, currently only. Netcore version.

Iii. performance testing of custom serialization methods

Although the execution speed is slightly slower than JSON, it supports serialization of the byte[] type, and the client does not need any markup for ease of use. The implementation process refers to the serialization implementation of Csharp-hessian, which supports cross-platform.

Iv. How to use RPC

1, the service side:

A, reference Netcorerpc.application project, first define the good istudentapplication and his implementation class Studentapplication

B, Reference Netcorerpc.servertest project program, first binding the dependency injection information, if you want to use ZK, then Add. USEZK (); Extension method, otherwise do not use ZK service discovery

Private StaticIServiceProvider Builddi () {iservicecollection Services=Newservicecollection (); Services. Addsingleton<iloggerfactory, loggerfactory>(); Services. Addsingleton (typeof(ilogger<>),typeof(logger<>)); Services. Addsingleton<istudentapplication, studentapplication>(); varstr = configuration.getvalue<string> ("Mongodb:str"); varDbName = configuration.getvalue<string> ("Mongodb:databasename"); Services. Userpc (). Usemongodbmonitor (()=                    {                        return Newmonogodbconfig (str, dbName); });//. Usezk ();            varServiceProvider =Services.            Buildserviceprovider (); varLoggerfactory = serviceprovider.getrequiredservice<iloggerfactory>(); Loggerfactory.addnlog (Newnlogprovideroptions {capturemessagetemplates =true, capturemessageproperties =true }); Loggerfactory.configurenlog ("Nlog.config"); returnserviceprovider; }
binding Dependency Injection information

C, configuration file information, configuring ZK and MongoDB link configuration information

{  "Netcorerpc": {    "Zookeeper": {      "Connection":"192.168.100.34:2181",      "ParentName":"/netcorerpc/clienttest"    }  },  "MongoDB": {    "Str":"Mongodb://root:[email protected]:27017",    "DatabaseName":"Rpc_monitor"  }}

D. Turn on the monitor

        Private Static voidMain (string[] args) {Console.WriteLine ("Please enter the listening port:"); varStrport =Console.ReadLine (); varBuilder =NewConfigurationbuilder (). Setbasepath (Path.Combine (appcontext.basedirectory)). Addjsonfile ("Netcorerpc.json", Optional:true); Configuration=Builder.            Build (); varServicesprovider =Builddi ();            Dependencymanage.setserviceprovider (Servicesprovider, Configuration); Nrpcserver Nrpcserver=NewNrpcserver (int.            Parse (Strport)); Nrpcserver.start ("netcorerpc.application"); Console.WriteLine ("Welcome to use netcorerpc!"); Console.WriteLine ("Input exit to exit"); varstr =Console.ReadLine ();  while(!string. Equals (str,"Exit", StringComparison.OrdinalIgnoreCase)) {STR=Console.ReadLine ();        } nrpcserver.shutdown (); }
Turn on monitoring

2. How the client calls

A, refer to the Netcorerpc.clienttest project first configure some basic dependency information

Private StaticIServiceProvider Builddi () {iservicecollection Services=Newservicecollection (); Services.            AddOptions (); Services. Configure<RpcConfig> (Configuration.getsection ("Netcorerpc")); Services. Addsingleton<iloggerfactory, loggerfactory>(); Services. Addsingleton (typeof(ilogger<>),typeof(logger<>)); Services. Userpc (). Usemongodbmonitor (()=            {                varstr = configuration.getvalue<string> ("Mongodb:str"); varDbName = configuration.getvalue<string> ("Mongodb:databasename"); return Newmonogodbconfig (str, dbName); });//. Usezk ();            varServiceProvider =Services.            Buildserviceprovider (); varLoggerfactory = serviceprovider.getrequiredservice<iloggerfactory>(); //Configure NLogLoggerfactory.addnlog (Newnlogprovideroptions {capturemessagetemplates =true, capturemessageproperties =true }); Loggerfactory.configurenlog ("Nlog.config"); returnserviceprovider; }
Configuration Dependencies

B. configuration file contents

{  "Netcorerpc": {    "Requesttimeoumillis":10000,    "Default":"192.168.129.117:12345",    //"Group": [//  {    //" NameSpace": "",//"Address": "127.0.0.1:12345"//  }    //],    "Zookeeper": {      "Connection":"192.168.100.34:2181",      "ParentName":"/netcorerpc/clienttest"    }  },  "MongoDB": {    "Str":"Mongodb://root:[email protected]:27017",    "DatabaseName":"Rpc_monitor"  }}
configuration file

Configure server-side addresses, or you can configure server-side addresses based on the name of the class, because a client may invoke multiple services at many times, providing a server-side address based on the type name

C. How to start calling

First, the interface class and the model class naming control must be consistent with the server, such as the example of Istudentapplication and Testmodel two.

The interface class is then instantiated through the proxy class, and then the corresponding method is called.

        Private Static voidSend () {varStudentapplication = proxyfactory.create<istudentapplication>();            Console.WriteLine (Studentapplication.age ()); Console.WriteLine (Studentapplication.isyongpeople ( the)); varRunTask = Studentapplication.runasync (111); Studentapplication.say ("Hello World"); Studentapplication.say (Encoding.UTF8.GetBytes ("hi!"));            Console.WriteLine (Encoding.UTF8.GetString (Studentapplication.say ())); varTest =studentapplication.test (); Console.WriteLine (Test.            ToString ());            Studentapplication.sleep ();        Console.WriteLine (Runtask.result); }
Client Calls

The framework version is similar to the. Netcore version, so please refer to the test file for each project for practical use.

V. Invocation of CASE results

Vi. Service-Side monitoring

Monitoring application is not perfect, follow-up will be perfect, for the monitoring application must be based on MongoDB.

Vii. Project Open Source Address

HTTPS://GITHUB.COM/YJQGITHUB/NETCORERPC welcome everyone to spit groove, suggestions, if you are interested to improve together better.

And the version on NuGet is not yet released, and the original release is not the latest code

An RPC that can communicate with each other between ASP. NET and Asp.netcore

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.