gRPC with Go - Introduction

gRPC With Go Crash Course – Introduction


This article is part of the series gRPC With Go Crash Course

In the following set of articles, you’ll learn how gRPC works by building several simple web applications and hook them together using gRPC.

The articles won’t go into details of what gRPC is and what’s its purpose. There’s a lot of existing material on the subject already. Instead, they focus on helping you get some real practice using gRPC, integrating it into your own applications & understanding the core mechanisms it provides.

First, we’ll cover what we’re about to build & the environment setup you’ll need for the rest of the articles.

Additionally, have in mind that all the parts of the course are written in a way that they are independent from each other. So if you’re not interested in the whole course and want to jump straight to Bidirectional Streams (for example), feel free to do so.

What we’re building

The system we’re building is a casino, which supports buying and selling tokens in exchange for dollars. Those tokens can be used to gamble on the fluctuating stock price of an imaginary stock called AwesomeStock.

The system also supports generating a payment statement, which provides a breakdown of all the transactions you’ve made.

Here’s what the application looks like in the end:

grpc with go - final application

Use-cases and gRPC Routines

This section outlines the different use-cases and routines our web applications will support.

BuyTokens, Withdraw and Balance

These are simple request-response style routines invoked by the client application to the casino web service.

grpc request-response communication

Payment Statement

The client invokes this routine for the casino web service. The casino web service then forwards all payments of the user to the payment_statements web service via client-side streaming. 

grpc client-side streaming

The payment_statements service outputs a nicely-formatted summary of a user’s transactions.

Get Payments

The GetPayments routine returns a similar result to the payment statement. It shows a history of a user’s payments. What’s interesting about it is that it uses server-side streaming

grpc server-side streaming

Gamble

The most complex routine the casino will support is Gamble. It is a bidirectional stream routine. On one end, it will provide a constant stream of price updates for AwesomeStock and the client will consume that data and show it to the user.

grpc bidirectional streaming

On the other hand, the client will process user input (e.g buy or sell) and propagate the commands to the casino server, which process the stock purchase for the user.

Prerequisites

This section covers what you’ll need to setup before proceeding with the rest of the articles. If you go through all these steps successfully, you shouldn’t experience any other issues through the rest of the course.

Git + Git Bash (Windows)

Install git following the instructions for your OS from here.

If you’re using Windows, make sure you always use Git Bash for running any of terminal commands in this document and in the rest of the materials:

starting git bash

Don’t use cmd or PowerShell as the commands won’t work as-is on those shells. 

Only use those if you feel sufficiently proficient with them to be able to transform the bash-based commands to PS/CMD commands.

Go

This is a Go crash course, after all, so you need to have Go installed. 🙂

Install it following the instructions for your OS from here.

Protoc

This is the protobuf compiler we’ll use to generate the code via our schema definitions.

To install it, follow the instructions for your platform here.

If you are on Windows, then download this file, unzip it and put bin/protoc.exe somewhere in your $PATH.

Go plugins for the Protobuf compiler

Install the necessary go plugins which are to be used by protoc by following the instructions here

Setup Project

Run this command to clone the final branch of the project we’re building:

git clone --branch v6-finale https://github.com/preslavmihaylov/go-grpc-crash-course


Enter the directory, where you’ve cloned the project & install the dependencies:

go mod download

Generate the gRPC schema definitions:

./make_protos.sh

Run these three commands in different terminal tabs:

go run payment_statements/*.go
go run client/*.go
go run casino/*.go


In the tab where you’ve started client/*.go, execute the commands you see in the gif below to verify you’re able to run the program we’ll build in the workshop:

final application demo

Finale

If you’ve completed all these steps and you haven’t encountered any issues, then congratulations! You’re all set up for the rest of the exercises. 🚀

All that’s left is for you to bring your good spirit and eagerness to tinker with an exciting technology. 🙂

If you’ve encountered any issues throughout this setup, feel free to email me at preslav@pmihaylov.com and I’ll be sure to address them.

Go to Part 1 – Service Schemas

Site Footer

BulgariaEnglish