August 30, 2019

building a gRPC application (part 2)

This is part two of a guided demonstration on how to build an application using gRPC. Each post represents one commit; you may follow along by reading and implementing yourself.

For this lesson, let’s look at how to build language bindings.

Build language bindings

The protocol we created - in order to be used as part of running software - will need to be compiled into a target language binding. This simply means that the protocol itself is not runnable and that you will need to convert it to a runnable format.

Say we run code in the Ruby programming language. We will need to setup your project. Create a source directory for Ruby code and place a Gemfile.

.
├── README.md
├── protocols
│   └── user.proto
└── src
    └── ruby
        └── Gemfile

Specify in the Gemfile that you’ll be using gRPC:

source 'https://rubygems.org'

gem 'grpc'
gem 'grpc-tools'

Perform a bundle install of those gems, then generate the bindings:

cd src/ruby
bundle install
mkdir -p lib
grpc_tools_ruby_protoc -I../../protocols --ruby_out=lib ../../protocols/*.proto
cd -

You should now have the following created in your project:

.
├── README.md
├── protocols
│   └── user.proto
└── src
    └── ruby
        ├── Gemfile
        ├── Gemfile.lock
        └── lib
            └── user_pb.rb

That last file, user_pb.rb, was generated by the gRPC toolchain from the protocol buffer file. You will use it to implement your gRPC service and call the service from your clients.

Multiple, well-maintained language bindings are available. My favorites - corresponding to my preferred programming languages - are Go, Ruby, and Python. There’s plenty more available on the developers website.

Browse the completed code for this lesson on Gitlab: https://gitlab.com/1ijk/guided-demo/tree/fb45afa714ccf190b232c2780f0d62e68e36569f

Content by © Jared Davis 2019-2020

Powered by Hugo & Kiss.