This tutorial provides a basic Ruby programmer’s introduction to working with gRPC.
By walking through this example you’ll learn how to:
It assumes that you have read the Overview and are familiar with protocol buffers. Note that the example in this tutorial uses the proto3 version of the protocol buffers language, which is currently in alpha release: you can find out more in the proto3 language guide and see the release notes for the new version in the protocol buffers GitHub repository.
Our example is a simple route mapping application that lets clients get information about features on their route, create a summary of their route, and exchange route information such as traffic updates with the server and other clients.
With gRPC we can define our service once in a .proto file and implement clients and servers in any of gRPC’s supported languages, which in turn can be run in environments ranging from servers inside Google to your own tablet - all the complexity of communication between different languages and environments is handled for you by gRPC. We also get all the advantages of working with protocol buffers, including efficient serialization, a simple IDL, and easy interface updating.
The example code for our tutorial is in
grpc/grpc/examples/ruby/route_guide.
To download the example, clone the grpc
repository by running the following
command:
$ git clone -b v1.17.1 https://github.com/grpc/grpc
$ cd grpc
Then change your current directory to examples/ruby/route_guide
:
$ cd examples/ruby/route_guide
You also should have the relevant tools installed to generate the server and client interface code - if you don’t already, follow the setup instructions in the Ruby quick start guide.
Our first step (as you’ll know from the Overview) is to
define the gRPC service and the method request and response types using
protocol
buffers. You can
see the complete .proto file in
examples/protos/route_guide.proto
.
To define a service, you specify a named service
in your .proto file:
service RouteGuide {
...
}
Then you define rpc
methods inside your service definition, specifying their
request and response types. gRPC lets you define four kinds of service method,
all of which are used in the RouteGuide
service:
// Obtains the feature at a given position.
rpc GetFeature(Point) returns (Feature) {}
stream
keyword before the response type.// Obtains the Features available within the given Rectangle. Results are
// streamed rather than returned at once (e.g. in a response message with a
// repeated field), as the rectangle may cover a large area and contain a
// huge number of features.
rpc ListFeatures(Rectangle) returns (stream Feature) {}
stream
keyword before the request type.// Accepts a stream of Points on a route being traversed, returning a
// RouteSummary when traversal is completed.
rpc RecordRoute(stream Point) returns (RouteSummary) {}
stream
keyword before both the request and the response.// Accepts a stream of RouteNotes sent while a route is being traversed,
// while receiving other RouteNotes (e.g. from other users).
rpc RouteChat(stream RouteNote) returns (stream RouteNote) {}
Our .proto file also contains protocol buffer message type definitions for all
the request and response types used in our service methods - for example, here’s
the Point
message type:
// Points are represented as latitude-longitude pairs in the E7 representation
// (degrees multiplied by 10**7 and rounded to the nearest integer).
// Latitudes should be in the range +/- 90 degrees and longitude should be in
// the range +/- 180 degrees (inclusive).
message Point {
int32 latitude = 1;
int32 longitude = 2;
}
Next we need to generate the gRPC client and server interfaces from our .proto
service definition. We do this using the protocol buffer compiler protoc
with
a special gRPC Ruby plugin.
If you want to run this yourself, make sure you’ve installed protoc and followed the gRPC Ruby plugin installation instructions first):
Once that’s done, the following command can be used to generate the ruby code.
$ grpc_tools_ruby_protoc -I ../../protos --ruby_out=../lib --grpc_out=../lib ../../protos/route_guide.proto
Running this command regenerates the following files in the lib directory:
lib/route_guide.pb
defines a module Examples::RouteGuide
lib/route_guide_services.pb
, extends Examples::RouteGuide
with stub and
service classes
Service
for use as a base class when defining RouteGuide service
implementationsStub
that can be used to access remote RouteGuide instancesFirst let’s look at how we create a RouteGuide
server. If you’re only
interested in creating gRPC clients, you can skip this section and go straight
to Creating the client (though you might find it interesting
anyway!).
There are two parts to making our RouteGuide
service do its job:
You can find our example RouteGuide
server in
examples/ruby/route_guide/route_guide_server.rb.
Let’s take a closer look at how it works.
As you can see, our server has a ServerImpl
class that extends the generated
RouteGuide::Service
:
# ServerImpl provides an implementation of the RouteGuide service.
class ServerImpl < RouteGuide::Service
ServerImpl
implements all our service methods. Let’s look at the simplest type
first, GetFeature
, which just gets a Point
from the client and returns the
corresponding feature information from its database in a Feature
.
def get_feature(point, _call)
name = @feature_db[{
'longitude' => point.longitude,
'latitude' => point.latitude }] || ''
Feature.new(location: point, name: name)
end
The method is passed a _call for the RPC, the client’s Point
protocol buffer
request, and returns a Feature
protocol buffer. In the method we create the
Feature
with the appropriate information, and then return
it.
Now let’s look at something a bit more complicated - a streaming RPC.
ListFeatures
is a server-side streaming RPC, so we need to send back multiple
Feature
s to our client.
# in ServerImpl
def list_features(rectangle, _call)
RectangleEnum.new(@feature_db, rectangle).each
end
As you can see, here the request object is a Rectangle
in which our client
wants to find Feature
s, but instead of returning a simple response we need to
return an Enumerator that
yields the responses. In the method, we use a helper class RectangleEnum
, to
act as an Enumerator implementation.
Similarly, the client-side streaming method record_route
uses an
Enumerable, but here it’s
obtained from the call object, which we’ve ignored in the earlier examples.
call.each_remote_read
yields each message sent by the client in turn.
call.each_remote_read do |point|
...
end
Finally, let’s look at our bidirectional streaming RPC route_chat
.
def route_chat(notes)
q = EnumeratorQueue.new(self)
t = Thread.new do
begin
notes.each do |n|
...
end
end
q = EnumeratorQueue.new(self)
...
return q.each_item
end
Here the method receives an Enumerable, but also returns an Enumerator that yields the responses. The implementation demonstrates how to set these up so that the requests and responses can be handled concurrently. Although each side will always get the other’s messages in the order they were written, both the client and server can read and write in any order — the streams operate completely independently.
Once we’ve implemented all our methods, we also need to start up a gRPC server
so that clients can actually use our service. The following snippet shows how we
do this for our RouteGuide
service:
addr = "0.0.0.0:8080"
s = GRPC::RpcServer.new
s.add_http2_port(addr, :this_port_is_insecure)
logger.info("... running insecurely on #{addr}")
s.handle(ServerImpl.new(feature_db))
s.run_till_terminated
As you can see, we build and start our server using a GRPC::RpcServer
. To do
this, we:
ServerImpl
.add_http2_port
method.GRPC::RpcServer
.run
on theGRPC::RpcServer
to create and start an RPC server for our
service.In this section, we’ll look at creating a Ruby client for our RouteGuide
service. You can see our complete example client code in
examples/ruby/route_guide/route_guide_client.rb.
To call service methods, we first need to create a stub.
We use the Stub
class of the RouteGuide
module generated from our .proto.
stub = RouteGuide::Stub.new('localhost:50051')
Now let’s look at how we call our service methods. Note that the gRPC Ruby only provides blocking/synchronous versions of each method: this means that the RPC call waits for the server to respond, and will either return a response or raise an exception.
Calling the simple RPC GetFeature
is nearly as straightforward as calling a
local method.
GET_FEATURE_POINTS = [
Point.new(latitude: 409_146_138, longitude: -746_188_906),
Point.new(latitude: 0, longitude: 0)
]
..
GET_FEATURE_POINTS.each do |pt|
resp = stub.get_feature(pt)
...
p "- found '#{resp.name}' at #{pt.inspect}"
end
As you can see, we create and populate a request protocol buffer object (in our
case Point
), and create a response protocol buffer object for the server to
fill in. Finally, we call the method on the stub, passing it the context,
request, and response. If the method returns OK
, then we can read the response
information from the server from our response object.
Now let’s look at our streaming methods. If you’ve already read Creating the
server some of this may look very familiar - streaming RPCs are
implemented in a similar way on both sides. Here’s where we call the server-side
streaming method list_features
, which returns an Enumerable
of Features
resps = stub.list_features(LIST_FEATURES_RECT)
resps.each do |r|
p "- found '#{r.name}' at #{r.location.inspect}"
end
The client-side streaming method record_route
is similar, except there we pass
the server an Enumerable
.
...
reqs = RandomRoute.new(features, points_on_route)
resp = stub.record_route(reqs.each, deadline)
...
Finally, let’s look at our bidirectional streaming RPC route_chat
. In this
case, we pass Enumerable
to the method and get back an Enumerable
.
resps = stub.route_chat(ROUTE_CHAT_NOTES)
resps.each { |r| p "received #{r.inspect}" }
Although it’s not shown well by this example, each enumerable is independent of the other - both the client and server can read and write in any order — the streams operate completely independently.
Build client and server:
$ # from examples/ruby
$ gem install bundler && bundle install
Run the server, which will listen on port 50051:
$ # from examples/ruby
$ bundle exec route_guide/route_guide_server.rb ../python/route_guide/route_guide_db.json
$ # (note that the route_guide_db.json file is actually language-agnostic; it's just
$ # located in the python folder).
Run the client (in a different terminal):
$ # from examples/ruby
$ bundle exec route_guide/route_guide_client.rb ../python/route_guide/route_guide_db.json