A modern, scalable microservice system with API gateway, service discovery, and real-time monitoring dashboard built with Node.js and Express.
- Interactive Dashboard: Real-time monitoring of service health and metrics
- API Gateway: Centralized entry point with rate limiting and request routing
- Service Discovery: Automatic service registration and health checks using Consul
- Load Balancing: Intelligent request distribution across service instances
- Metrics & Monitoring: Prometheus metrics collection and visualization
- Mock Data Support: Fallback to mock data when services are unavailable
- Swagger Documentation: Interactive API documentation
- Microservices:
- User Service: User management and authentication
- Product Service: Product catalog and inventory
- Order Service: Order processing and management
- Backend: Node.js, Express
- Service Discovery: Consul
- Metrics: Prometheus
- Documentation: Swagger/OpenAPI
- Database: MongoDB
- Communication: gRPC
- Containerization: Docker
- Orchestration: Kubernetes
.
├── api-gateway/
│ ├── middleware/
│ │ └── metrics.js
│ ├── public/
│ │ ├── index.html
│ │ ├── scripts.js
│ │ └── styles.css
│ ├── routes/
│ │ └── swagger.js
│ └── server.js
├── services/
│ ├── user-service/
│ │ ├── models/
│ │ │ └── user.js
│ │ └── server.js
│ ├── product-service/
│ │ ├── models/
│ │ │ └── product.js
│ │ └── server.js
│ └── order-service/
│ ├── models/
│ │ └── order.js
│ └── server.js
├── protos/
│ ├── user.proto
│ ├── product.proto
│ └── order.proto
├── kubernetes/
│ ├── api-gateway-deployment.yaml
│ ├── user-service-deployment.yaml
│ ├── product-service-deployment.yaml
│ ├── order-service-deployment.yaml
│ ├── consul-deployment.yaml
│ └── mongodb-deployment.yaml
├── scripts/
│ └── start-all.js
├── docker-compose.yml
├── Dockerfile
└── package.json
- Node.js (v18 or later)
- MongoDB
- Docker and Docker Compose (for containerized deployment)
- Kubernetes (for production deployment)
-
Clone the repository:
git clone <repository-url> cd microservice-architecture
-
Install dependencies:
npm install
-
Set up environment variables: Create a
.env
file in the root directory with the following content:# API Gateway API_GATEWAY_PORT=3000 # Service Discovery CONSUL_HOST=localhost CONSUL_PORT=8500 # User Service USER_SERVICE_PORT=3001 USER_SERVICE_HOST=localhost # Product Service PRODUCT_SERVICE_PORT=3002 PRODUCT_SERVICE_HOST=localhost # Order Service ORDER_SERVICE_PORT=3003 ORDER_SERVICE_HOST=localhost # MongoDB MONGODB_URI=mongodb://localhost:27017/microservices
-
Start all services:
npm run start:all
Or start individual services:
npm run start # API Gateway npm run start:user # User Service npm run start:product # Product Service npm run start:order # Order Service
-
Access the services:
- Dashboard: http://localhost:3000
- API Documentation: http://localhost:3000/api-docs
- Metrics: http://localhost:3000/metrics
- Build and run using Docker Compose:
docker-compose up --build
- Apply Kubernetes configurations:
kubectl apply -f kubernetes/
GET /api/users
- List all usersGET /api/users/:id
- Get user by IDPOST /api/users
- Create new userPUT /api/users/:id
- Update userDELETE /api/users/:id
- Delete user
GET /api/products
- List all productsGET /api/products/:id
- Get product by IDPOST /api/products
- Create new productPUT /api/products/:id
- Update productDELETE /api/products/:id
- Delete product
GET /api/orders
- List all ordersGET /api/orders/:id
- Get order by IDPOST /api/orders
- Create new orderPUT /api/orders/:id
- Update order statusDELETE /api/orders/:id
- Delete order
- System Status: Real-time monitoring of API Gateway and service health
- Service Health: Individual service status and uptime tracking
- Request Distribution: Visual representation of API request distribution
- Response Times: Service response time monitoring
- Interactive API Testing: Built-in API testing interface for all endpoints
┌─────────────┐
│ Clients │
│ Dashboard │
└──────┬──────┘
│
▼
┌──────────────┐ ┌─────────────┐
│ │ │ │
│ API Gateway │◄───►│ Consul │
│ │ │ │
└──┬─────┬─────┘ └─────────────┘
│ │ ▲
│ │ │
▼ ▼ │
┌─────┐ ┌─────┐ ┌─────┐
│User │ │Prod.│ │Order│
│Serv.│ │Serv.│ │Serv.│
└──┬──┘ └──┬──┘ └──┬──┘
│ │ │
└───────┼───────┘
▼
┌───────────┐
│ │
│ MongoDB │
│ │
└───────────┘
The system collects various metrics using Prometheus:
- Request duration
- Request counts by endpoint
- Service response times
- Error rates
- System resource usage
The system implements comprehensive error handling:
- Service unavailability fallback to mock data
- Rate limiting protection
- Request timeout handling
- Graceful service degradation
- Detailed error logging
- Rate limiting on all API endpoints
- Request validation
- Error message sanitization
- Secure service communication
- Environment variable configuration
- 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
) - Open a Pull Request
This project is licensed under the MIT License - see the LICENSE file for details.