In the last video, we looked at the advantages of Monorepos containing all of the protobuf definitions in a centralized location so that other services could see the endpoints available when working in distributed systems.
In this video, we are going to be focused on defining the protos for our service so that we can start the implementation of the transport layer of our microservice.
Defining our Protos
Let’s start off by creating a new file under rocket/v1
called rocket.proto
. This /v1
directory is important as it allows us to effectively version our protobuf definitions and gives us the ability to make breaking changes in say a v2/
or v3/
directory if we need.
Let’s start off by setting the syntax we want to use to proto3
at the top of our file, and then setting the package as rocket:
syntax = "proto3";
package rocket;
// Rocket represents the model of our rocket
message Rocket {
string type = 1;
}
// RocketService - defines what endpoints we'll be exposing
// from our gRPC rocket service
service RocketService {
rpc GetRocket (GetRocketRequest) returns (GetRocketResponse);
rpc AddRocket (AddRocketRequest) returns (AddRocketResponse);
rpc DeleteRocket (DeleteRocketRequest) returns (DeleteRocketResponse);
}
// -- Get Rocket Handler --
message GetRocketRequest {
int64 id = 1;
}
message GetRocketResponse {
Rocket rocket = 1;
}
// -- --
// -- Add Rocket Handler --
message AddRocketRequest {
Rocket rocket = 1;
}
message AddRocketResponse {
Rocket rocket = 1;
}
// -- --
// -- Add Rocket Handler --
message DeleteRocketRequest {
int64 id = 1;
}
message DeleteRocketResponse {
string status = 1;
}
// -- --
Building our Protos
With this definition in place, let’s look at building the protos and pushing the built files to our repo:
$ protoc --go_out=plugins=grpc:. rocket/**/*.proto
$ git add -A
$ git commit -m "Added built files"
$ git push origin main
Future Improvements
It should be noted that the approach I have taken here is quite rough and ready. It certainly gets the job done for what we are trying to build, however, as the number of services grows, you may want to improve how you do things like make changes to the protos and validate that there are no breaking changes to existing versioned files.
Conclusion
Perfect. So, in this tutorial, we have successfully been able to set up our protobuf monorepo and define the protos for our new Rocket service!
We now have everything in place needed for us to crack on with the building of our gRPC transport layer within our original app.