This repository contains an example of building microservices using NestJS, RabbitMQ, and integrating multiple databases such as MySQL and MongoDB.
- Node.js
- Docker & Docker Compose
-
Clone the repo
git clone https://github.com/wardvisual/microservices-nestjs.git
-
Start the databases and RabbitMQ
docker-compose up -d
-
Install the dependencies
cd admin && npm install
cd user && npm install
-
Create
.envfile inadmindirectoryMYSQL_DB_PORT=3307 MYSQL_DB_USERNAME=root MYSQL_DB_PASSWORD=password MYSQL_DB_NAME=mn_admin_db AMQP_URL=amqp://guest:guest@localhost:5672
-
Create
.envfile inuserdirectoryMONGO_URI=mongodb://root:password@localhost:3308/admin AMQP_URL=amqp://guest:guest@localhost:5672
-
Start the admin service (runs on port 8000)
cd admin && npm run start:dev
-
Start the user service (runs on port 8001)
cd user && npm run start:dev
-
Start the user message listener
cd user && npm run listen
- Admin Service: REST API connected to MySQL database. Emits events to RabbitMQ when products are created, updated, or deleted.
- User Service: REST API connected to MongoDB. Listens for events from RabbitMQ and syncs product data.
- RabbitMQ: Message broker for communication between services using
user_queue.
Admin Service (http://localhost:8000/api)
| Method | Endpoint | Description |
|---|---|---|
| GET | /products | Get all products |
| GET | /products/:id | Get product by id |
| POST | /products | Create a product |
| PATCH | /products/:id | Update a product |
| DELETE | /products/:id | Delete a product |
| PATCH | /products/:id/like | Like a product |
User Service (http://localhost:8001/api)
| Method | Endpoint | Description |
|---|---|---|
| GET | /products | Get all products |
| PATCH | /products/:id/like | Like a product |
| Service | Port | Description |
|---|---|---|
| MySQL | 3307 | Admin database |
| MongoDB | 3308 | User database |
| RabbitMQ | 5672 | Message broker |
| RabbitMQ | 15672 | Management UI |
Distributed under the MIT License. See LICENSE for more information.
Edward Fernandez: Wardvisual