Ruby 快速使用 gRPC

更新时间: 2019-07-05 12:52

This guide gets you started with gRPC in Ruby with a simple working example.

Before you begin

Prerequisites

  • ruby: version 2 or higher

Install gRPC

$ gem install grpc

Install gRPC tools

Ruby’s gRPC tools include the protocol buffer compiler protoc and the special plugin for generating server and client code from the .proto service definitions. For the first part of our quickstart example, we’ve already generated the server and client stubs from helloworld.proto, but you’ll need the tools for the rest of our quickstart, as well as later tutorials and your own projects.

To install gRPC tools, run:

gem install grpc-tools

Download the example

You’ll need a local copy of the example code to work through this quickstart. Download the example code from our GitHub repository (the following command clones the entire repository, but you just need the examples for this quickstart and other tutorials):

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

Run a gRPC application

From the examples/ruby directory:

  1. Run the server
   $ ruby greeter_server.rb
  1. In another terminal, run the client
   $ ruby greeter_client.rb

Congratulations! You’ve just run a client-server application with gRPC.

Update a gRPC service

Now let’s look at how to update the application with an extra method on the server for the client to call. Our gRPC service is defined using protocol buffers; you can find out lots more about how to define a service in a .proto file in gRPC Basics: Ruby. For now all you need to know is that both the server and the client “stub” have a SayHelloRPC method that takes a HelloRequest parameter from the client and returns a HelloResponse from the server, and that this method is defined like this:

// 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;
}

Let’s update this so that the Greeter service has two methods. Edit examples/protos/helloworld.proto and update it with a new SayHelloAgain method, with the same request and response types:

// 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;
}

(Don’t forget to save the file!)

Generate gRPC code

Next we need to update the gRPC code used by our application to use the new service definition. From the examples/ruby/ directory:

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

This regenerates lib/helloworld_services_pb.rb, which contains our generated client and server classes.

Update the server

In the same directory, open greeter_server.rb. Implement the new method like this

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
...

Update the client

In the same directory, open greeter_client.rb. Call the new method like this:

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

Run!

Just like we did before, from the examples/ruby directory:

  1. Run the server
   $ ruby greeter_server.rb
  1. In another terminal, run the client
   $ ruby greeter_client.rb

What’s next

创业头条

创业头条

[美]兰德尔•莱恩(Randall Lane)及《福布斯》杂志编辑部 / 孙莹莹 / 浙江人民出版社 / 2015-6 / 54.90

[内容简介] 全民创业的浪潮中,如何抓住共享经济带来的机遇?没有营收模式还一直烧钱的公司,如何赢得投资人的青睐?一轮死、二轮死、N轮死的魔咒下,怎样才能成功活下来?面对数十亿美元的收购要约,创始人究竟应该如何抉择?没有资金又不懂技术,是否就无法分享互联网创业的红利?《创业头条》一书将为你揭秘上述问题的答案。 阅读《创业头条》一书你会发现,在硅谷最新崛起的互联网亿万富豪身上,有这样一......一起来看看 《创业头条》 这本书的介绍吧!

图片转BASE64编码

图片转BASE64编码

在线图片转Base64编码工具

XML 在线格式化

XML 在线格式化

在线 XML 格式化压缩工具

html转js在线工具

html转js在线工具

html转js在线工具