Building an Basic API with gRPC and Protobuf in Go

golang.jpg

Repository

https://github.com/golang/go

What Will I Learn?

  • You will learn about gRPC with Go
  • You will learn about Protocol Buffers
  • You will learn how to make a gRPC API
  • You will learn how to generate an RPC
  • You will learn how to wire up a Client and Server with gRPC

Requirements

System Requirements:

Operating System:
  • FreeBSD 10.3 or later
  • Linux 2.6.23 or later with glibc
  • macOS 10.10 or later
  • Windows 7, Server 2008R2 or later

Required Knowledge

  • An understanding of APIs
  • A basic understanding of Go
  • Some understanding of micro service architecture.

Resources for Go and this Project:

Credits/Sources:

Difficulty

  • Advanced

Description

In the last Go video tutorial, we took at look at a basic RPC program. The next logical step towards building microservices is to look at Google's gRPC framework. This gRPC framework makes it much easier to build preformant and robust RPC interfaces. It makes use of code generation and a language neutral, platform-neutral data serializing format called Protocol Buffers. This means that the gRPC protocol can be applied to any language with both servers and clients supporting a wide array of languages.

gRPC, RPC, Code Generation and Go

The gRPC protocol is a performance heavy, open source RPC framework that can run in any environment. It can be used to connect various different services to one another through the use of basic interfaces. These interfaces are built through the use of code generation via Protocol Buffers and can be implemented in a way that allows for the use many different styles of middleware.

interface.png

The image above showcases the server interface that was generated for this project. This interface contains the different RPC commands/functions which can be called by the client service. These functions must be implemented by the server service to register the service on a GRPC server which allows it to be exposed to the client. The generated file also includes different data structures that were specified in the protobuf file.

Serializing data with Protocol Buffers

With the gRPC framework, we need a method of serializing and deserializing data so that each language can understand the interfaces. XML is a language neural data protocol, but it isn't efficient when compared to Protocol buffers. Like with XML, protocol buffers shrink the data into byte code and pass it along a transport system. Protocol buffers make use of a binary format to refer to the data that is being sent and they don't require a ton of metadata like XML. This allows the data transport to be compact and extremely fast.

protobuf.png

Above is the protocol buffer format used to generate the gRPC file for our project. In it we've declared a Request type, a Response type and our service architecture. The service contains two main rpc functions, Add and Multiply both of which take in a Request and return a Response. This file allows us to standardize the method of serialization and transport for the data between our different go services.

The Source Code for this video may be found here: https://github.com/tensor-programming/grpc_tutorial

Video Tutorial

Curriculum

Building a Basic RPC Server and Client with Go

H2
H3
H4
3 columns
2 columns
1 column
5 Comments