0x00 php.cn/code/10224.html "target=" _blank "> Introduction
DOTBPE.RPC is an RPC framework written based on Dotnet Core, and its dad DOTBPE, the goal is to implement an out-of-the-box microservices framework, but it almost means, but only in the stage of conception and trial. But anyway RPC is the foundation of MicroServices, let's talk about RPC implementation. The DOTBPE.RPC underlying communication default implementation is based on Dotnetty, which is a netty implementation of C # developed by the Microsoft Azure team and is very cool. Of course you can also switch to other socket communication components. The default protocol name used by DOTBPE.RPC is called AMP, and codec uses Google's Protobuf3, but these default implementations are all replaceable.
Source Address: Github.com/xuanye/dotbpe.git
0X01 about the AMP protocol and Google Protobuf
AMP (A Message Protocol)
AMP (A message Protocol), the Chinese name 一个消息协议
, is the DOTBPE.RPC default implementation of the message protocol, in real development, it is not necessary to understand how the message is encoded and transmitted, but understanding the protocol can help to understand the framework. The basic structure of the Protocol is as follows:
0 1 2 3 4 5 6 7 8 9 ten <length>-14+------------+----------+---------+------+--- ----+---------+------------+| <ver/argc> | <length> | <seq> |<type>|<serid>| <msgid> | <data> |+------------+----------+---------+------+-------+---------+------------+
The AMP protocol default message header is 14 bytes long and does not contain an extension header
No. 0 bit: VER/ARGC//For the version number, for the time being, the default is 0
第1-4位: Length//For the total package (including Baotou length)
第5-8位: Sequence//is the message sequence number, which corresponds to request <---> response
9th bit: type//message types, present value of 5, as follows:
Request = 1, Response = 2, Notify = 3,notfound = 4, ERROR = 5
第10-11位: serviceid//Message ID ushort type
第12-13位: msgid//Message ID ushort type
In the AMP protocol, Serviceid identifies a class of requests, similar to the modules in the application, while the MsgId identifies the specific methods in the module
Then follow the actual data
Google Protobuf
Google Protocol Buffer (protobuf) is a mixed-language data standard within Google, with more than 48,162 message format definitions and more than 12,183. proto files already in use. They are used for RPC systems and continuous data storage systems.
Protocol buffers is a lightweight and efficient structured data storage format that can be used for structured data serialization, or serializing. It is ideal for data storage or RPC data interchange formats. It can be used in the communication protocol, data storage and other fields of the language-independent, platform-independent, extensible serialization structure data format. Currently available in a variety of languages, including C + +, C #, GO, JAVA, PYTHON
My previous blog using CSharp to write Google Protobuf plugin has been introduced if by writing a plugin way, by defining the proto file, and generate the code we need.
In Dotbpe.rpc, I use PROTOBUF as the description file for the service and generate the server and client proxy classes through a custom plug-in approach.
0x02 Quick Start
0. Prerequisites
Because DOTBPE is developed based on dotnet core, you must already have a dotnet core development environment
Use GitHub managed code, so you must already have a GIT client installed
Template code needs to be generated via PROTOC, so you must already have the Google protobuf command line tool installed
1. Download the sample program
In order to be able to explain our Quick Start program, you need a copy of the sample code that can be run locally. Download the sample code I have written from GitHub, which allows you to quickly build your program without the tedious, but necessary, steps.
>$ # clone The repository to get the example code: >$ git Clone https://github.com/xuanye/dotbpe-sample.git >$ CD Dotbpe-sample
Using VS2017, or Vscode to open the downloaded code, the directory structure looks like this:
If you use VS2017 can automatically help you restore, if you use Vscode, you need to run dotnet restore
download dependencies, after successful use dotnet build
compile a look at the results: look at the perfect
2. Running the program
Running the server
>$ CD hellodotbpe.server >$ dotnet Run
Run Client
>$ CD hellodotbpe.client >$ dotnet Run
Congratulations! You have already run an server/client application using DOTBPE.RPC.
3. Let's take a look at the code.
3.1 Service description File Proto
The first is the DOTBPE.RPC framework of the proto extension files, all projects need this file, about how to extend the proto, my blog has a more detailed introduction, here does not repeat that
Dotbpe_option.proto file Syntax = "Proto3";p ackage dotbpe;option csharp_namespace = "dotbpe.protobuf"; import "google/ Protobuf/descriptor.proto ";//Extended service extend Google.protobuf.ServiceOptions { int32 service_id = 51001; bool disable_generic_service_client = 51003; Disallow generation of client code bool Disable_generic_service_server = 51004;//prohibit generating server-side code}extend google.protobuf.MethodOptions { Int32 message_id = 51002;} Extend Google.protobuf.FileOptions { bool disable_generic_services_client = 51003;//Disallow generation of client code BOOL Disable_ Generic_services_server = 51004; Prohibit generation of server-side code bool Generic_markdown_doc = 51005;//whether to generate documentation This example is useless bool generic_objectfactory = 51006; Whether to generate objectfactory in this example is not useful}
The following service description file greeter.proto
is the real example of the service description file: Simpler, defining a greeter RPC service, and defining a Hello method
Greeter.protosyntax = "Proto3";p ackage dotbpe;option csharp_namespace = "Hellodotbpe.common";//Import Extended Import Public " Dotbpe_option.proto ";//define a service Greeter { option (service_id) = 100;//message ID, global must be unique //sends a greeting RPC Hello (hellorequest) returns (Helloresponse) { option (message_id) = 1;//Set Message ID, unique}}//within the same service Request message containing the user ' s name.message hellorequest { string name = 1;} The response message containing the Greetingsmessage helloresponse { string message = 1;}
Using the Protoc tool to generate the template code, the code in the sample is generated in the Hellodotbpe.common_g directory, and the local students who can run the shell command can go directly to
Dotbpe-sample\script\generate Directory Run sh generate_hello.sh
(general installation under Windows Cgywin), can not run the classmate also in the HELLODOTBPE directory, directly run the command line
Protoc-i=. /protos--csharp_out=./hellodotbpe.common/_g/--dotbpe_out=./hellodotbpe.common/_g/ . /protos/dotbpe_option.proto. /protos/greeter.proto --plugin=protoc-gen-dotbpe=. /.. /tool/protoc_plugin/protobuf.gen.exe
Of course, I still recommend that you install the following Cgywin running environment, you can run some common commands on UNIX. Some scripts for the common development environment can be deployed to the formal environment at the same time.
3.2 Service-side code
Service implementations:
Service implementation code public class Greeterimpl:greeterbase {public override task
Service-Side Startup class
public class Startup:istartup {public void Configure (Iappbuilder app, ihostingenvironment env) { }< C4/>public IServiceProvider configureservices (iservicecollection services) { services. ADDDOTBPE (); Add Dotbpe.rpc's core dependency services. Addserviceactors<ampmessage> (actors = { actors. Add<greeterimpl> (); Registration Service Implementation }); Return services. Buildserviceprovider (); } }
Start the service side
Class program { static void Main (string[] args) { console.outputencoding = System.Text.Encoding.UTF8 ; In the console output debug log DotBPE.Rpc.Environment.SetLogger (new DotBPE.Rpc.Logging.ConsoleLogger ()); var host = new Rpchostbuilder () . Useserver ("0.0.0.0:6201")//Bind the ground port 6201 . Usestartup<startup> () . Build (); Host. Startasync (). Wait (); Console.WriteLine ("Press any key to quit!"); Console.readkey (); Host. Shutdownasync (). Wait (); } }
3.3 Client Code
Class Program {static void Main (string[] args) {console.outputencoding = Encoding.UTF8; var client = ampclient.create ("127.0.0.1:6201"); Establish link channel var greeter = new Greeterclient (client); Client proxy class while (true) {Console.WriteLine ("Input your name and press ENTER :"); String name = Console.ReadLine (); if ("Bye"). Equals (name)) {break; } try {var request = new Hellorequest () {name = name}; var result = Greeter. Helloasync (Request). Result; Console.WriteLine ($ "---------------Receive form Server:{result. Message}-----------"); } catch (Exception ex) {Console.WriteLine ("Error occurred:" + ex. Message); } } Console.WriteLine ($ "---------------close connection-----------"); Client. Closeasync (); } }
0X03 Next
In the next article I will detail the main classes and invocation relationships in DOTBPE.RPC and describe how to implement RPC communication using Dotnetty.
In fact, I'm writing a more complex example of Https://github.com/xuanye/PiggyMetrics.git,
This was originally a sample program for Spring cloud, which I used to DOTBPE to describe the application of DOTBPE in real-world scenarios. Including service registration and discovery, inter-service call, public httpapi, monitoring and inspection functions, and through the practice to further improve the DOTBPE. Preliminary functions have been implemented, but not yet written and documented. The implementation of the system is described in detail later in the series.