- Publish Messages: Accepts POST requests to publish messages to RabbitMQ exchanges/queues.
- Create Queues: Provides endpoints to create queues and bind them to exchanges.
- Test Messages: Includes a test endpoint for quick message publishing.
- Event Listener: Automatically listens to and processes test events.
- Publish Messages: Sends messages to the Centralized RabbitMQ Service via HTTP requests.
- Batch Publishing: Supports publishing multiple messages in a single request.
- Message Types: Supports events, commands, and notifications.
- Consume Messages: Listens to queues and processes messages from the Centralized RabbitMQ Service.
- Error Handling: Implements retry logic and dead-letter queues for failed messages.
- Custom Handlers: Allows adding custom message handlers for different message types.
- Producer (Fastify Microservice) sends a POST request to the Centralized RabbitMQ Service (NestJS).
- The Centralized Service publishes the message to RabbitMQ.
- The message is stored in a queue in RabbitMQ.
- The Consumer (Fastify Microservice) listens to the queue and processes the message.
- Centralized Management: All RabbitMQ operations are handled by a single service.
- Decoupling: Microservices are decoupled from RabbitMQ configuration.
- Scalability: The Centralized Service can be scaled independently.
- Consistency: Ensures consistent message formatting and routing rules.
- Node.js (v16 or higher)
- RabbitMQ (Docker recommended)
- Docker (optional, for RabbitMQ)
-
Clone the repository:
git clone https://github.com/noahwillson/centralized-rabbitmq-microservices.git cd centralized-rabbitmq-microservices -
Install dependencies for each service:
cd centralized-service && npm install cd ../rabbitmq-publisher && npm install cd ../rabbitmq-consumer && npm install
-
Start RabbitMQ using Docker:
docker run -d --name rabbitmq -p 5672:5672 -p 15672:15672 rabbitmq:3-management
-
Configure environment variables:
- Create
.envfiles in each service directory with the following content:RABBITMQ_URI=amqp://localhost:5672 PORT=3000 # For centralized-service PORT=3001 # For rabbitmq-consumer PORT=3002 # For rabbitmq-publisher
- Create
-
Start the Centralized RabbitMQ Service:
cd centralized-service npm run start -
Start the Consumer Microservice:
cd ../rabbitmq-consumer npm run start -
Start the Publisher Microservice:
cd ../rabbitmq-publisher npm run start
Use the Publisher Microservice to send a message:
curl -X POST http://localhost:3002/events \
-H "Content-Type: application/json" \
-d '{
"type": "user.created",
"data": {
"userId": "123",
"username": "john_doe",
"email": "john@example.com"
}
}'Use the Centralized Service to create a queue:
curl -X POST http://localhost:3000/rabbitmq/create-queue \
-H "Content-Type: application/json" \
-d '{
"queueName": "user-events-queue",
"exchange": "events",
"routingKey": "user.#"
}'The Consumer Microservice automatically listens to the queue and processes messages. Check the logs to see the processed messages.
curl -X POST http://localhost:3000/rabbitmq/create-queue \
-H "Content-Type: application/json" \
-d '{
"queueName": "user-events-queue",
"exchange": "events",
"routingKey": "user.#"
}'curl -X POST http://localhost:3002/events \
-H "Content-Type: application/json" \
-d '{
"type": "user.created",
"data": {
"userId": "123",
"username": "john_doe",
"email": "john@example.com"
}
}'The Consumer Microservice processes the message and logs it:
Received user event: {
type: 'user.created',
data: {
userId: '123',
username: 'john_doe',
email: 'john@example.com'
},
timestamp: '2023-05-15T12:34:56.789Z',
id: 'abcdef123456'
}
Processing user event: {...}
Contributions are welcome! Please follow these steps:
- Fork the repository.
- Create a new branch (
git checkout -b feature/YourFeatureName). - Commit your changes (
git commit -m 'Add some feature'). - Push to the branch (
git push origin feature/YourFeatureName). - Open a pull request.
This project is licensed under the MIT License. See the LICENSE file for details.