Starting a RPC server in Golang using gRPC and Protocol Buffers v3

In today’s age of web development, REST is pretty much the standard for building a web based API.In the current project I am working on, I developed a microservice based architecture and there are some calls that are not completely stateless and are somewhat a lot heavier than basic CRUD operations.I decided to go with RPC instead of REST , maybe influenced a bit by the fact that I haven’t yet come to work with it and i was willing to experience with it.

A problem encountered, was the data interchanging between services, as i was not willing to work with XML.After some research I came across gRPC and protocol buffers.

gRPC is a RPC framework that enables client-server communication.Is it based on the idea of defining a service , specifying the methods that can be called remotely by the client with the specified parameters and return types.The service interface would be defined using protocol buffers ,wich according to the documentation ;“Protocol buffers are a flexible, efficient, automated mechanism for serializing structured data — think XML, but smaller, faster, and simpler”, I was sold solely by this statementand and it was pretty intuitevely to use too.So after some little research i found a reliable solution for my microservices architecture .An overview on protobuf and gRPC can be found here:

Getting started

To get started with protocol buffers , you need the protocol buffer compiler that you can get from :

We now need the plugins required for go , wich can be easily achieved running :

go get
go get

All left to do now is defining the service in a .proto file and generating the data access classes.

A message is a small record of information, that holds data types as fields including bool, int32, float, double, and string. You can also add further structure to your messages by using other message types as field types.The number thingy is a unique tag identifier and there are some interesting stuff you can use with this tag that could enforce backwards compatibilty ; that I won’t get into in this article but can be found in the link below.

To generate the access code run the following :

protoc -I=$SRC_DIR --go_out=$DST_DIR $SRC_DIR/addressbook.proto ;
or assuming you have more proto files all in one folder (example proto), you can use the following command to compile all proto files:
protoc --go_out=plugins=grpc:. proto/*.proto

The generated .go code file holds both our message payloads and service interface and should look like this:

So with the use of protocol buffers we generated a RPC server and client that we just need to provide implementation in our API.

Starting our RPC server

There are 2 parts in creating our server: Implementing the service interface generated from the service definition.Secondly we have to start the gRPC server and register our implementation ; and it will be ready to listen for requests from our clients.

A pretty basic implementation of our generated interface that just takes the email from request and pass it to the response(yes , I know , very usefull).

And all that is left to do is creating the gRPC client.

To call service methods, we first need to create a client connection to the given target. We create this by passing the server address and port number togrpc.Dial() . You can use DialOptions to set the auth credentials (or in this case no credentials).Next all we need is a stub client, and we are all set to make some calls to the RPC server.

That’s pretty much it, a basic RPC server using gRPC and protocol buffers.

Like what you read? Give Alex Popa a round of applause.

From a quick cheer to a standing ovation, clap to show how much you enjoyed this story.