DotBPE.RPC quick start

大家讲道理
Release: 2017-05-31 14:37:00
Original
2403 people have browsed it

0x00 Introduction

DotBPE.RPC is an RPC written based on dotnet coreFramework, and its father, DotBPE, aims to implement an out-of-the-box microservice framework, but it is still not very interesting and is still in the stage of conception and experimentation. But anyway, RPC is the basis of microservices. , let’s talk about the implementation of RPC first. The default implementation of DotBPE.RPC underlying communication is based on DotNetty, which is the C# Netty implementation developed by the Microsoft Azure team. Of course, you can also replace it with other Sockets. Communication component. The default protocol name used by DotBPE.RPC is Amp, and the encoding and decoding use Google's Protobuf3, but these default implementations can be replaced.

Source code address: github.com/xuanye/dotbpe.git#.

##0x01 About Amp Protocol and Google Protobuf

Amp(A Message Protocol)

Amp(A Message Protocol), the Chinese name is

一Message Protocol is the message protocol implemented by DotBPE.RPC by default. In actual development, it is not necessary to understand how messages are encoded, decoded and transmitted, but understanding the protocol will help to further understand the framework. The basic structure of the protocol is shown in the figure below:

      0        1 2 3 4   5 6 7 8     9     10 11     12 13   <length>-14
+------------+----------+---------+------+-------+---------+------------+
| <ver> | <length> |  <seq>  |<type>|<serid>| <msgid> |   <data>   |
+------------+----------+---------+------+-------+---------+------------+</data></msgid></serid></type></seq></length></ver></length>
Copy after login
The default message header length of the Amp protocol is 14 bytes, excluding the extension header

The 0th bit: ver/argc // is the version number, for the time being, the default is 0
Digits 1-4: length // is the total length of the packet (including header length)
Digits 5-8: sequence // is the message sequence number, through which the sequence number corresponds to the request response
9th position: type // Message type, there are 5 current values, as follows:

Request = 1, Response = 2, Not

ify = 3,NotFound = 4, ERROR = 5Bits 10-11: serviceId//message ID ushort type
Bit 12-13: msgId//message ID ushort type
In the Amp protocol, serviceId identifies a type of request , similar to the module in the application, and msgId identifies the specific method in the module

followed by the actual data

Google Protobuf

Google Protocol Buffer(abbreviation Protobuf) is Google's internal mixed language data standard. Currently, there are more than 48,162 message format definitions and more than 12,183 .proto files in use. They are used in RPC systems and persistent data storage systems.

Protocol Buffers is a lightweight and efficient structured data storage format that can be used for structured data serialization, or serialization. It is suitable for data storage or RPC data exchange format. A language-independent, platform-independent, and extensible serialized structured data format that can be used in communication protocols, data storage and other fields. Currently,

API is provided in multiple languages, including C++, C#, GO, JAVA, PYTHON

In my previous blog, I used CSharp to write the Google Protobuf plug-in. Let’s introduce how to define the proto file and generate the code we need by writing a plug-in.

In DotBPE.RPC, I use protobuf as the service description file, and generate server and client proxy classes through customized plug-ins.

0x02

Quick Start

0. Prerequisite

Because DotBPE is developed based on dotnet core, you must already have dotnet core locally The development environment

uses github to host the code, so you must have
installed the git client Need to generate template code through protoc, so you must have installed the google protobuf command line tool

1. Download the sample program

In order to explain our quick start program, you need a sample code that can be run locally. Downloading the sample code I have written from github allows you to quickly build the program and avoid some tedious but necessary steps.

>$ # Clone the repository to get the example code:    
>$ git clone https://github.com/xuanye/dotbpe-sample.git  
>$ cd dotbpe-sample
Copy after login
Use VS2017 or VSCode to open the downloaded code.

The directory structure is as follows:
DotBPE.RPC quick start

If you use VS2017, it can automatically help you Restore, if you use VSCode, you need to run

dotnet restore to download the dependencies. After success, use dotnet build to compile and see the result: it looks perfect
DotBPE.RPC quick start

2. Run the program

Run Server

>$ cd HelloDotBPE.Server   
>$ dotnet run
Copy after login

Run Client

>$ cd HelloDotBPE.Client   
>$ dotnet run
Copy after login

Congratulations! Have used DotBPE.RPC to run a Server/Client application.

3. Let’s take a look at the code

3.1 Service description file proto

The first is the proto extension file in the DotBPE.RPC framework. All projects require this File, regarding how to extend proto, my blog has a more detailed introduction, so I won’t repeat it here

//dotbpe_option.proto 文件

syntax = "proto3";
package dotbpe;


option csharp_namespace = "DotBPE.ProtoBuf";

import "google/protobuf/descriptor.proto";

//扩展服务
extend google.protobuf.ServiceOptions {
  int32 service_id = 51001;
  bool disable_generic_service_client = 51003; //禁止生成客户端代码
  bool disable_generic_service_server = 51004; //禁止生成服务端代码
}
extend google.protobuf.MethodOptions {
  int32 message_id = 51002;
}

extend google.protobuf.FileOptions {
  bool disable_generic_services_client = 51003; //禁止生成客户端代码
  bool disable_generic_services_server = 51004; //禁止生成服务端代码
  bool generic_markdown_doc = 51005; //是否生成文档 本示例中无用
  bool generic_objectfactory = 51006; //是否生成objectfactory 本示例中无用
}
Copy after login

下面的服务描述文件 greeter.proto 才是真正的示例的服务描述文件:比较简单,定义一个Greeter Rpc服务,并定义一个Hello的方法

//greeter.proto
syntax = "proto3";
package dotbpe;

option csharp_namespace = "HelloDotBPE.Common";

// 引入扩展
import public "dotbpe_option.proto";

// 定义一个服务
service Greeter {
  option (service_id)= 100 ;//消息ID,全局必须唯一
  // Sends a greeting
  rpc Hello (HelloRequest) returns (HelloResponse) {
    option (message_id)= 1 ;//设定消息ID,同一服务内唯一
  }

}

// The request message containing the user&#39;s name.
message HelloRequest {
  string name = 1;
}
// The response message containing the greetings
message HelloResponse {
  string message = 1;
}
Copy after login

通过protoc工具生成模板代码,示例中的代码生成到了 HelloDotBPE.Common_g 目录下,本地可以运行shell命令的同学可以直接到
dotbpe-sample\script\generate 目录运行sh generate_hello.sh (windows下一般安装cgywin),不能运行的同学也可以在HelloDotBPE目录下,直接运行命令行

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
Copy after login

当然我还是建议大家安装以下cgywin运行环境,可以运行unix上的一些常用命令。同时在部署到正式环境的时候可以公用开发环境的一些脚本。

3.2 服务端代码

服务实现:

// 服务实现代码
public class GreeterImpl : GreeterBase 
{ 
   public override Task<HelloResponse> HelloAsync(HelloRequest request)
   {
        // 直接返回Hello Name
       return Task.FromResult(new HelloResponse() { Message = "Hello " + request.Name });
   }
}
Copy after login

服务端启动类

 public class Startup : IStartup
    {
       
        public void Configure(IAppBuilder app, IHostingEnvironment env)
        {
           
        }

        public IServiceProvider ConfigureServices(IServiceCollection services)
        {
            services.AddDotBPE(); // 添加DotBPE.RPC的核心依赖
            services.AddServiceActors<AmpMessage>(actors => {
                actors.Add<GreeterImpl>(); // 注册服务实现
            });

            return services.BuildServiceProvider();
        }
    }
Copy after login

启动服务端

   class Program
    {
        static void Main(string[] args)
        {
            Console.OutputEncoding = System.Text.Encoding.UTF8;

            //在控制台输出调试日志
            DotBPE.Rpc.Environment.SetLogger(new DotBPE.Rpc.Logging.ConsoleLogger());

            var host = new RpcHostBuilder()
                .UseServer("0.0.0.0:6201") //绑定本地端口6201
                .UseStartup<Startup>()
                .Build();

            host.StartAsync().Wait();

            Console.WriteLine("Press any key to quit!");
            Console.ReadKey();

            host.ShutdownAsync().Wait();

        }
    }
Copy after login

3.3 客户端代码

 class Program
    {
        static void Main(string[] args)
        {
            Console.OutputEncoding = Encoding.UTF8;

            var client = AmpClient.Create("127.0.0.1:6201"); //建立链接通道
            var greeter = new GreeterClient(client); //客户端代理类
           
            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("发生错误:" + ex.Message);
                }
            }
            Console.WriteLine($"---------------close connection-----------");
            client.CloseAsync();
        }
    }
Copy after login

0x03 下一步

下一篇 我将详细讲述DotBPE.RPC中的主要类和调用关系,并介绍如何使用DotNetty实现RPC通信。
事实上我正在编写一个更加复杂的示例https://github.com/xuanye/PiggyMetrics.git,
这原是spring cloud的一个示例程序,我使用DotBPE进行改造,用示例描述DotBPE在真实场景中的应用。包括服务注册和发现,服务间调用,公开HttpApi,监控检查等功能,并通过实践进一步完善DotBPE。初步的功能已经实现,不过还没来的及写文档。该系列的后面将详细描述该系统的实现。

The above is the detailed content of DotBPE.RPC quick start. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!