Getting started with gRPC API in Golang
REST APIs have long been dominating the development world, but there is a new player in town — gRPC! In this article, let’s explore what exactly gRPC is and also build a simple starter API using Golang.
What is gRPC?
gRPC is an open source framework developed by Google for performing Remote Procedure Calls (RPC). This provides an efficient way for us to connect multiple services. The best part of gRPC is that we can directly call an API as if it was any other method!
Here’s a reference from the official gRPC docs:
“In gRPC, a client application can directly call a method on a server application on a different machine as if it were a local object, making it easier for you to create distributed applications and services.”
Also, gRPC clients and servers can communicate with each other in various environments irrespective of the language or framework used.
Arun Mathew Kurian has written a great article here that explores the differences between REST and RPC architectures in detail. Do check it out!
Getting Started With gRPC in Go
Let’s try developing a simple gRPC API in Go. Before we start, make sure you have the following prerequisites ready:
Install Go
You will need Go installed. Check out the official link here to download.
Install protoc
protoc
is a protocol buffer compiler. Check the instructions here to install.
Install Protocol Compiler Plugins
Install protocol compiler plugins through the below commands.
go install google.golang.org/protobuf/cmd/protoc-gen-go@v1.26
go install google.golang.org/grpc/cmd/protoc-gen-go-grpc@v1.1
Update PATH
Use the following command to update the path:
export PATH="$PATH:$(go env GOPATH)/bin"
Creating a New Go Project
For simplicity, we will be doing all of the coding in the same package. Create a folder called go_grpc
and execute the below commands within this folder:
go mod init go_grpc
go get google.golang.org/grpc
go get google.golang.org/protobuf/reflect/protoreflect@v1.25.0
go get google.golang.org/protobuf/runtime/protoimpl@v1.25.0
Creating a Proto File
Now that we are ready with the setup, let’s create a proto file where we define our API. Let’s create an API that returns the details of a user.
Create a file called user.proto
. In our proto definition, we will define the request and response objects with the required fields.
We have defined a service called UserService
that provides a rpc (similar to a function) called GetUser
.
From our definition, let’s generate a Go file. To do this, we execute the following command:
protoc --go_out=. --go_opt=paths=source_relative \
--go-grpc_out=. --go-grpc_opt=paths=source_relative \
users.proto
This will create two files, users.pb.go
and users_grpc.pb.go
. Both the files contain all the required code to populate, serialize, and retrieve the messages we have defined in our proto file.
It will also contain generated code for the client and server.
Creating a Server
Next, create a main.go
file, and in our main
function, let’s create the gRPC server and start listening to port 8000
.
Our server’s main goal is to server requests of GetUser
. It prints out the id
in the request body and returns a standard response where the name is “John Doe.”
We have added a conditional check for a command-line argument. Since we are putting all the files together in the same package, we will accept a command-line argument that specifies if we are kickstarting a server or a client.
Creating a Client
Finally, let’s also create a client that can make a gRPC call, GetUser
, to our server.
Notice that the way we make the call is analogous to calling a function. All we are doing is client.GetUser(context.Background(), request)
and this gives us back the response from the server.
Running Our Server and Client
Before we run our code, let’s build it using go build .
from the root of the folder. This will create an executable go_grpc
in the root directory.
Now, let’s open up two terminals — one for our server, one for our client.
In the first terminal, run ./go_grpc server
. If you have successfully run the server and client, you should immediately see the following output:
In the second terminal, start the client using ./go_grpc client
and you should see that both the server terminal and client terminal would have printed out the following output:
And there we go! Our gRPC server and client are ready.
Conclusion
In this article, we saw how easy it is to define an API and have a server and client-generated using protoc
. One of the main advantages of using gRPC is that since it uses protocol buffers over the standard JSON or XML in REST, there is better encoding of data.
Also, gRPC allows us to use HTTP2 as the transfer protocol. Given that it integrates really well with various languages, it would be a great option for us to have efficient communication between various microservices too.
Napon Mekavuthikul has also written a great article on how gRPC can save development time here. Do give it a read to appreciate how we can further improve our development processes and lifecycle with gRPC.
Give gRPC a try for your next project, and share your experience!