Golang grpc service using docker

Himanshu Chaudhary
3 min readAug 25, 2020

Grpc is way to go when you are building micro services. Grpc services can be created in multiple languages. But I have found golang much convenient when working with gRPC to create micro services. This is Part-1 of two parts story.

There is a great tutorial on introduction to gRPC services and languages which can be used to create them.

Grpc is a framework developed by google and supported by Cloud Native. I have created a simple gRPC server for user service and a client to consume it. We are going to use protocol buffers as IDL (Interface definition language) to send and receive data over HTTP2. Here is a link if you want to know more about protocol buffers.

Lets start with creating user-service proto file.

command to generate protocol buffer:-

$ protoc --proto_path=api/proto/v1 --proto_path=third_party --go_out=plugins=grpc:pkg/api/v1 user-service.proto

Above command will create a pb.go protocol buffer file. Let’s create a gRPC server for gRPC services: path to file. rpc > pkg > protocol > grpc > server.go

Once the server is created we can create services by implementing user service interface created inside user-service.pb.go. We will connect to database inside the gRPC function. I am using mysql for database but you can choose which ever you prefer. To start with mysql, create a User table with few columns:-

Next step will be to create gRPC server implementation of Create and ReadAll rpc, which will insert and fetch from database.

Directory structure for gRPC code will be:-

Our code snippet is almost complete. I have a created a file which is used to call the function grpc.RunServer() to create a gRPC server. This fill is also used to get flags used inside go run gRPC server command. The sql connection is created inside this file which will be passed as an struct to be used for the services when inserting and reading from mysql. We can call it as init function for gRPC server. path to file :- rpc > pkg > cmd > server> server.go

Once all the code is in place we can simply run the service, only one last file which is required to run the server, is a small code which is calling RunServer() function. This will be the entry point main.go for gRPC server.

Once the code part is done we need to run go command to run the server. We will pass flags for gRPC-port, database connection details:-

$ go run cmd/server/main.go -grpc-port=9090 -db-host=127.0.0.1:3306 -db-user=<db-user> -db-password=<password> -db-schema=rpc

Above command will run the gRPC-server. Grpc client will dial to server to insert data by passing request and fetch all users from database:-

Run gRPC client to create user and read the users created inside database:-

$ go run cmd/client-grpc/main.go -server=127.0.0.1:9090

That’s it……… a running gRPC server and client.

Next part will be completion of the story to create a gRPC server and running it inside docker container. The gRPC client will connect to server running inside the container. It will contain some excited code on how to create a docker container to use host database and then exposing the port to gRPC client running on host.

Second part to the story show how to deploy a running gRPC service using docker. Follow for updates….

--

--

Himanshu Chaudhary

Cloud Engineer, Kubernetes, Docker, CI/CD, Golang, gRPC, Micro services, git, Infrastructure as a Service, Storage, Compute, Protobuf, REST.