. NET next several service framework introduction

Source: Internet
Author: User
Tags net domain
Brief introduction


After the company's services more, in order to invoke the convenience, at the same time for future service governance, will generally use a number of service framework, here mainly introduce I know a few service framework, briefly analyze the basic concepts of these service frameworks.


It can be put into production environment.


The following two service frameworks, I have seen companies into the production environment, so for stability, there should be no need to worry too much.

ServiceStack Https://github.com/ServiceStack/ServiceStack


ServiceStack may not have been used, but it's two other components that everyone should have used, Servicestack.redis (Redis Access Tool), Servicestack.text (JSON serialization tool), Servicestack is a service framework that can be easily used to create services, which are HTTP-based and provide client-side calls, with serialization of data containing JSON, XML, binary, PROTOBUF, and a description of the services created.


1 HTTP requests, there are two things critical, request path and parameters, for Servicestack, the parameter is the object, that is, it is to pass the parameters are encapsulated in a class, in addition to tag on the class, the label content is the request path, so that when the client calls, reflect the request path and parameters, Can initiate a call.


Because the servicestack itself has provided the demo, so here does not write the demo, we can learn a bit.

Hessian https://sourceforge.net/projects/hessiancsharp/


Hessian is a serialization tool and a service framework that provides a multi-lingual implementation, including. NET, which seems to be less well-known in the. NET domain and probably hasn't been updated for a long time.

When using Hessian, the first need to define the interface, and then two copies of the server to implement these interfaces, the client refers to these interfaces, and then the client uses the Realproxxy class proxy, all interface calls will eventually invoke the proxy class Invoke method, In the Invoke method to get the name of the method to invoke, parameters, etc., after serialization sent to the server on a unified URL, the URL will end in Hessian, so you need to configure a handle in Web. config to intercept the request, after the request is intercepted, Deserializes the parameter and then initiates the call through reflection.


Call here to refer to this blog http://www.cnblogs.com/lxsfg/archive/2008/08/27/1277777.html

There are many other service frameworks, such as THRIFT,JSON-RPC but because they are not used, there is no comment here.


Some service frameworks within the blog park

The following three frames I found when browsing the blog park, and are open-source, for the Learning Service Framework is a good project, in addition I do not often on the blog park, the following 3 frame is also the opportunity to see under the coincidence, blog Park should have other excellent service framework just I did not find it, if you have found that, Write to the comments section and learn from them.

Rabbit.rpc http://www.cnblogs.com/ants/p/5605754.html

And hessin Some like, also first define the service interface, then a two parts, the service side through the inheritance interface to achieve the specific logic, for the client, he is through reflection, extract all the methods within the interface, as well as the method parameters and return value information, and then by the way of dynamically generated code, generate a proxy class, Then dynamically compile and put into the container. About the generated proxy class, all methods are a unified implementation, that is, call an Invoke method to send methods and parameters to the underlying, the underlying assembly is sent to the server. The way to generate code here is more fun, you can learn.


In addition, the framework integrates service governance at the same time, the service is registered to zookeeper, and the client is called by Zoopeeper to get the specific service address. In fact, this is not only a service framework, but also includes the service governance function.


I also just rough time the code, the details have not understood thoroughly, want to learn more about the source can see.

Dyd.BaseService.ServiceCenter Http://git.oschina.net/chejiangyi/Dyd.BaseService.ServiceCenter

See the introduction of basic functions are, but look at the author of this project is to say that the project is research-based, for the service invocation of the client, but also through the code generation way, specific to see the code.

In addition to introduce this Bo friend, he in the blog Park also has Bo

Guest http://www.cnblogs.com/chejiangyi/, the next open source of a number of projects Http://git.oschina.net/chejiangyi are some very good projects, such as scheduling services, configuration services, monitoring services, For the internet companies on the dot scale, these services are needed and, more valuable, these services are based on. NET, so for some use. NET development of the Internet company, there is a good reference significance.

Redola.rpc http://www.cnblogs.com/gaochundong/p/redola_yet_another_csharp_rpc_framework.html

This is what I recently discovered, the framework uses the Actor model, the serialization uses the protobuf-net, the transmission mode is based on TCP, TCP framework uses his own cowboy.websockets, see Introduction also implemented the service center, But because I do not understand the Actor model, so here is not the lily, interested students can be regarded as the blog.


I wrote the service framework myself

See so many service framework, oneself preface also wrote a, just a service call and is experimental nature, can serve as a reference.

The principle of the framework and Hession similar, first to define the interface, and then the server implementation interface, here I took a coincidence, I directly let the controller in the Web API inherit the interface, so there is no need to specifically define a handle to intercept the request, but also does not affect the Web API access, So even if you use this framework, you will not be prevented from accessing it directly in the form of HTTP. The client is called in a similar manner to hession, and it is necessary to define the proxy and then collect the parameters

First define the interface

With the interface, next is the implementation, here I use the Web API, we define a controller, this controller in addition to inherit Apicontroller, but also inherit the interface, in this controller, the implementation of interface specific logic.

At the same time, this interface is located in the DLL copy to the client, here to introduce a function, that is, RealProxy, this is. NET comes with a proxy class and is also a mechanism used by hession.

Using system;using system.linq;using system.reflection;using system.runtime.remoting.messaging;using System.runtime.remoting.proxies;using newtonsoft.json;using restsharp;namespace SimleRPC{public class Simpleproxy:r        Ealproxy {private readonly Uri _uri;        Public Simpleproxy (type type, URI uri): base (type) {This._uri = URI; The public override IMessage Invoke (IMessage msg) {//msg parameter contains information about the calling method, which is encapsulated to make the information richer by some IMEs            Thodcallmessage methodmessage = new Methodcallmessagewrapper ((IMethodCallMessage) msg);            MethodInfo MethodInfo = (MethodInfo) methodmessage.methodbase;  parameterinfo[] Paramsinfo = Methodinfo.getparameters (); Get the method call parameter//assemble the path class name that is the controller name, the method name is the action name to indicate the specific URL string path = "/api/" + methodinfo.de            Claringtype.name + "/" + methodinfo.name; Request type, post or get bool Ishttpget = MethodInfo.CustomAttributes.Any (CustomAttributeData =&Gt            CustomAttributeData.AttributeType.Name = = "Httpgetattribute");            var client = new Restclient (_uri); var request = new Restrequest (path, ishttpget?)            Method.GET:Method.POST); Build Parameters//web API for all rules reference http://www.cnblogs.com/babycool/p/3922738.html HTTP://WWW.CNBLOGS.COM/LANDEANF en/p/5337072.html Two Blog if (ishttpget) {//Here the default GET request parameters are basic type for (int i = 0; i < paramsinfo.length; i++) {request. Addparameter (Paramsinfo[i].                Name, Methodmessage.args[i]);                }} else {//for post requests, either without parameters (of course, this is not the case), or only one parameter (these are limited by the Web API) if (Paramsinfo.length > 0) {request.                Addjsonbody (Methodmessage.args[0]); }}//Send request to get results irestresponse response = client.            Execute (Request); var content = Response. ContEnt     Type returntype = Methodinfo.returntype;            Gets the call method return type, deserializing the result according to the type of object returnvalue;            if (Isbasetype (returntype)) {returnvalue = Convert.changetype (content, returntype); } else {returnvalue = jsonconvert.deserializeobject (content, returntype);//If it is an object , the JSON is deserialized} return new ReturnMessage (ReturnValue, Methodmessage.args, Methodmessage.argcount, ME        Thodmessage.logicalcallcontext, Methodmessage);        }//<summary>///Determines whether the underlying type (int long double), string is a reference type, but also belongs to the underlying type///</summary> <param name= "type" ></param>///<returns></returns> private static bool I Sbasetype (Type type) {if (type = = typeof (String) | | Type.            Isprimitive) return true;        return false; }    }}

All method calls invoke the Invoke method, and within the Invoke method, you can get specific information about the calling method, such as parameters, return value types, and so on. Then, through reflection and assembly, an HTTP request is made, where my default interface class name is the name of the controller, and the interface method name is the name of the action.

Finally, the request is sent out through Restsharp. Finally, the value required for the method return value is deserialized based on the HTTP result.

In fact, for the server to the first, whether the Web API inherits the interface is not important, if not inherit, then the signature of the interface is consistent with the signature of the method in the Web API.

Through my demo, the method is adjustable, if someone interested in this, you can test some more cases.


At last


Now a lot of internet companies have their own RPC framework, some are open source, some because of historical issues, their own writing, for the means of communication, there are HTTP-based, there are TCP-based, there are two protocols are compatible. Serialization is also a variety of ways, I only listed above 5, in fact, on GitHub search, there are many excellent RPC.


RPC is only one phase of the project development process. With RPC, you can do a lot of things on this basis, such as:


Service governance all services in the start of the registration to the service center, the client at startup, from the registry to obtain the real address, direct calls, not through nginx and other agents, here can get real address to do some permission restrictions, such as which clients can use, which clients do not use, can use how many, Here you can refer to Dubbo.


HTTP request path now microservices are very popular, front-end a request, may have to go through the backend several services, can be added to the HTTP header RequestID and Requestindex, these services, such as A->b->c,a service invoke B service, If you find that there is no requestid in the HTTP head, you can generate one through the GUID, while the Requestindex plus 1, b service calls the C service side, because RequestID already have, directly passed down, while Requestindex plus 1, This information is recorded in the log, through the analysis, the entire call is strung together, through the complete data can be drawn out the entire service call link graph.


In addition to the link graph, because we can intercept calls to each service, we can record the service call time-consuming, plus the link graph, the entire service information will be more perfect.

The above is. NET next several service framework introduction content, more related content please concern topic.alibabacloud.com (www.php.cn)!

  • Related Article

    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.