小白学习 gRPC官方快速上手学习笔记(c#版)

上手前准备工作

支持操作系统:windows、OS X、Linux。实例采用.net、.net core sdk。

  • The .NET Core SDK command line tools.
  • The .NET framework 4.5 (for OS X and Linux, the open source .NET Framework implementation, “Mono”, at version 4+, is suitable)
  • Git (to download the sample code)

在windows系统开发环境, 采用 Visual Studio开发工具, 需要满足以下要求:

  • .NET Framework 4.5+
  • Visual Studio 2013 or 2015.
  • Git (to download the sample code)

    在OS X 系统开发环境, 采用Xamarin Studio开发工具, 需要满足以下要求:

  • Mono 4.4.2+ (or Mono 4+ is sufficient if you manually update NuGet to version 2.12+)
  • Xamarin Studio 6.0+
  • Git (to download the sample code)

    在 Linux 系统开发环境, 采用 the Monodevelop IDE,需要满足以下要求 :

  • Mono 4.4.2+ (or Mono 4+ is sufficient if you manually update nuget to version 2.12+)
  • MonoDevelop 5.9+
  • A NuGet executable, at version 2.12+ (you’ll need to restore NuGet package dependencies from the command line)
  • Git (to download the sample code)

下载官方demo

git clone -b v1.6.x https://github.com/grpc/grpc
  1. 打开下载的demo文C:\Users\YPF\Desktop\grpc
  2. 进入目录examples/csharp/helloworld

Build the example

  1. 使用Visual Studio打开Greeter.sln
  2. 在该项目的解决右键重新生成解决方案
    439779-20170928143833653-66585942.png

项目会自动使用NuGet进行必要的package的安装。

运行 a gRPC application

  1. 运行服务
> cd GreeterServer/bin/Debug
> GreeterServer.exe

439779-20170928144014122-1497764621.png

  1. 运行客户端
> cd GreeterClient/bin/Debug
> GreeterClient.exe

439779-20170928144026184-443054984.png

更新 a gRPC service

打开目录examples/protos/helloworld.proto
将原来的文件修改为如下并保存:

// The greeting service definition.
service Greeter {
  // Sends a greeting
  rpc SayHello (HelloRequest) returns (HelloReply) {}
  // Sends another greeting
  rpc SayHelloAgain (HelloRequest) returns (HelloReply) {}
}

// The request message containing the user's name.
message HelloRequest {
  string name = 1;
}

// The response message containing the greetings
message HelloReply {
  string message = 1;
}

生成 gRPC code

在demo的根目录(examples/csharp/helloworld)下执行如下命令:

packages\Grpc.Tools.1.6.1\tools\windows_x86\protoc.exe -I../../protos --csharp_out Greeter --grpc_out Greeter ../../protos/helloworld.proto --plugin=protoc-gen-grpc=packages/Grpc.Tools.1.6.1/tools/windows_x86/grpc_csharp_plugin.exe

439779-20170928144048637-1425923015.png

这里的Grpc.Tools.1.6.1这个命令必须是跟项目中使用NuGet安装的版本一致,否则会报错。

更新并从新运行

修改服务端代码

GreeterServer/Program.cs

class GreeterImpl : Greeter.GreeterBase
{
    // Server side handler of the SayHello RPC
    public override Task<HelloReply> SayHello(HelloRequest request, ServerCallContext context)
    {
        return Task.FromResult(new HelloReply { Message = "Hello " + request.Name });
    }

    // Server side handler for the SayHelloAgain RPC
    public override Task<HelloReply> SayHelloAgain(HelloRequest request, ServerCallContext context)
    {
        return Task.FromResult(new HelloReply { Message = "Hello again " + request.Name });
    }
}

修改服务端代码

GreeterClient/Program.cs

public static void Main(string[] args)
{
    Channel channel = new Channel("127.0.0.1:50051", ChannelCredentials.Insecure);

    var client = new Greeter.GreeterClient(channel);
    String user = "you";

    var reply = client.SayHello(new HelloRequest { Name = user });
    Console.WriteLine("Greeting: " + reply.Message);
    
    var secondReply = client.SayHelloAgain(new HelloRequest { Name = user });
    Console.WriteLine("Greeting: " + secondReply.Message);

    channel.ShutdownAsync().Wait();
    Console.WriteLine("Press any key to exit...");
    Console.ReadKey();
}

运行 a gRPC application

  • 运行服务

    cd GreeterServer/bin/Debug
    GreeterServer.exe

  • 运行客户端

    cd GreeterClient/bin/Debug
    GreeterClient.exe

参考文章:

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值