2 min read

gRPC and protobuf Awesome Microservices Communication Introduction

gRPC is a remote procedure call (RPC) system released by Google. It uses HTTP/2 for transport, Protocol Buffers as the interface description language (IDL) and provides some awesome features such as authentication and some load balancing capabilities for free.

One can use Protocol Buffers to generate data access classes for different programming languages. This is pretty helpful when dealing with multiple languages deployed as separate microservices.

grpc_communication

A sample proto definition. Here, under a filename helloworld.proto can be defined as follows:

// There is a version 2
// So make sure to readup on the correct doc
syntax = "proto3";

package helloworld;

// Look I am doing comments
// 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;
}

Just by running the protoc command, one can generate different classes.

protoc --python_out=. helloworld.proto

Which will generate a file as the following:

# Generated by the protocol buffer compiler.  DO NOT EDIT!
# source: helloworld.proto

import sys
_b=sys.version_info[0]<3 and (lambda x:x) or (lambda x:x.encode('latin1'))
from google.protobuf import descriptor as _descriptor
from google.protobuf import message as _message
from google.protobuf import reflection as _reflection
from google.protobuf import symbol_database as _symbol_database
from google.protobuf import descriptor_pb2
# @@protoc_insertion_point(imports)

_sym_db = _symbol_database.Default()

# and it goes on...

The neat thing is not the generated file. Although I must admit it does some cool Pythoning :) The neat thing is that the protoc command doesn't only generate Python files!
Let us run protoc --help and see what we have available.

  --cpp_out=OUT_DIR           Generate C++ header and source.
  --csharp_out=OUT_DIR        Generate C# source file.
  --java_out=OUT_DIR          Generate Java source file.
  --javanano_out=OUT_DIR      Generate Java Nano source file.
  --js_out=OUT_DIR            Generate JavaScript source.
  --objc_out=OUT_DIR          Generate Objective C header and source.
  --php_out=OUT_DIR           Generate PHP source file.
  --python_out=OUT_DIR        Generate Python source file.
  --ruby_out=OUT_DIR          Generate Ruby source file.

This means that potentially you might have different microservices in all these different languages communicating to each other via gRPC and having a common data object definition via protobuf.

There will be a follow up post that has a working example between a Python app and some other languaged app. It will be fun!

Stay tuned!