Skip to content

Server: Generating Stubs

Shaun McCormick edited this page Jun 17, 2019 · 4 revisions

Gruf follows the gRPC standards for server/client stub generation, defined in the gRPC Ruby Quickstart Guide here.

The summary is first you'll need a protobuf file. You can include this file in the same repository as your service, or in a separate repository that you are generating files from.

In our experience, having all your protobufs for your platform in a single repository is best. We call this an "Interface Definition Library", and for us, it contains all the proto files that define all of our internal APIs. You can then generate the stubs for those on merge/builds, and publish a gem with that repository (say my-company-interfaces) that includes the generated files. (We do this by modifying the $LOAD_PATH and letting autoloading do its thing.)

This allows you to easily treat your protobufs as a centralized, easy-to-find contract definition across your platform, and reduces the cost of service decomposition as you don't have to reimplement generation code every time you launch a new service.

Example Proto

Let's create a new file, called Jobs.proto:

syntax = "proto3";

package demo;

service Jobs {
    rpc GetJob(GetJobReq) returns (GetJobResp) { }
}

message GetJobReq {
    uint64 id = 1;
}

message GetJobResp {
    uint64 id = 1;
    string name = 2;
}

This will generate a new service called demo.Jobs, which will be available as a gRPC server stub when generated as Demo::Jobs::Service.

You can do this like so, after running gem install grpc grpc-tools to get the grpc_tools_ruby_protoc binary:

grpc_tools_ruby_protoc --ruby_out=./ --grpc_out=./ ./Jobs.proto

Once you do this, you'll find you now have your server/message stubs created for you.


Next: Creating Controllers