Further Exploration of gRPC
In the last blog, we talked about what gRPC is and how to use it to build a simple service. In this blog, we will continue our journey of exploring gRPC by covering topics like client/service streaming (unidirectional and bi-directional) and “language interpolation”, where the client and server are written in different languages but communicate with each other using protobuf.
We will keep the service that is written by Kotlin in the previous blog but will change client implementation to Golang in this blog. So if you are not familiar with Golang, I would recommend you go through the official quick tutorial to get a basic understanding of it.
Some preparation is needed before we start diving into coding.
- The first thing is to make sure we install Golang. If you are a new user of Golang, I recommend following the official installation doc. There is also another way of installing it using gvm, which is similar to nvm if you know it.
- If you followed the previous blog, I assume you already have protobuf compiler installed.
- Next, we have to install a go plugin for protobuf compiler. Run this command to install it:
go get google.golang.org/protobuf/cmd/protoc-gen-goand
go get google.golang.org/grpc/cmd/protoc-gen-go-grpc
After that, update your
PATHenvironment variable so that the
protoccompiler can see these plugins,
export PATH="$PATH:$(go env GOPATH)/bin". It would be better to put it in your
- Finally, IDE installation. I recommend using Goland. Of course, it’s not required. You could also use other editors like vscode and then install go related extensions. I personally found that Goland has better support for Golang. If you have Intellij Idea installed, you can just go to the marketplace to install a Golang extension, and after that it functions pretty much the same as Goland.
After everything mentioned above is installed, now let’s set up our go project. Create an empty folder name it whatever you want, I named it
cd to the newly created folder. Inside it, let's initialize our go project using the go module by running the following command:
go mod init <module name>
You can choose any name for the module name, I named it
<my name>/go-grpc. After that, you should see a
go.mod file generated for you. Inspecting the file, you should see a
module <module name> at the top, followed by the go version.
Next, let’s install the dependencies we need for our project. First install go protobuf. Run the following command:
go get github.com/golang/protobuf/proto
Then, install the grpc-go by running the command:
go get google.golang.org/grpc
Now if you inspect the
go.mod file, you should see a
require section which lists all the dependencies we just installed.
Next is to copy the
blog.proto file we created in the previous blog to the current folder. But before that, we have to edit it to add one line:
option go_package="generated/blog"; to make it work with our go project. You can put this line after or before the existing two options. Now create a folder called
protos inside the folder where
go.mod sits, then copy the
blog.proto file and paste it into the newly created folder. The last step is to create a script file that contains the command to generate proto codes for us. We will use this a lot. I named it
generate_ptoto_codes.sh with the following content:
if [ -d "./generated" ]
rm -rf ./generated/*
mkdir -p ./generated
protoc -I protos/ protos/*.proto --go_out=plugins=grpc:./
Now everything is set up and ready to go, let’s get into coding!
Unary (Simple Client/Server)
Let’s implement the simple client/server method we defined last time. Run the script file to generate go grpc code first, then create a new folder called
client in which we will place our client code. Up until now, your project structure should look like this:
client folder, create a file named
blog.go or whatever name you want. Write an
initialize function, which we will reuse a lot later. The function should look like below:
It returns a connection and blog client (the required packages should be auto imported by your IDE or code editor if you have golang extensions installed). Now let’s implement the unary example we defined last time. Run the
generate_ptoto_codes.sh script to generate proto code, then put this piece of code after the
Callibrity has locations in Cincinnati and Columbus, Ohio, with a national reach. Callibrity meets its clients wherever they are on their digital journey, specializing in software engineering, digital transformation, cloud strategy, and data-driven insights. Callibrity provides subject matter expertise and solves complex problems with simple solutions for ever-changing business models. More information can be found at Callibrity.com.