https://github.com/wardvisual/microservices-nestjs
This repository contains an example of building microservices using NestJS, RabbitMQ, and integrating multiple databases such as MySQL and MongoDB.
https://github.com/wardvisual/microservices-nestjs
microservices nestjs nestjs-microservices rabbitmq
Last synced: about 1 month ago
JSON representation
This repository contains an example of building microservices using NestJS, RabbitMQ, and integrating multiple databases such as MySQL and MongoDB.
- Host: GitHub
- URL: https://github.com/wardvisual/microservices-nestjs
- Owner: wardvisual
- License: mit
- Created: 2023-01-14T15:09:25.000Z (over 3 years ago)
- Default Branch: main
- Last Pushed: 2024-07-20T12:42:03.000Z (almost 2 years ago)
- Last Synced: 2025-08-12T21:41:30.701Z (10 months ago)
- Topics: microservices, nestjs, nestjs-microservices, rabbitmq
- Language: TypeScript
- Homepage:
- Size: 219 KB
- Stars: 4
- Watchers: 1
- Forks: 2
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Microservices with NestJS and RabbitMQ
This repository contains an example of building microservices using NestJS, RabbitMQ, and integrating multiple databases such as MySQL and MongoDB.
## Prerequisites
- Node.js
- Docker & Docker Compose
## Installation
1. Clone the repo
```sh
git clone https://github.com/wardvisual/microservices-nestjs.git
```
2. Start the databases and RabbitMQ
```sh
docker-compose up -d
```
3. Install the dependencies
```sh
cd admin && npm install
```
```sh
cd user && npm install
```
4. Create `.env` file in `admin` directory
```env
MYSQL_DB_PORT=3307
MYSQL_DB_USERNAME=root
MYSQL_DB_PASSWORD=password
MYSQL_DB_NAME=mn_admin_db
AMQP_URL=amqp://guest:guest@localhost:5672
```
5. Create `.env` file in `user` directory
```env
MONGO_URI=mongodb://root:password@localhost:3308/admin
AMQP_URL=amqp://guest:guest@localhost:5672
```
## Running the Application
1. Start the admin service (runs on port 8000)
```sh
cd admin && npm run start:dev
```
2. Start the user service (runs on port 8001)
```sh
cd user && npm run start:dev
```
3. Start the user message listener
```sh
cd user && npm run listen
```
## Architecture
- **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`.
## API Endpoints
### 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 |
## Docker Services
| Service | Port | Description |
| -------- | ----- | -------------- |
| MySQL | 3307 | Admin database |
| MongoDB | 3308 | User database |
| RabbitMQ | 5672 | Message broker |
| RabbitMQ | 15672 | Management UI |
## License
Distributed under the MIT License. See `LICENSE` for more information.
## Contact
Edward Fernandez: [Wardvisual](https://wardvisual.me/)