.NET使用gRPC

gRPC 简单介绍:

grpc 是一个高性能、开源和通用的 RPC 框架,面向移动和 HTTP/2 设计。目前提供 C、Java 和 Go 语言版本,分别是:grpc, grpc-java, grpc-go. 其中 C 版本支持 C, C++, Node.js, Python, Ruby, Objective-C, PHP 和 C# 支持.

gRPC 基于 HTTP/2 标准设计,带来诸如双向流、流控、头部压缩、单 TCP 连接上的多复用请求等特。这些特性使得其在移动设备上表现更好,更省电和节省空间占用。

参考文档:

http://doc.oschina.net/grpc?t=57966

 

新建工程GrpcClient、GrpcServer和GrpcLibrary

   添加 - 新建项目 - 控制台应用 GrpcClient、GrpcServer。

   添加 - 新建项目 - 类库 GrpcLibrary。 工程中的三个项目情况如下: 

 

 使用程序包管理器控制台

 

安装程序包Grpc

   三个项目GrpcClient、GrpcServer、GrpcLibrary均安装程序包Grpc。

Install-Package Grpc

  

 

安装程序包Google.Protobuf 

三个项目GrpcClient、GrpcServer、GrpcLibrary均安装程序包Google.Protobuf 。

 

Install-Package Google.Protobuf

 同上操作

安装程序包Grpc.Tools

 类库GrpcLibrary安装程序包Grpc.Tools。

Install-Package Grpc.Tools

  

 

自定义服务

  在项目GrpcLibrary里添加HelloWorld.proto用以生成代码。

syntax = "proto3";
package GrpcLibrary;
service GrpcService {
  rpc SayHello (HelloRequest) returns (HelloReply) {}
}
 
message HelloRequest {
  string name = 1;
}
 
message HelloReply {
  string message = 1;
}

 接着看看在定义参数的一些注意事项:
参数类型&参数值支持的数据类型对照表

 
.proto C#
double   double
float float
int32 int
int64 long
uint32 int
uint64 long
sint32 int
sint64 long
fixed32 int
fixed64 long
sfixed32 int
sfixed64 long
bool boolean
string string
bytes bytestring
 
 

 

然后在命令行执行以下命令,注意执行命令的目录为packages 的上层目录

 

注意:生成协议代码需 protoc.exe、grpc_csharp_plugin.exe工具.
在.net framework 项目下引用安装 Grpc.Tools 会得到protoc.exe、grpc_csharp_plugin.exe,但.net core 项目引用安装是不会下载工具到项目目录的,所以我们需要建一个.net framework项目,
我建了个.net framework类库执行Install-Package Grpc.Tools用于引用安装得到工具。

 

获取grpc C# 的.cs文件

 

从packages\Grpc.Tools.1.19.0\tools\windows_x64文件夹下复制grpc_csharp_plugin.exe,protoc.exe复制到GrpcLibrary文件夹下

 

 

在GrpcLibrary下创建1.cmd文件,输入一下内容并保存

protoc -I . --csharp_out . --grpc_out . --plugin=protoc-gen-grpc=grpc_csharp_plugin.exe HelloWorld.proto

  

双击执行

命令执行成功后,GrpcLibrary目录下会生成HelloWorld.cs和HelloWorldGrpc.cs。(记得包含到项目中)

 

 

最后GrpcClient、GrpcServer分别引用类库GrpcLibrary。

服务端

    class GrpcImpl : GrpcService.GrpcServiceBase
    {
        // 实现SayHello方法
        public override Task<HelloReply> SayHello(HelloRequest request, ServerCallContext context)
        {
            return Task.FromResult(new HelloReply { Message = "Hello " + request.Name });
        }
    }
    class Program
    {
        const int Port = 10007;
 
        public static void Main(string[] args)
        {
            Server server = new Server
            {
                Services = { GrpcService.BindService(new GrpcImpl()) },
                Ports = { new ServerPort("localhost", Port, ServerCredentials.Insecure) }
            };
            server.Start();
 
            Console.WriteLine("GrpcService server listening on port " + Port);
            Console.WriteLine("任意键退出...");
            Console.ReadKey();
 
            server.ShutdownAsync().Wait();
        }
    }

  

客户端

 class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Hello World!");
            Channel channel = new Channel("127.0.0.1:10007", ChannelCredentials.Insecure);

            var client = new GrpcService.GrpcServiceClient(channel);
        
            var input = Console.ReadLine();
            var reply = client.SayHello(new HelloRequest { Name = input });
            Console.WriteLine("来自" + reply.Message);
            
            channel.ShutdownAsync().Wait();
            Console.WriteLine("任意键退出...");
            Console.ReadKey();
        }
    }

  

测试

先启动服务的,再启动客户端。

 

 

 

posted @ 2019-02-28 16:43  贾咩咩  Views(4299)  Comments(2Edit  收藏  举报