快速入门

本指南通过一个简单的工作示例,帮助您开始在 Ruby 中使用 gRPC。

快速入门

本指南通过一个简单的工作示例,帮助您开始在 Ruby 中使用 gRPC。

先决条件

  • Ruby 2 或更高版本

gRPC

要安装 gRPC,请运行以下命令

gem install grpc

gRPC 工具

Ruby 的 gRPC 工具包括协议缓冲区编译器 protoc 和用于从 .proto 服务定义生成服务器和客户端代码的特殊插件。在快速入门示例的第一部分,我们已经从 helloworld.proto 生成了服务器和客户端存根,但您需要这些工具来完成快速入门的其余部分,以及后续教程和您自己的项目。

要安装 gRPC 工具,请运行以下命令

gem install grpc-tools

下载示例

您需要一份示例代码的本地副本才能完成本快速入门。从我们的 GitHub 存储库下载示例代码(以下命令会克隆整个存储库,但您只需要本快速入门和其他教程的示例)

# Clone the repository to get the example code:
git clone -b v1.66.0 --depth 1 --shallow-submodules https://github.com/grpc/grpc
# Navigate to the "hello, world" Ruby example:
cd grpc/examples/ruby

运行 gRPC 应用程序

examples/ruby 目录中

  1. 运行服务器

    ruby greeter_server.rb
    
  2. 从另一个终端,运行客户端

    ruby greeter_client.rb
    

恭喜!您刚刚使用 gRPC 运行了一个客户端-服务器应用程序。

更新 gRPC 服务

现在让我们看看如何更新应用程序,在服务器上添加一个额外的方法供客户端调用。我们的 gRPC 服务是使用协议缓冲区定义的;您可以在 基础教程 中找到更多关于如何在 .proto 文件中定义服务的信息。现在您只需要知道,服务器和客户端 “存根” 都具有一个 SayHello RPC 方法,该方法从客户端接收 HelloRequest 参数,并从服务器返回 HelloResponse,并且此方法定义如下

// The greeting service definition.
service Greeter {
  // Sends a greeting
  rpc SayHello (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;
}

让我们更新它,使 Greeter 服务具有两个方法。编辑 examples/protos/helloworld.proto,并使用一个新的 SayHelloAgain 方法进行更新,具有相同的请求和响应类型

// 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 代码

接下来,我们需要更新应用程序使用的 gRPC 代码以使用新的服务定义。从 examples/ruby/ 目录中

grpc_tools_ruby_protoc -I ../protos --ruby_out=lib --grpc_out=lib ../protos/helloworld.proto

这将重新生成 lib/helloworld_services_pb.rb,其中包含我们生成的客户端和服务器类。

更新服务器

在同一目录中,打开 greeter_server.rb。像这样实现新方法

class GreeterServer < Helloworld::Greeter::Service

  def say_hello(hello_req, _unused_call)
    Helloworld::HelloReply.new(message: "Hello #{hello_req.name}")
  end

  def say_hello_again(hello_req, _unused_call)
    Helloworld::HelloReply.new(message: "Hello again, #{hello_req.name}")
  end
end

更新客户端

在同一目录中,打开 greeter_client.rb。像这样调用新方法

def main
  stub = Helloworld::Greeter::Stub.new('localhost:50051', :this_channel_is_insecure)
  user = ARGV.size > 0 ?  ARGV[0] : 'world'
  message = stub.say_hello(Helloworld::HelloRequest.new(name: user)).message
  p "Greeting: #{message}"
  message = stub.say_hello_again(Helloworld::HelloRequest.new(name: user)).message
  p "Greeting: #{message}"
end

运行!

就像我们之前做的那样,从 examples/ruby 目录中

  1. 运行服务器

    ruby greeter_server.rb
    
  2. 从另一个终端,运行客户端

    ruby greeter_client.rb
    

下一步

上次修改时间:2024 年 11 月 25 日:feat: move the $ shell line indicator to scss (#1354) (ab8b3af)