[Tutorial, Part 1] How to develop Go gRPC microservice with HTTP/REST endpoint, middleware, Kubernetes deployment, etc.

Table of Content

  • This Part 1 is about how to create gRPC CRUD service and client
  • Part 2 is about how to add HTTP/REST endpoint to the gRPC service
  • Part 3 is about how to add middleware (e.g. logging/tracing) to gRPC service and HTTP/REST endpoint as well
  • Part 4 is going to be dedicated how to add Kubernetes deployment configuration with health check and how to build and deploy project to Google Cloud

Prerequisites

  • This article is not training material for Go language. I assume you have some experience already.
  • You have to have installed and configured Go v1.11 before start. We are going to use Go modules capabilities.
  • You have to have experience how to install/configure any SQL database to use it as persistent storage for this tutorial

API first

  • API definition MUST be language-, protocol-, transport- neutral
  • API definition and API implementation MUST be loosely coupled
  • API versioning
  • I need to exclude manual work to sync API definition, API implementation and API documentation. I need API implementation stubs/skeleton and API documentation are generated from API definition automatically.

“To Do list” microservice

  • ID (unique integer identifier)
  • Title (text)
  • Description (text)
  • Reminder (timestamp)

Part 1: create gRPC CRUD service

Step 1: Create API definition

mkdir go-grpc-http-rest-microservice-tutorialcd go-grpc-http-rest-microservice-tutorialgo mod init github.com/<you>/go-grpc-http-rest-microservice-tutorial
mkdir -p api\proto\v1
  • Download Proto compiler binaries here:
  • Extract package to any folder on your PC and add “bin” to PATH environment variable
  • Create “third_party” folder in the “go-grpc-http-rest-microservice-tutorial
  • Copy everything from Proto compiler “include” folder to “third_party” folder:
Result project structure should look like this
  • Install Go language code generator plugin for Proto compiler:
go get -u github.com/golang/protobuf/protoc-gen-go
  • Create protoc-gen.cmd (protoc-gen.sh for MacOS/Linux) file in the “third_party” folder:
  • Create output folder for generated Go files:
mkdir -p pkg/api/v1
  • Ensure we are in go-grpc-http-rest-microservice-tutorial folder and run compilation:
.\third_party\protoc-gen.cmd
./third_party/protoc-gen.sh
Result project structure should look like this
.\third_party\protoc-gen.cmd
./third_party/protoc-gen.sh

Step 2: Develop API implementation using Go language

Step 3: Write API implementation tests

Step 4: Create gRPC server startup

Step 5: Create gRPC client

Step 6: Run gRPC server and client

cd cmd/servergo build .server.exe -grpc-port=9090 -db-host=<HOST>:3306 -db-user=<USER> -db-password=<PASSWORD> -db-schema=<SCHEMA>
2018/09/09 08:02:16 starting gRPC server...
cd cmd/client-grpcgo build .client-grpc.exe -server=localhost:9090
2018/09/09 09:16:01 Create result: <api:"v1" id:13 >2018/09/09 09:16:01 Read result: <api:"v1" toDo:<id:13 title:"title (2018-09-09T06:16:01.5755011Z)" description:"description (2018-09-09T06:16:01.5755011Z)" reminder:<seconds:1536473762 > > >2018/09/09 09:16:01 Update result: <api:"v1" updated:1 >2018/09/09 09:16:01 ReadAll result: <api:"v1" toDos:<id:9 title:"title (2018-09-09T04:45:16.3693282Z)" description:"description (2018-09-09T04:45:16.3693282Z)" reminder:<seconds:1536468316 > > toDos:<id:10 title:"title (2018-09-09T04:46:00.7490565Z)" description:"description (2018-09-09T04:46:00.7490565Z)" reminder:<seconds:1536468362 > > toDos:<id:13 title:"title (2018-09-09T06:16:01.5755011Z)" description:"description (2018-09-09T06:16:01.5755011Z) + updated" reminder:<seconds:1536473762 > > >2018/09/09 09:16:01 Delete result: <api:"v1" deleted:1 >

Resume for Part 1

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store