This repository contains two Node.js microservices that communicate asynchronously via RabbitMQ.
- Microservice A (Sender): Exposes an API endpoint (
/send) to accept messages and publish them to a RabbitMQ queue. - Microservice B (Receiver): Listens to the RabbitMQ queue and processes incoming messages.
- Asynchronous Messaging:
RabbitMQ is used to decouple the sender and receiver, which enhances scalability and fault tolerance. - Decoupling:
The services run independently and communicate solely via the message broker, making the system more modular.
Run the following command in the repository root:
docker-compose up -dThis starts RabbitMQ with the AMQP port on 5672 and the management UI on 15672.
- Navigate to the
microservice-adirectory:cd microservice-a - Install dependencies:
npm install
- Create a
.envfile (optional) with:RABBIT_URL=amqp://localhost PORT=3000 QUEUE=messageQueue - Start the service:
npm start
- Navigate to the
microservice-bdirectory:cd microservice-b - Install dependencies:
npm install
- Create a
.envfile (optional) with:RABBIT_URL=amqp://localhost QUEUE=messageQueue - Start the service:
npm start
From the microservice-a directory, run:
npm testThis executes unit tests for the /send endpoint.
From the microservice-b directory, run:
npm testThis runs tests to verify that incoming messages are correctly processed.
-
Reliable Asynchronous Messaging:
By ensuring persistent messaging and proper acknowledgment in RabbitMQ, the system can reliably process messages even under load. -
Decoupling Services:
Using RabbitMQ allows each service to operate independently, easing scalability and maintenance. -
Testing Asynchronous Code:
We mock the RabbitMQ connection and channels in tests to simulate message delivery without a live broker.
Any fool can write code that a computer can understand 🤖. Good programmers write code that humans can understand💡
Happy coding🚀