Building an Basic API with gRPC and Protobuf in Go

in #utopian-io6 years ago

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

Sort:  

Hi @tensor

Thank you again for another great video tutorial.

This tutorial is more advanced and I appreciate the fact that you spend more time explaining the concept in detail so that students can follow along.

You are doing good.

Your contribution has been evaluated according to Utopian policies and guidelines, as well as a predefined set of questions pertaining to the category.

To view those questions and the relevant answers related to your post, click here.


Need help? Chat with us on Discord.

[utopian-moderator]

Thank you for your review, @rosatravels! Keep up the good work!

Hi @tensor!

Your post was upvoted by @steem-ua, new Steem dApp, using UserAuthority for algorithmic post curation!
Your post is eligible for our upvote, thanks to our collaboration with @utopian-io!
Feel free to join our @steem-ua Discord server

Hi, @tensor!

You just got a 5.58% upvote from SteemPlus!
To get higher upvotes, earn more SteemPlus Points (SPP). On your Steemit wallet, check your SPP balance and click on "How to earn SPP?" to find out all the ways to earn.
If you're not using SteemPlus yet, please check our last posts in here to see the many ways in which SteemPlus can improve your Steem experience on Steemit and Busy.

Hey, @tensor!

Thanks for contributing on Utopian.
We’re already looking forward to your next contribution!

Get higher incentives and support Utopian.io!
Simply set @utopian.pay as a 5% (or higher) payout beneficiary on your contribution post (via SteemPlus or Steeditor).

Want to chat? Join us on Discord https://discord.gg/h52nFrV.

Vote for Utopian Witness!

Coin Marketplace

STEEM 0.21
TRX 0.20
JST 0.034
BTC 98914.40
ETH 3374.27
USDT 1.00
SBD 3.08