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 ProtobufAmp(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>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, Notfollowed by the actual dataGoogle ProtobufGoogle 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,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
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. 0x02Quick Start
0. PrerequisiteBecause DotBPE is developed based on dotnet core, you must already have dotnet core locally The development environment1. Download the sample programIn 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.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
>$ # Clone the repository to get the example code: >$ git clone https://github.com/xuanye/dotbpe-sample.git >$ cd dotbpe-sampleUse VS2017 or VSCode to open the downloaded code.
The directory structure is as follows:
dotnet restore to download the dependencies. After success, use
dotnet build to compile and see the result: it looks perfect
>$ cd HelloDotBPE.Server
>$ dotnet run
Run Client>$ cd HelloDotBPE.Client
>$ dotnet run
Congratulations! Have used DotBPE.RPC to run a Server/Client application. 3. Let’s take a look at the code3.1 Service description file protoThe 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 本示例中无用 }
下面的服务描述文件 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's name. message HelloRequest { string name = 1; } // The response message containing the greetings message HelloResponse { string message = 1; }
通过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
当然我还是建议大家安装以下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 }); } }
服务端启动类
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(); } }
启动服务端
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(); } }</startup>
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(); } }
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!

In Unity, 3D physics engines and AI behavior trees can be implemented through C#. 1. Use the Rigidbody component and AddForce method to create a scrolling ball. 2. Through behavior tree nodes such as Patrol and ChasePlayer, AI characters can be designed to patrol and chase players.

u is used in C language to declare unsigned integer constants. 1. The u suffix represents an unsigned integer, such as 10u. 2. The range of unsigned integers starts from 0 and does not contain negative numbers. They are suitable for large-range positive numbers and bit operations. 3. Pay attention to overflow and negative number processing issues when using unsigned integers.

In C language, /0 refers to an empty character, which is used to mark the end of a string. 1) The value of the null character in the ASCII code table is 0. 2) It is the basis for C string processing, and the compiler will automatically add null characters at the end of the string. 3) The empty character is not visible but exists in memory, telling the string function to end the string. 4) When using it, make sure that the string ends with empty characters to avoid undefined behavior.

In C language, the bool type is introduced through header files to represent true and false values. 1. The value of type bool can be true (1) or false (0), and any non-zero value is considered true. 2. Using bool types can improve the readability of the code, especially when dealing with complex logical conditions. 3. Although bool types are convenient, in some cases, using integer types for boolean operations may be more efficient.

In C language, you can use the Taylor series method and the exp function in the standard library to calculate the x power of e. 1. The Taylor series method is calculated through approximately, which is suitable for situations where the accuracy requirements are not high, but may overflow when large numbers are large. 2. The exp function method uses the math.h header file, with high accuracy and good optimization, but requires linking to the math library. The selection method needs to be based on specific needs.

In C language, avg usually means "average", which is a common variable name for calculating the average value of a set of numbers. 1. Declare variables: use avg to store the average value. 2. Accumulation and calculation: traverse the dataset and accumulate all values, and then divide by the dataset length. 3. Result storage: Save the average value into the avg variable. Use double or float types to improve calculation accuracy.

"aa" has no special meaning in C language, it is just a normal identifier. 1. Variable name rules: Only include letters, numbers and underscores, starting with letters or underscores, not keywords, and are case-sensitive. 2. Best practice: Use meaningful names, avoid being too long, use camels or underscore nomenclature to avoid confusing names.

In C language, f represents floating point numbers, and the specific usage includes: 1. As a format specifier, used for printf and scanf functions; 2. Appear in mathematical function names, such as sinf and cosf; 3. As a floating point suffix, specify the type float; 4. Pay attention to accuracy issues in floating point operations and use tolerance for comparison; 5. Use float to optimize performance, but trade-offs are required.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

SublimeText3 Linux new version
SublimeText3 Linux latest version

SublimeText3 English version
Recommended: Win version, supports code prompts!

Notepad++7.3.1
Easy-to-use and free code editor

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.
