-
Notifications
You must be signed in to change notification settings - Fork 0
Add A Microservice
First thing first, in this project, we're using gRPC for our server side.
For now, it consists of a constellation of microservices which are started by an entrypoint, the main.go
file at the root of the server/
folder.
A microservice consists of the smallest functional business unit you could have.
If you're more familiar with monolithic REST APIs, you can (often) consider a resource as a microservice.
So, if you wanna add a micro service, please follow this rules:
- Your work directory is the
server/
folder. - Here, create a sub-folder named after your service's name.
- In this sub-folder, create two other sub-folders:
serviceName_server/
andserviceNamepb/
.
The serviceName_server/
folder aims to contain the main intelligence of your server.
Name the main file in here server.go
(if you're writing go).
You still can create other sub-folders in this folder if you wanna split your code (it's also a good practice :) ).
The serviceNamepb/
folder contains the generated code from the protofiles. Note that your protofiles should be created at the root of the repo, in the protofiles/
. Also, don't forget to add the generation script in a commentary at the top of the file and to add to the generateProtos.sh
.
Don't forget to generate your code before pushing to the repo plz.
Your server.go
file should at least contain this to start the gRPC server:
type server struct{}
func StartServer() {
lis, err := net.Listen("tcp", "0.0.0.0:50051")
if err != nil {
log.Fatalf("Failed to listen %v \n", err)
}
s := grpc.NewServer()
// Register Service here
reflection.Register(s) // allows us to expose the gRPC server so the client can see what's available. You can use Evans CLI for that
if err := s.Serve(lis); err != nil {
log.Fatalf("Failed to serve: %v", err)
}
}
Please take care that the port you're chosing is not already taken by another service (you can see that in the compose-file). Don't pick up a random port tho, just increment from the last port number used.
Now that your gRPC server has been written, you've got to start it.
As said above, we, for now, use a main entrypoint which is the main.go
at the root of the server/
folder.
In the main function, just call the StartServer()
method from the service you've just created. However, it's better if you launch it in a different thread, using goroutines.
So you'd write something like:
go serviceName_server.StartServer()
Ja, good!
It might not be the best approach, and we'll consider handling all these micro-services separately in a nereast future.
Actually, for now, edit the docker-compose.dev.yml
at the root of the repo.
Add your port binding to the service named server
.
And if your stack is already running, just type docker-compose -f docker-compose.yml -f docker-compose.dev.yml restart server
.
Don't waste your time writing a gRPC client to call your methods.. Just run the compose-stack and use evans CLI.
The command you'll have to use to connect is:
evans -r repl -p PORT_NUMBER
Then, refer to the evans CLI doc to learn how to use it (or dm me).
You're now ready to Go :) Feel free to open an issue, or edit this if you feel that it is unclear, or that I'm missing something.