翼度科技»论坛 编程开发 .net 查看内容

.NET Core(.NET6)中gRPC使用

9

主题

9

帖子

27

积分

新手上路

Rank: 1

积分
27
目录
 
回到顶部一、简介

简单解析一下gRPC,gRPC 是一个由Google开源的,跨语言的,高性能的远程过程调用(RPC)框架。
特点:

  • 跨语言
  • 内容protobuf格式(比json体积小),网络传输快
  • 使用HTTP/2进行传输
适合高性能轻量的微服务,一般对外的接口用restful api,内部服务的调用用gRPC。gRPC是一个分布式服务框架,和以前的WebService,WCF类似。
回到顶部二、创建gRPC服务端

1.创建gRPC项目

新建一个gRPC模板的项目

 
 

 
 

 
 特别的地方就这里4点。
1.基于http2来通信。
2.proto协议文件,greet.proto是项目默认给我们的一个hello world的示例。它会根据协议自动生成需要的类。

 
 
3.服务类,Greeter.GreeterBase来自于2中的proto文件自动生成的类,生成的类在\obj\Debug\net6.0\Protos 目录下。

 
 自动生成的类:

 
 4.Program.cs添加了gRPC服务,和把GreeterService注册到管道中。
2.编写自己的服务

怎么样写自己的调用服务呢?
 1.创建proto文件
参照上面的示例创建自己的Proto文件

 
 
 
 
 代码:
  1. syntax = "proto3";
  2. option csharp_namespace = "GrpcDemo.Service";
  3. package order;
  4. // 订单服务定义
  5. service Order {
  6.   // 创建订单
  7.   rpc CreateOrder (CreateRequest) returns (CreateResult);
  8.   //查询订单
  9.   rpc QueryOrder (QueryRequest) returns (QueryResult);
  10. }
  11. //创建订单请求参数
  12. message CreateRequest {
  13.   string orderNo = 1;
  14.   string orderName=2;
  15.   double price=3;
  16. }
  17. //创建订单返回结果
  18. message CreateResult {
  19.   bool result = 1;
  20.   string message=2;
  21. }
  22. //查询订单请求参数
  23. message QueryRequest{
  24.     string id=1;
  25. }
  26. //查询订单返回结果
  27. message QueryResult{
  28.     string id=1;
  29.     string orderNo=2;
  30.     string orderName=3;
  31.     double price=4;
  32. }
复制代码
 
生成一下就能看到对应的类已经生成了。

 
 2.实现定义的服务
 创建OrderService.cs
  1. public class OrderService:Order.OrderBase
  2.     {
  3.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  4.       <Protobuf Include="Protos\order.proto" GrpcServices="Client" />private readonly ILogger<GreeterService> _logger;
  5.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  6.       <Protobuf Include="Protos\order.proto" GrpcServices="Client" />public OrderService(ILogger<GreeterService> logger)
  7.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  8.       <Protobuf Include="Protos\order.proto" GrpcServices="Client" />{
  9.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  10.       <Protobuf Include="Protos\order.proto" GrpcServices="Client" />    _logger = logger;
  11.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  12.       <Protobuf Include="Protos\order.proto" GrpcServices="Client" />}
  13.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  14.       <Protobuf Include="Protos\order.proto" GrpcServices="Client" />/// <summary>
  15.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  16.       <Protobuf Include="Protos\order.proto" GrpcServices="Client" />/// 创建订单
  17.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  18.       <Protobuf Include="Protos\order.proto" GrpcServices="Client" />/// </summary>
  19.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  20.       <Protobuf Include="Protos\order.proto" GrpcServices="Client" />/// <param name="request"></param>
  21.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  22.       <Protobuf Include="Protos\order.proto" GrpcServices="Client" />/// <param name="context"></param>
  23.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  24.       <Protobuf Include="Protos\order.proto" GrpcServices="Client" />/// <returns></returns>
  25.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  26.       <Protobuf Include="Protos\order.proto" GrpcServices="Client" />public override Task<CreateResult> CreateOrder(CreateRequest request, ServerCallContext context)
  27.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  28.       <Protobuf Include="Protos\order.proto" GrpcServices="Client" />{
  29.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  30.       <Protobuf Include="Protos\order.proto" GrpcServices="Client" />    //报存数据库 todo
  31.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  32.       <Protobuf Include="Protos\order.proto" GrpcServices="Client" />    return Task.FromResult(new CreateResult
  33.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  34.       <Protobuf Include="Protos\order.proto" GrpcServices="Client" />    {
  35.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  36.       <Protobuf Include="Protos\order.proto" GrpcServices="Client" />  <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  37.       <Protobuf Include="Protos\order.proto" GrpcServices="Client" />Result=true,
  38.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  39.       <Protobuf Include="Protos\order.proto" GrpcServices="Client" />  <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  40.       <Protobuf Include="Protos\order.proto" GrpcServices="Client" />Message="订单创建成功"
  41.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  42.       <Protobuf Include="Protos\order.proto" GrpcServices="Client" />    });
  43.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  44.       <Protobuf Include="Protos\order.proto" GrpcServices="Client" />}
  45.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  46.       <Protobuf Include="Protos\order.proto" GrpcServices="Client" />/// <summary>
  47.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  48.       <Protobuf Include="Protos\order.proto" GrpcServices="Client" />/// 查询订单
  49.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  50.       <Protobuf Include="Protos\order.proto" GrpcServices="Client" />/// </summary>
  51.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  52.       <Protobuf Include="Protos\order.proto" GrpcServices="Client" />/// <param name="request"></param>
  53.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  54.       <Protobuf Include="Protos\order.proto" GrpcServices="Client" />/// <param name="context"></param>
  55.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  56.       <Protobuf Include="Protos\order.proto" GrpcServices="Client" />/// <returns></returns>
  57.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  58.       <Protobuf Include="Protos\order.proto" GrpcServices="Client" />public override Task<QueryResult> QueryOrder(QueryRequest request, ServerCallContext context)
  59.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  60.       <Protobuf Include="Protos\order.proto" GrpcServices="Client" />{
  61.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  62.       <Protobuf Include="Protos\order.proto" GrpcServices="Client" />    //查询数据库 //todo
  63.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  64.       <Protobuf Include="Protos\order.proto" GrpcServices="Client" />    return Task.FromResult(new QueryResult
  65.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  66.       <Protobuf Include="Protos\order.proto" GrpcServices="Client" />    {
  67.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  68.       <Protobuf Include="Protos\order.proto" GrpcServices="Client" />  <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  69.       <Protobuf Include="Protos\order.proto" GrpcServices="Client" />
  70.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  71.       <Protobuf Include="Protos\order.proto" GrpcServices="Client" />  <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  72.       <Protobuf Include="Protos\order.proto" GrpcServices="Client" />    Id = request.Id,
  73.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  74.       <Protobuf Include="Protos\order.proto" GrpcServices="Client" />  <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  75.       <Protobuf Include="Protos\order.proto" GrpcServices="Client" />    OrderNo = DateTime.Now.ToString("yyyyMMddHHmmss"),
  76.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  77.       <Protobuf Include="Protos\order.proto" GrpcServices="Client" />  <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  78.       <Protobuf Include="Protos\order.proto" GrpcServices="Client" />    OrderName = "冰箱",
  79.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  80.       <Protobuf Include="Protos\order.proto" GrpcServices="Client" />  <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  81.       <Protobuf Include="Protos\order.proto" GrpcServices="Client" />    Price = 1288
  82.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  83.       <Protobuf Include="Protos\order.proto" GrpcServices="Client" />  <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  84.       <Protobuf Include="Protos\order.proto" GrpcServices="Client" />
  85.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  86.       <Protobuf Include="Protos\order.proto" GrpcServices="Client" />    });
  87.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  88.       <Protobuf Include="Protos\order.proto" GrpcServices="Client" />}
  89.     }
复制代码
继承的Order.OrderBase 是上面的proto文件生成的,然后实现了proto里面定义的两个服务。
然后在program.cs里面把服务注入管道。

 
 到这里服务端就完成了,就可以启动服务端了。

 
 浏览器访问不了,要通过gRPC客户端才能访问,下面就建一个gRPC客户端。
回到顶部三、创建gRPC客户端

1.创建客户端项目

1.1、这里创建一个控制台程序。
1.2、然后添加Nuget包
  1. Google.Protobuf
  2. Grpc.Net.Client
  3. Grpc.Tools<br>Grpc.Net.clientFactory
复制代码
1.3、把服务端的proto文件拷贝过来

 
 1.4、编辑项目文件,把proto里面的内容替换掉,默认是服务端的配置

 
 
  1.   <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  2.       <Protobuf Include="Protos\order.proto" GrpcServices="Client" />
复制代码
生成的时候,客户端也生成了对应proto的类

 
2.grPC服务https的调用

因为服务端提供了http和https的端口,这里先调用https的
创建GrpcRequestTest.cs类
  1. ///     /// gRPC请求测试    ///     public class GrpcRequestTest    {  <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  2.       <Protobuf Include="Protos\order.proto" GrpcServices="Client" />public void CreateOrder()  <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  3.       <Protobuf Include="Protos\order.proto" GrpcServices="Client" />{  <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  4.       <Protobuf Include="Protos\order.proto" GrpcServices="Client" />    //常规使用,https  <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  5.       <Protobuf Include="Protos\order.proto" GrpcServices="Client" />    string url = "https://localhost:7246";  <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  6.       <Protobuf Include="Protos\order.proto" GrpcServices="Client" />    using(var channel=GrpcChannel.ForAddress(url))  <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  7.       <Protobuf Include="Protos\order.proto" GrpcServices="Client" />    {  <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  8.       <Protobuf Include="Protos\order.proto" GrpcServices="Client" />  <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  9.       <Protobuf Include="Protos\order.proto" GrpcServices="Client" />var client = new Order.OrderClient(channel);  <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  10.       <Protobuf Include="Protos\order.proto" GrpcServices="Client" />  <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  11.       <Protobuf Include="Protos\order.proto" GrpcServices="Client" />var reply = client.CreateOrder(new CreateRequest()  <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  12.       <Protobuf Include="Protos\order.proto" GrpcServices="Client" />  <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  13.       <Protobuf Include="Protos\order.proto" GrpcServices="Client" />{  <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  14.       <Protobuf Include="Protos\order.proto" GrpcServices="Client" />  <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  15.       <Protobuf Include="Protos\order.proto" GrpcServices="Client" />    OrderNo = DateTime.Now.ToString("yyyMMddHHmmss"),  <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  16.       <Protobuf Include="Protos\order.proto" GrpcServices="Client" />  <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  17.       <Protobuf Include="Protos\order.proto" GrpcServices="Client" />    OrderName = "冰箱22款",  <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  18.       <Protobuf Include="Protos\order.proto" GrpcServices="Client" />  <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  19.       <Protobuf Include="Protos\order.proto" GrpcServices="Client" />    Price = 1688  <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  20.       <Protobuf Include="Protos\order.proto" GrpcServices="Client" />  <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  21.       <Protobuf Include="Protos\order.proto" GrpcServices="Client" />});  <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  22.       <Protobuf Include="Protos\order.proto" GrpcServices="Client" />  <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  23.       <Protobuf Include="Protos\order.proto" GrpcServices="Client" />Console.WriteLine($"结果:{reply.Result},message:{reply.Message}");  <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  24.       <Protobuf Include="Protos\order.proto" GrpcServices="Client" />    }  <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  25.       <Protobuf Include="Protos\order.proto" GrpcServices="Client" />    Console.ReadKey();  <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  26.       <Protobuf Include="Protos\order.proto" GrpcServices="Client" />}    }
复制代码
结果:

3.gRPC内网http调用

  1.       public void CreateOrder()  <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  2.       <Protobuf Include="Protos\order.proto" GrpcServices="Client" />{  <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  3.       <Protobuf Include="Protos\order.proto" GrpcServices="Client" />     //使用http  <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  4.       <Protobuf Include="Protos\order.proto" GrpcServices="Client" />    AppContext.SetSwitch("System.Net.Http.SocketsHttpHandler.Http2UnencryptedSupport", true);  <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  5.       <Protobuf Include="Protos\order.proto" GrpcServices="Client" />    string url = "http://localhost:5246";  <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  6.       <Protobuf Include="Protos\order.proto" GrpcServices="Client" />    using(var channel=GrpcChannel.ForAddress(url))  <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  7.       <Protobuf Include="Protos\order.proto" GrpcServices="Client" />    {  <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  8.       <Protobuf Include="Protos\order.proto" GrpcServices="Client" />  <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  9.       <Protobuf Include="Protos\order.proto" GrpcServices="Client" />var client = new Order.OrderClient(channel);  <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  10.       <Protobuf Include="Protos\order.proto" GrpcServices="Client" />  <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  11.       <Protobuf Include="Protos\order.proto" GrpcServices="Client" />var reply = client.CreateOrder(new CreateRequest()  <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  12.       <Protobuf Include="Protos\order.proto" GrpcServices="Client" />  <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  13.       <Protobuf Include="Protos\order.proto" GrpcServices="Client" />{  <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  14.       <Protobuf Include="Protos\order.proto" GrpcServices="Client" />  <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  15.       <Protobuf Include="Protos\order.proto" GrpcServices="Client" />    OrderNo = DateTime.Now.ToString("yyyMMddHHmmss"),  <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  16.       <Protobuf Include="Protos\order.proto" GrpcServices="Client" />  <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  17.       <Protobuf Include="Protos\order.proto" GrpcServices="Client" />    OrderName = "冰箱22款",  <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  18.       <Protobuf Include="Protos\order.proto" GrpcServices="Client" />  <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  19.       <Protobuf Include="Protos\order.proto" GrpcServices="Client" />    Price = 1688  <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  20.       <Protobuf Include="Protos\order.proto" GrpcServices="Client" />  <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  21.       <Protobuf Include="Protos\order.proto" GrpcServices="Client" />});  <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  22.       <Protobuf Include="Protos\order.proto" GrpcServices="Client" />  <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  23.       <Protobuf Include="Protos\order.proto" GrpcServices="Client" />Console.WriteLine($"结果:{reply.Result},message:{reply.Message}");  <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  24.       <Protobuf Include="Protos\order.proto" GrpcServices="Client" />    }  <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  25.       <Protobuf Include="Protos\order.proto" GrpcServices="Client" />    Console.ReadKey();  <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  26.       <Protobuf Include="Protos\order.proto" GrpcServices="Client" />}
复制代码
比起https,前面多了一行。结果:

 
4.IOC注入的方式调用gRPC

4.1、program.cs里面注入gRPCClient
  1. // See https://aka.ms/new-console-template for more information
  2. using GrpcDemo.Client;
  3. using GrpcDemo.Service;
  4. using Microsoft.Extensions.DependencyInjection;
  5. Console.WriteLine("Hello, World!");
  6. IServiceCollection services = new ServiceCollection();
  7. services.AddTransient<GrpcRequestTest>();
  8. #region gRPC Client注册
  9. AppContext.SetSwitch("System.Net.Http.SocketsHttpHandler.Http2UnencryptedSupport", true);
  10. services.AddGrpcClient<Order.OrderClient>(options =>
  11. {
  12.     options.Address = new Uri("http://localhost:5246");
  13. }).ConfigureChannel(grpcOptions =>
  14. {
  15.     //可以完成各种配置,比如token
  16. });
  17. #endregion
  18. //构建容器
  19. IServiceProvider serviceProvider = services.BuildServiceProvider();
  20. //解析grpc请求测试
  21. var grpcRequestTest = serviceProvider.GetService<GrpcRequestTest>();
  22. //执行
  23. grpcRequestTest.CreateOrder();
复制代码
grpcRequestTest里代码:
  1. ///     /// gRPC请求测试    ///     public class GrpcRequestTest    {  <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  2.       <Protobuf Include="Protos\order.proto" GrpcServices="Client" />private Order.OrderClient _orderClient;  <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  3.       <Protobuf Include="Protos\order.proto" GrpcServices="Client" />public GrpcRequestTest(Order.OrderClient orderClient)  <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  4.       <Protobuf Include="Protos\order.proto" GrpcServices="Client" />{  <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  5.       <Protobuf Include="Protos\order.proto" GrpcServices="Client" />    _orderClient = orderClient;  <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  6.       <Protobuf Include="Protos\order.proto" GrpcServices="Client" />}  <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  7.       <Protobuf Include="Protos\order.proto" GrpcServices="Client" />public void CreateOrder()  <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  8.       <Protobuf Include="Protos\order.proto" GrpcServices="Client" />{  <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  9.       <Protobuf Include="Protos\order.proto" GrpcServices="Client" />    var reply = _orderClient.CreateOrder(new CreateRequest()  <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  10.       <Protobuf Include="Protos\order.proto" GrpcServices="Client" />    {  <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  11.       <Protobuf Include="Protos\order.proto" GrpcServices="Client" />  <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  12.       <Protobuf Include="Protos\order.proto" GrpcServices="Client" />OrderNo = DateTime.Now.ToString("yyyMMddHHmmss"),  <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  13.       <Protobuf Include="Protos\order.proto" GrpcServices="Client" />  <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  14.       <Protobuf Include="Protos\order.proto" GrpcServices="Client" />OrderName = "冰箱22款",  <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  15.       <Protobuf Include="Protos\order.proto" GrpcServices="Client" />  <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  16.       <Protobuf Include="Protos\order.proto" GrpcServices="Client" />Price = 1688  <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  17.       <Protobuf Include="Protos\order.proto" GrpcServices="Client" />    });  <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  18.       <Protobuf Include="Protos\order.proto" GrpcServices="Client" />    Console.WriteLine($"结果:{reply.Result},message:{reply.Message}");  <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  19.       <Protobuf Include="Protos\order.proto" GrpcServices="Client" />    Console.ReadKey();  <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  20.       <Protobuf Include="Protos\order.proto" GrpcServices="Client" />}    }
复制代码
结果:

 
回到顶部四、webapi中加入gRPC

通常我们的服务有对外提供对外接口,又要对内提供gRPC服务,那怎么做呢,下面在webapi中加入gRPC服务
1.创建asp.net core mvc项目
2.安装nuget包
  1. Grpc.AspNetCore
复制代码
3.添加protebuf文件
把上面的proto文件复制过来

 
 
4.添加Service

 
 
  1.   public class OrderService : Order.OrderBase    {  <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  2.       <Protobuf Include="Protos\order.proto" GrpcServices="Client" />private readonly ILogger _logger;  <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  3.       <Protobuf Include="Protos\order.proto" GrpcServices="Client" />public OrderService(ILogger logger)  <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  4.       <Protobuf Include="Protos\order.proto" GrpcServices="Client" />{  <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  5.       <Protobuf Include="Protos\order.proto" GrpcServices="Client" />    _logger = logger;  <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  6.       <Protobuf Include="Protos\order.proto" GrpcServices="Client" />}  <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  7.       <Protobuf Include="Protos\order.proto" GrpcServices="Client" />///  <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  8.       <Protobuf Include="Protos\order.proto" GrpcServices="Client" /> /// 创建订单  <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  9.       <Protobuf Include="Protos\order.proto" GrpcServices="Client" />///  <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  10.       <Protobuf Include="Protos\order.proto" GrpcServices="Client" /> ///  <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  11.       <Protobuf Include="Protos\order.proto" GrpcServices="Client" /> ///  <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  12.       <Protobuf Include="Protos\order.proto" GrpcServices="Client" /> ///  <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  13.       <Protobuf Include="Protos\order.proto" GrpcServices="Client" /> public override Task CreateOrder(CreateRequest request, ServerCallContext context)  <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  14.       <Protobuf Include="Protos\order.proto" GrpcServices="Client" />{  <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  15.       <Protobuf Include="Protos\order.proto" GrpcServices="Client" />    //报存数据库 todo  <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  16.       <Protobuf Include="Protos\order.proto" GrpcServices="Client" />    return Task.FromResult(new CreateResult  <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  17.       <Protobuf Include="Protos\order.proto" GrpcServices="Client" />    {  <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  18.       <Protobuf Include="Protos\order.proto" GrpcServices="Client" />  <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  19.       <Protobuf Include="Protos\order.proto" GrpcServices="Client" />Result = true,  <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  20.       <Protobuf Include="Protos\order.proto" GrpcServices="Client" />  <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  21.       <Protobuf Include="Protos\order.proto" GrpcServices="Client" />Message = "订单创建成功"  <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  22.       <Protobuf Include="Protos\order.proto" GrpcServices="Client" />    });  <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  23.       <Protobuf Include="Protos\order.proto" GrpcServices="Client" />}  <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  24.       <Protobuf Include="Protos\order.proto" GrpcServices="Client" />///  <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  25.       <Protobuf Include="Protos\order.proto" GrpcServices="Client" /> /// 查询订单  <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  26.       <Protobuf Include="Protos\order.proto" GrpcServices="Client" />///  <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  27.       <Protobuf Include="Protos\order.proto" GrpcServices="Client" /> ///  <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  28.       <Protobuf Include="Protos\order.proto" GrpcServices="Client" /> ///  <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  29.       <Protobuf Include="Protos\order.proto" GrpcServices="Client" /> ///  <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  30.       <Protobuf Include="Protos\order.proto" GrpcServices="Client" /> public override Task QueryOrder(QueryRequest request, ServerCallContext context)  <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  31.       <Protobuf Include="Protos\order.proto" GrpcServices="Client" />{  <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  32.       <Protobuf Include="Protos\order.proto" GrpcServices="Client" />    //查询数据库 //todo  <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  33.       <Protobuf Include="Protos\order.proto" GrpcServices="Client" />    return Task.FromResult(new QueryResult  <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  34.       <Protobuf Include="Protos\order.proto" GrpcServices="Client" />    {  <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  35.       <Protobuf Include="Protos\order.proto" GrpcServices="Client" />  <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  36.       <Protobuf Include="Protos\order.proto" GrpcServices="Client" />OrderInfo = new OrderInfo  <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  37.       <Protobuf Include="Protos\order.proto" GrpcServices="Client" />  <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  38.       <Protobuf Include="Protos\order.proto" GrpcServices="Client" />{  <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  39.       <Protobuf Include="Protos\order.proto" GrpcServices="Client" />  <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  40.       <Protobuf Include="Protos\order.proto" GrpcServices="Client" />    Id = request.Id,  <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  41.       <Protobuf Include="Protos\order.proto" GrpcServices="Client" />  <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  42.       <Protobuf Include="Protos\order.proto" GrpcServices="Client" />    OrderNo = DateTime.Now.ToString("yyyyMMddHHmmss"),  <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  43.       <Protobuf Include="Protos\order.proto" GrpcServices="Client" />  <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  44.       <Protobuf Include="Protos\order.proto" GrpcServices="Client" />    OrderName = "冰箱",  <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  45.       <Protobuf Include="Protos\order.proto" GrpcServices="Client" />  <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  46.       <Protobuf Include="Protos\order.proto" GrpcServices="Client" />    Price = 1288  <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  47.       <Protobuf Include="Protos\order.proto" GrpcServices="Client" />  <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  48.       <Protobuf Include="Protos\order.proto" GrpcServices="Client" />}  <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  49.       <Protobuf Include="Protos\order.proto" GrpcServices="Client" />    });  <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
  50.       <Protobuf Include="Protos\order.proto" GrpcServices="Client" />}    }
复制代码
5.注册gRPC服务
在Program.cs文件中

 
到这里,就把gRPC加入到webapi里面了。 
6.验证
启动asp.net core mvc程序
 

 
 
浏览器能访问,证明 restful api是没问题的。
然后再用上面的客户端访问一下gRPC的服务。

 
 
 结果:

 
 这样就成功对外提供api接口和对内提供gRPC服务了。
 
源码地址:https://github.com/weixiaolong325/GrpcDemo.Service
 

来源:https://www.cnblogs.com/lijiasnong/p/18047005
免责声明:由于采集信息均来自互联网,如果侵犯了您的权益,请联系我们【E-Mail:cb@itdo.tech】 我们会及时删除侵权内容,谢谢合作!

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

x

上一篇: 委托

下一篇: .NET Core(.NET6)中gRPC使用

举报 回复 使用道具