Microservices with NestJS: RabbitMQ, Redis, and Docker Compose Example
This project demonstrates how to work with microservices using NestJS, RabbitMQ, Redis, and Docker Compose. It consists of three NestJS containers, one for the Swagger API documentation and a simple endpoint to create tasks and send them to RabbitMQ. The service_a container contains a microservice that implements the command businessCase1 in an imperative style. The service_b container contains a microservice that implements the commands checkApproved, approve, getBalance, feed, sign, send. In the example, the app container calls the businessCase1 command, and businessCase1 calls commands (checkApproved, approve, getBalance, feed, sign, send) sequentially. Before invoking a subcommand, the script checks whether the result of that command's execution is present in the cache. This is done for fault tolerance. If a task fails to execute completely for some reason, it will be restarted. If the subtasks have already been executed, it skips their execution and retrieves the results from the cache. The cache is stored in Redis, and after a successful task execution, the cache is cleared. All containers are launched using Docker Compose.
- To run this project locally, please follow the instructions below:
git clone git@github.com:andresayak/nestjs-microservices-example.git
- Navigate to the project directory:
cd nestjs-microservices-example
- Create a .env file in the root directory of the project and set the following environment variables:
RABBITMQ_QUEUE_NAME=test-subscribers
RABBITMQ_USER=guest
RABBITMQ_PASS=guest
RABBITMQ_VHOST=test
- Start the Docker containers using Docker Compose:
docker-compose up
- Once the containers are up and running, you can access the Swagger API documentation at http://localhost. This endpoint allows you to create tasks and send them to RabbitMQ.
- The microservice with the implementation of the test1, test2, and test3 commands will be available for communication through RabbitMQ. You can interact with it programmatically or via the Swagger API.
This project is licensed under the MIT License.
Contributions are welcome! If you find any issues or want to enhance the project, feel free to open a pull request.
If you have any questions or suggestions, please feel free to reach out to the project maintainer: Thank you for using this project!
I hope it helps you understand microservices with NestJS, RabbitMQ, and Redis using Docker Compose.