Simple Microservices Using Golang and Twirp RPC (also MongoDB) — Part 1
Today I want to make a documentation about how to build a simple application (service) using Golang and Twirp RPC, and I decide to add MongoDB integration too to make it more interesting. For you who just heard about Twirp, it’s better to go to their official page here to give you a little insight about it.
Ok suppose we will create a simple service called “Product Service”. We will provide some endpoints like Save, Get, Delete, and Update Stock. And make RPC as a main communication protocol to connect to this service.
Twirp RPC, since it is built on protobuf, it is also good for you to read protobuf documentation here.
Let’s start by installing Twirp and protobuf :
- Download protobuf binaries here : https://github.com/google/protobuf/releases
- Copy the protoc binary to our GOPATH bin folder
- Install go packages for twirp and protobuf :
go get github.com/twitchtv/twirp/protoc-gen-twirp
go get github.com/golang/protobuf/protoc-gen-go
Following twirp’s best practice here, our folder and files structure will end up like this:
To keep it simple, the data model for our Product Service will only consist of 3 fields, it is only ProductId, Name, and Stock. Let’s start by creating service.proto file under simple-microservices/product/rpc/product folder. Here’s what that looks like in protobuf:
After that, let’s compile our proto file using protoc binary we’ve installed before:
protoc --proto_path=$GOPATH/src:. --twirp_out=. --go_out=. ./rpc/product/service.proto
It will create 2 files called service.pb.go and service.twirp.go under the same folder. Do not be too concerned about with these 2 files, it is auto generated.
After that, because we will use MongoDB as our database, let’s create the function to connect, create the session or get context to MongoDB. The server credential and database name will be stored in a JSON file, so we must create a function to handle that too.
Suppose the configuration (JSON) file is like this:
I will put it under :
simple-microservices/product/cmd/server/config.json, so it will be easily for our main function to read later on.
Then create common function to read the configuration and prepare the DB session :
Put it under : simple-microservices/product/internal/common/utils.go
Next, create the function to handle DB context. Let’s called it context.go.
And put it under :
simple-microservices/product/internal/productserver/context.go
Well I can’t believe it will be this long, so I decide to make it become 2 parts.
Next part, we will create a controller to provide the business logic for each of endpoints we define in our protobuf. Then a little demo for the client communication using RPC, also don’t worry I will share my github link for this project later on the next part.
Hope this helps.
Updated : Part 2