Communication between Golang Apps with gRPC

Radityo Hernanda
KANO Engineering
Published in
4 min readApr 26, 2019

--

If you just know about REST API, then i will introduction you with gRPC.

You can use gRPC when dealing communication between two or more application. It’s a also good choice to use when we dealing with microservice architecture.

have a different language? Don’t worry! gRPC also support other language like Java. So you can send/receive message from your Go Application and Java Application.

Making friend between Go and gRPC isn’t hard. Almost two of them have engineer from google. We just need to put some motivation and coffee into it. Let’s get started!

First, we need to relax our mind. Our purpose making application is simple.

  • Create 2 application. i give name “Alpha” as Client and “Zero” as Server
  • Alpha will request and sending valuename to Zero
  • Zero Will response with message include name (i.e Hello {name})
  • Make it Works!

Requirement is quite simply

  • Golang 1.9+
  • gRPC + Protobuf

I assume we already install Golang 1.9+ to our machine. Then we need to download gRPC and Protobuf

Prepare gPRC and Protobuf

Firstly we need to download grpc for golang and protoc-gen-go for generating protobuf grpc for go.

go get -u google.golang.org/grpc
go get -u github.com/golang/protobuf/{proto,protoc-gen-go}

protoc-gen-go will be installed inside $GOBIN or by default in $GOPATH/bin you need to add them to $PATH so we can call them late

export PATH=$PATH:$GOPATH/bin

If you are using windows you can set to PATH environment.

You can test if your environment is ready, create 1 file example.proto

syntax = "proto3";
package protobuf;
service Example {}

Then try to generate protobuf file using protoc

protoc --go_out=plugins=grpc:. example.proto

Yay, now you already create Protobuf file and generated gRPC service for golang (example.pb,go).

Protobuf Model

Create protobuf/message.proto

Create MessageService as service and one request Get with request parameter Request model and return Response model. in this section we have 2 model dataRequest and Response and 1 Service MessageService with 1 rpc Get .

Assume client will doing request Get and passing model Request with some data name . Server will receive Request model and return Response model.

Then in our client we just read the response model. that’s it!

Next, we just need Compile into model into go

protoc --go_out=plugins=grpc:. message.proto# if you want to compile all *.proto files with output folder
go_out="$GOPATH\src\raditzlawliet\mgent\protobuf"
protoc --go_out=plugins=grpc:"$go_out" *.proto

Create Server Service

create new main file for server zero/main.go Then we can create struct for server and handling request from client.

In this code, we define server struct with

Then we creating basic main func to start the server

Try to running

go run zero/main.go

Zero gRPC server will listen on port 9995 by default

Server is up!

Building Client and Request to Server

Create new main for client alpha/main.go create some client struct

We just create some Connect function and GetMessage function. Connect function will connect our client into server and creating service. After that we can use GetMessage to sending request and get the response from server

Now we can create basic main func for client request to server

Try to running (don’t forget to run the server first)

go run alpha/main.go

You will see the result something like this

Client send request to server
Server receive request from client

Conclude

Fantastic, gRPC make communication between application easier, gRPC also support multiple language like Java, PHP, C# and many more! Even gRPC is powerfull for build communication especially in microservice architecture.

I put the code in here https://github.com/raditzlawliet/go-grpc-example so you can use it for reference, practice or even boilerplate your project.

And you also can read more complete basic guide from official site https://grpc.io/docs/tutorials/basic/go.html

What will you use gRPC for ? Let me know

--

--

Radityo Hernanda
KANO Engineering

Software Engineer and Game Dev. Enthusiast at My Studio, The Flavare