A scalable microservices architecture showcasing service communication, database management, and containerization using NestJS, MySQL, and Docker.
Our microservices ecosystem consists of:
- Central entry point for all client requests
- Routes traffic to appropriate microservices
- Handles request/response transformation
- Swagger API documentation
- Load balancing and service discovery
- Manages user-related operations
- Features:
- User registration and authentication
- Profile management
- JWT token handling
- Dedicated MySQL instance (Port 3307)
- Handles all post-related operations
- Features:
- Create, read, update, delete posts
- Post metadata management
- Content validation
- Dedicated MySQL instance (Port 3308)
- Framework: NestJS with TypeScript
- Databases: MySQL 8.0
- Containerization: Docker & Docker Compose
- Documentation: Swagger/OpenAPI
- Testing: Jest & Supertest
- Node Version: 18.x
- Docker Desktop
- Node.js 18+
- npm 8+
- Clone the repository:
git clone <your-repo-url>
cd microservice-nest-js
- Set up environment variables:
Create .env
for User Service:
DB_HOST=user-db
DB_PORT=3306
DB_USERNAME=root
DB_PASSWORD=password
DB_DATABASE=user_service_db
Create .env
for Post Service:
DB_HOST=post-db
DB_PORT=3306
DB_USERNAME=root
DB_PASSWORD=password
DB_DATABASE=post_service_db
- Start the services:
# Build and start all services
docker-compose up -d --build
- Verify the deployment:
# Check running containers
docker ps
# Check service logs
docker-compose logs -f
- API Gateway: http://localhost:4000
- User Service: http://localhost:4001
- Post Service: http://localhost:4002
# Install dependencies for each service
cd api-gateway && npm install
cd ../user-service && npm install
cd ../post-service && npm install
# Run services in development mode
npm run start:dev
# Start services
docker-compose up -d
# Rebuild a specific service
docker-compose up -d --build [service-name]
# View logs
docker-compose logs -f [service-name]
# Stop all services
docker-compose down
# Clean up volumes
docker-compose down -v
Access Swagger documentation at:
- Gateway API: http://localhost:4000/api
- User Service: http://localhost:4001/api
- Post Service: http://localhost:4002/api
Run tests for each service:
# Unit tests
npm test
# E2E tests
npm run test:e2e
# Test coverage
npm run test:cov
-
Database Connection Failures
- Check if MySQL containers are healthy
- Verify environment variables
- Ensure ports are not in use
-
Service Discovery Issues
- Check if all services are running
- Verify network connectivity
- Check service names in docker-compose.yml
-
Build Failures
- Clear Docker cache:
docker builder prune
- Remove node_modules:
rm -rf node_modules
- Rebuild:
docker-compose up -d --build
- Clear Docker cache:
- Fork the repository
- Create your feature branch:
git checkout -b feature/amazing-feature
- Commit your changes:
git commit -m 'Add amazing feature'
- Push to the branch:
git push origin feature/amazing-feature
- Submit a pull request
This project is licensed under the MIT License - see the LICENSE file for details.