文档(中文版):http://doc.oschina.net/grpc
github 地址: https://github.com/grpc/grpc
- 易于创建分布式应用和服务(支持多语言C、C++、Node.js、Python、Ruby、Objective-C、PHP和C#等语言,跨平台)
- 高性能、通用的开源RPC框架,其由Google主要面向移动应用开发并基于HTTP/2协议标准而设计,基于ProtoBuf(Protocol Buffers)序列化协议开发,且支持众多开发语言。gRPC提供了一种简单的方法来精确地定义服务和为iOS、Android和后台支持服务自动生成可靠性很强的客户端功能库。客户端充分利用高级流和链接功能,从而有助于节省带宽、降低的TCP链接次数、节省CPU使用;
C#调用过程(3步骤)
第一步:定义服务(在一个 .proto 文件内定义服务)
例子使用的是 protocol buffers 语言的 proto3 版本,它目前只是 alpha 版:可以在 proto3 语言指南和 protocol buffers 的 Github 仓库的版本注释发现更多关于新版本的内容。
github地址 https://github.com/protocolbuffers/protobuf
文档 :https://segmentfault.com/a/1190000020286021?utm_source=tag-newest
https://www.cnblogs.com/silvermagic/p/9087593.html
// 例子使用的是 protocol buffers 语言的 proto3 版本,它目前只是 alpha 版:可以在 proto3 语言指南和 protocol buffers 的 Github 仓库的版本注释发现更多关于新版本的内容。 // github地址 https://github.com/protocolbuffers/protobuf // 文档 :https://segmentfault.com/a/1190000020286021?utm_source=tag-newest syntax = "proto3"; option java_multiple_files = true; option java_package = "io.grpc.examples.routeguide"; option java_outer_classname = "RouteGuideProto"; option objc_class_prefix = "RTG"; package routeguide; // 服务接口 service RouteGuide { // 一个 简单 RPC , 客户端使用存根发送请求到服务器并等待响应返回,就像平常的函数调用一样。 rpc GetFeature(Point) returns (Feature) {} // 一个 服务器端流式 RPC , 客户端发送请求到服务器,拿到一个流去读取返回的消息序列。 // 客户端读取返回的流,直到里面没有任何消息。从例子中可以看出,通过在 响应 类型前插入 stream 关键字,可以指定一个服务器端的流方法。 rpc ListFeatures(Rectangle) returns (stream Feature) {} // 一个 客户端流式 RPC , 客户端写入一个消息序列并将其发送到服务器,同样也是使用流。一旦客户端完成写入消息,它等待服务器完成读取返回它的响应。 // 通过在 请求 类型前指定 stream 关键字来指定一个客户端的流方法。 rpc RecordRoute(stream Point) returns (RouteSummary) {} // 一个 双向流式 RPC 是双方使用读写流去发送一个消息序列。两个流独立操作,因此客户端和服务器可以以任意喜欢的顺序读写: // 比如, 服务器可以在写入响应前等待接收所有的客户端消息,或者可以交替的读取和写入消息,或者其他读写的组合。 // 每个流中的消息顺序被预留。你可以通过在请求和响应前加 stream 关键字去制定方法的类型。 rpc RouteChat(stream RouteNote) returns (stream RouteNote) {} } // 我们的 .proto 文件也包含了所有请求的 protocol buffer 消息类型定义以及在服务方法中使用的响应类型 // 比如,下面的Point消息类型,Rectangle 消息类型 等等 message Point { int32 latitude = 1; int32 longitude = 2; } message Rectangle { Point lo = 1; Point hi = 2; } message Feature { string name = 1; Point location = 2; } message RouteNote { Point location = 1; string message = 2; } message RouteSummary { int32 point_count = 1; int32 feature_count = 2; int32 distance = 3; int32 elapsed_time = 4; }
第二步:用 protocol buffer 编译器生成服务器和客户端代码 (使用Grpc.Tools生成代码)
给项目中添加上Grpc 及 Google.Protobuf 引用
NuGet 命令行:Install-Package Grpc
NuGet 命令行:Install-Package Google.Protobuf
给项目中再添加上工具 Grpc.Tools
NuGet 命令行:Install-Package Grpc.Tools
然后在命令行执行以下命令,注意执行命令的目录为packages 的上层目录
cd 到 Grpc.Tools目录下对应系统文件夹 如:..\packages\Grpc.Tools.1.0.0\tools\windows_x64 ;
执行命令 protoc.exe -IRouteGuide --csharp_out RouteGuide RouteGuide\helloworld.proto --grpc_out RouteGuide --plugin=protoc-gen-grpc=grpc_csharp_plugin.exe
执行完以后,在项目目录下会生成出RouteGuide.cs 及 RouteGuideGrpc.cs 类,将其包含至项目;然后grpc服务端项目和grpc客户端项目 分别都引用 该项目。
第三步:使用 gRPC 的 C# API 为你的服务实现一个简单的客户端和服务器
创建grpc服务端项目,并实现对应接口类 public class RouteGuideImpl : RouteGuide.IRouteGuide;
开启服务
Server server = new Server { Services = { RouteGuide.BindService(new RouteGuideImpl()) }, Ports = { new ServerPort("localhost", Port, ServerCredentials.Insecure) } }; server.Start(); Console.WriteLine("RouteGuide server listening on port " + port); Console.WriteLine("Press any key to stop the server..."); Console.ReadKey(); server.ShutdownAsync().Wait();
创建grpc客户端项目,并创建存根 RouteGuide.NewClient(channel) ;
调用服务
Channel channel = new Channel("127.0.0.1:50052", ChannelCredentials.Insecure) var client = RouteGuide.NewClient(channel); //调用 Point request = new Point { Latitude = 409146138, Longitude = -746188906 }; Feature feature = client.GetFeature(request); channel.ShutdownAsync().Wait();