如何将.Net Core微服务RPC框架GRPC通信应用于实际长尾场景?
- 内容介绍
- 文章标签
- 相关推荐
本文共计582个文字,预计阅读时间需要3分钟。
序+上一篇博客把gRPC的概念介绍了个大概,介绍了proto的数据类型、基本语法,也写了一个小demo,是不是没那么难?+今天要从理论到实践,写两个微服务,并利用gRPC完成两者之间的通信。
序
上一篇博客把grpc的概念说了个大概,介绍了proto的数据类型,基本语法,也写了个小demo,是不是没那么难?
今天要从理论到实际,写两个微服务,并利用grpc完成两者之间的通信。只是作为demo写的话会十分简单,毕竟理解为主。
服务端
首先要拿出之前写好的proto文件,然后修改两个属性:
Build Action => Protobuf compiler gRpc Stub Classes => Server only
如图:
当然也可以在项目文件里看到它:
然后重新生成项目 ,会自动根据proto文件生成server端的文件。
引用
经过刚才,已经生成了对应的服务,我们可以直接在代码里调用。
这是之前写好的proto:
syntax = "proto3"; option csharp_namespace = "gRPCApiDemo.Protos"; package Demo; service MyMath{ rpc MathAdd (AddRequest) returns (AddRespones) {} } message AddRequest{ int32 a=1; int32 b=2; } message AddRespones{ int32 a=1; }
生成以后,会有一个MyMath.MyMathBase这个类,我们来继承一下:
注意看命名空间,这是刚才项目生成以后根据proto生成的。
现在来重写一下里面的方法(下图是生成,也可以手动写):
根据proto文件可知:
AddRequest包含两个int参数:A、B
AddRespones包含一个名为A的int参数
那我们把AB相加然后返回:
using Grpc.Core; using gRPCApiDemo.Protos; using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; namespace gRPCApiDemo.Grpc { public class GrpcServiceMath : MyMath.MyMathBase { public override Task<AddRespones> MathAdd(AddRequest request, ServerCallContext context) { var respones = new AddRespones { A = request.A + request.B }; return Task.FromResult(respones); } } }
再然后进入StartUp设置一下:
app.UseHttpsRedirection(); app.UseEndpoints(endpoints => { endpoints.MapGrpcService<MathServices>(); });
服务端到这里就写完了。
如果写了更多service,那就需要在这里声明更多的实现类;而且localhost:5001"));
MyMath是proto里声明的服务,MyMathClient是刚才生成的,里面的Uri是服务端所在的域名。
因为gRpc是基于gitee.com/muchengqingxin/GrpcServerDemo.git
gitee.com/muchengqingxin/GrpcClientDemo.git
到此这篇关于.Net Core微服务rpc框架GRPC通信实际运用的文章就介绍到这了。希望对大家的学习有所帮助,也希望大家多多支持自由互联。
本文共计582个文字,预计阅读时间需要3分钟。
序+上一篇博客把gRPC的概念介绍了个大概,介绍了proto的数据类型、基本语法,也写了一个小demo,是不是没那么难?+今天要从理论到实践,写两个微服务,并利用gRPC完成两者之间的通信。
序
上一篇博客把grpc的概念说了个大概,介绍了proto的数据类型,基本语法,也写了个小demo,是不是没那么难?
今天要从理论到实际,写两个微服务,并利用grpc完成两者之间的通信。只是作为demo写的话会十分简单,毕竟理解为主。
服务端
首先要拿出之前写好的proto文件,然后修改两个属性:
Build Action => Protobuf compiler gRpc Stub Classes => Server only
如图:
当然也可以在项目文件里看到它:
然后重新生成项目 ,会自动根据proto文件生成server端的文件。
引用
经过刚才,已经生成了对应的服务,我们可以直接在代码里调用。
这是之前写好的proto:
syntax = "proto3"; option csharp_namespace = "gRPCApiDemo.Protos"; package Demo; service MyMath{ rpc MathAdd (AddRequest) returns (AddRespones) {} } message AddRequest{ int32 a=1; int32 b=2; } message AddRespones{ int32 a=1; }
生成以后,会有一个MyMath.MyMathBase这个类,我们来继承一下:
注意看命名空间,这是刚才项目生成以后根据proto生成的。
现在来重写一下里面的方法(下图是生成,也可以手动写):
根据proto文件可知:
AddRequest包含两个int参数:A、B
AddRespones包含一个名为A的int参数
那我们把AB相加然后返回:
using Grpc.Core; using gRPCApiDemo.Protos; using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; namespace gRPCApiDemo.Grpc { public class GrpcServiceMath : MyMath.MyMathBase { public override Task<AddRespones> MathAdd(AddRequest request, ServerCallContext context) { var respones = new AddRespones { A = request.A + request.B }; return Task.FromResult(respones); } } }
再然后进入StartUp设置一下:
app.UseHttpsRedirection(); app.UseEndpoints(endpoints => { endpoints.MapGrpcService<MathServices>(); });
服务端到这里就写完了。
如果写了更多service,那就需要在这里声明更多的实现类;而且localhost:5001"));
MyMath是proto里声明的服务,MyMathClient是刚才生成的,里面的Uri是服务端所在的域名。
因为gRpc是基于gitee.com/muchengqingxin/GrpcServerDemo.git
gitee.com/muchengqingxin/GrpcClientDemo.git
到此这篇关于.Net Core微服务rpc框架GRPC通信实际运用的文章就介绍到这了。希望对大家的学习有所帮助,也希望大家多多支持自由互联。

