Boring GRPC -DAY1
1.DONET CORE 2.1
===============================================================================
引用Nuget包
Grpc 1.15.0
Grpc.Tools 1.15.0
Google.Protobuf 3.6.1
SETP 1
创建protos文件夹,和.proto原型文件,以及生成文件的所属文件夹
.proto片段代码截图
SETP 2
编写generate.bat可执行脚本,通过grpc.tool生成目标文件
Generate.bat片段代码截图
生成后,解决解决方案目录图
STEP 3
编写接口实现,本事例中我定义了两个服务statistics接口以及reg接口
注意实现类应该继承原型Base类,这里是 userImp:UserBase
STEP 4
启动RPC服务
控制台片段代码,这里我是IP绑定本地,设置通信端口为7777
===============================================================================
STEP 5客户端
演示没有把服务接口做成Nuget包,所以直接是引用。在实际项目中可以直接引用Nuget业务包,这样方便业务模块升级,强烈建议使用(需自搭建Nuget包管理服务器)
代码片段如图(注意引用Grpc及Google.Protobuf包)
STEP 6
运行服务端
运行客户端
至此,GRPC服务通信事例DEMO,演示结束?
===================================================================================
增加了关于枚举和嵌套Model的相关示例代码
===================================================================================
proto文件
syntax = "proto3";
service HeWei{
rpc test (HwRequest) returns (ContactBook){}
}
message HwRequest{
string Department = 1;
string city = 2;
}
enum PhoneType {
HOME = 0;
WORK = 1;
}
message Phone {
PhoneType type = 1;
string number = 2;
}
message Person {
int32 id = 1;
string name = 2;
repeated Phone phones = 3;
}
message ContactBook {
string Department = 1;
repeated Person persons = 2;
}
接口实现
#region << 版 本 注 释 >>
/*----------------------------------------------------------------
* 项目名称 :grpcService.imp
* 项目描述 :
* 类 名 称 :heweiImp
* 类 描 述 :
* 所在的域 :PANWEI
* 命名空间 :grpcService.imp
* 机器名称 :PANWEI
* CLR 版本 :4.0.30319.42000
* 作 者 :jetaime
* 创建时间 :4/18/2019 7:21:58 AM
* 更新时间 :4/18/2019 7:21:58 AM
* 版 本 号 :v1.0.0.0
*******************************************************************
* Copyright @ jetaime 2019. All rights reserved.
*******************************************************************
//----------------------------------------------------------------*/
#endregion
using System.Collections.Generic;
using System.Threading.Tasks;
using Grpc.Core;
namespace grpcService.imp
{
public class HeweiImp : HeWei.HeWeiBase
{
public override Task<ContactBook> test(HwRequest request, ServerCallContext context)
{
ContactBook contactBook = new ContactBook();
contactBook.Department = "support";
//add new contactbook for member named "hewei"
Person hewei = new Person
{
Id = 2019,
Name = "hewei"
};
hewei.Phones.AddRange(new List<Phone>
{
new Phone
{
Type = PhoneType.Work,
Number = "13900000002"
},
new Phone
{
Type = PhoneType.Home,
Number = "13900000001"
}
});
contactBook.Persons.Add(hewei);
//add new contactbook for member named "jiangwei"
Person jiangwei = new Person
{
Id = 2019,
Name = "jiangwei"
};
jiangwei.Phones.AddRange(new List<Phone>
{
new Phone
{
Type = PhoneType.Work,
Number = "13800000055"
},
new Phone
{
Type = PhoneType.Home,
Number = "13800000066"
}
});
contactBook.Persons.Add(jiangwei);
return Task.FromResult(contactBook);
}
}
}
客户端如何调用
var heweiClient = new HeWei.HeWeiClient(channel);
var heweiReplay = heweiClient.test(new HwRequest {City = "yangzhou", Department = "support"});
Console.WriteLine(heweiReplay.ToString());
源码百度网盘下载地址:
链接:https://pan.baidu.com/s/1E5CnLoMPl0eLeGmac0xQJA
提取码:dph9