The second micro-service (M2) was implemented using Go and some nice libs such as: (i) cobra to create a command line interface, (ii) Viper to read .env files, (iii) GIN to provide a web-service, and (iv) Gocron to trigger a task periodically.
M2 consumes the EMAIL queue as a message is ready. Then, the message is unmarshalled and a welcome email is sent to the added user. Then, information weather the email was correctly send or not is persisted into the database. Using Gocron a task periodically check if there is any email that wasn’t sent - if so, we try to send the email again.
Nowadays there are a lot of micro-services out there. One way to accomplish the communication between them is by using message-broker, such as RabbitMQ Kafka.
I have created two isolate micro-services as can be seen in the figure. The first micro-service (M2) was devised using Java Spring Boot.
M1 is just responsible to validade and persist a user into the database. Then, a message is sent to the RabbitMQ (message-broker). All messages are put into a queue (FIFO).
The second micro-service (M2) was implemented using Go and some nice libs such as: (i) cobra to create a command line interface, (ii) Viper to read .env files, (iii) GIN to provide a web-service, and (iv) Gocron to trigger a task periodically.
M2 consumes the EMAIL queue as a message is ready. Then, the message is unmarshalled and a welcome email is sent to the added user. Then, information weather the email was correctly send or not is persisted into the database. Using Gocron a task periodically check if there is any email that wasn’t sent - if so, we try to send the email again.
#golang #Java #SpringBoot #go #docker #rabbitmq
Important links:
M1 GitHub: link
M2 GitHub: link
Firstly install Go and Docker in your machine then install the following:
This is an example of how to list things you need to use the software and how to install them.
- docker compose
docker-compose up
- Clone the repo
git clone https://github.com/rdurelli/save-user-microservice-m1.git
- Install go mod
go get -u github.com/spf13/cobra
go get -u github.com/gin-gonic/gin
go get github.com/spf13/viper
go get -u github.com/go-sql-driver/mysql
go get -u github.com/jasonlvhit/gocron
Install all dependencies listed before
go run main.go
As can be seen we have available four commands: (i) completion, (ii) help, (iii) queue, and (iv) server. The first one is generated automatically by COBRA CLI. Command help show how to use the app. Command queue and server are the important ones. If I run:
go run main.go queue --help
As can be noted we can specify some flags like address to connect the AMQP (RabbitMQ), SMTP Port etc. If none information is provided we have default values.
go run main.go queue
Calling the command queue will start the RabbitMQ
The next command is the server command. By running you can define the port to create the web-service, default is 8080
go run main.go server --help
To create the web-service just call the following command:
go run main.go server
Two end-point is created as depicted in figure: (i) GET /api/emails and (ii) GET /api/emails/showAll
The first end-point show all emails that have been failed to be sent. The second end-point list all emails.
Distributed under the MIT License. See LICENSE
for more information.
Rafael S. Durelli - @rafaeldurelli - rafael.durelli@ufla.br
Project Link: https://github.com/rdurelli/save-user-microservice-m1.git