Skip to content

rdurelli/consumer-microservice-m2

Repository files navigation

Microservice M2

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.

Table of Contents

  1. About The Project
  2. Getting Started
  3. Usage
  4. Contributing
  5. License
  6. Contact
  7. Acknowledgements

About The Project

alt text 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

Built With

Getting Started

Firstly install Go and Docker in your machine then install the following:

Prerequisites

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 

Installation

  1. Clone the repo
    git clone https://github.com/rdurelli/save-user-microservice-m1.git
  2. 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

Usage

Install all dependencies listed before

go run main.go

alt text

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

alt text

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 alt text

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 

alt text

To create the web-service just call the following command:

  go run main.go server 

alt text

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.

License

Distributed under the MIT License. See LICENSE for more information.

Contact

Rafael S. Durelli - @rafaeldurelli - rafael.durelli@ufla.br

Project Link: https://github.com/rdurelli/save-user-microservice-m1.git

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published