Hệ thống microservice cho ứng dụng thương mại điện tử được xây dựng với Node.js, TypeScript và các công nghệ hiện đại bao gồm Kafka, MongoDB, và Elasticsearch.
- Kiến trúc hệ thống
- Công nghệ sử dụng
- Cấu trúc dự án
- Hướng dẫn cài đặt
- Chạy ứng dụng
- Services
- API Documentation
- Monitoring & Logging
- Development Workflow
- Troubleshooting
┌─────────────────┐ ┌─────────────────┐ ┌─────────────────┐
│ Client Apps │ │ Load Balancer │ │ API Gateway │
│ │───▶│ │───▶│ (Port 3000) │
│ Web/Mobile/etc │ │ │ │ │
└─────────────────┘ └─────────────────┘ └─────────────────┘
│
┌───────────────────────────────────────┴───────────────────────────────────────┐
│ │
▼ ▼ ▼
┌─────────────────┐ ┌─────────────────┐ ┌─────────────────┐
│ Auth Service │ │ User Service │ │ Catalog Service │
│ (Port 3001) │ │ (Port 3002) │ │ (Port 3003) │
└─────────────────┘ └─────────────────┘ └─────────────────┘
│ │ │
└───────────────────────────────┼───────────────────────────────┘
│
▼
┌─────────────────┐
│ Message Broker │
│ Apache Kafka │
│ (Port 9092) │
└─────────────────┘
│
┌───────────────────────────────┼───────────────────────────────┐
│ │ │
▼ ▼ ▼
┌─────────────────┐ ┌─────────────────┐ ┌─────────────────┐
│ MongoDB │ │ Elasticsearch │ │ Kibana │
│ (Port 27017) │ │ (Port 9200) │ │ (Port 5601) │
└─────────────────┘ └─────────────────┘ └─────────────────┘
- Node.js - JavaScript runtime
- Express.js - Web framework
- TypeScript - Type-safe JavaScript
- MongoDB - Document database
- Elasticsearch - Search engine
- Mongoose - MongoDB ODM
- Apache Kafka - Event streaming platform
- KafkaJS - Kafka client for Node.js
- Kibana - Data visualization
- Kafka UI - Kafka management interface
- Winston - Logging library
- Docker & Docker Compose - Containerization
- Prettier - Code formatting
- ESLint - Code linting
- Husky - Git hooks
- Commitlint - Commit message linting
ecommerce-microservice/
├── api-gateway/ # API Gateway - Route requests to services
│ ├── src/
│ │ ├── config/ # Configuration files
│ │ ├── middleware/ # Custom middleware
│ │ ├── proxies/ # Service proxy configurations
│ │ ├── routes/ # API routes
│ │ └── utils/ # Utility functions
│ ├── dockerfile # Docker configuration
│ └── package.json
├── auth-service/ # Authentication & Authorization Service
│ ├── src/
│ │ ├── controllers/ # Request handlers
│ │ ├── models/ # Database models
│ │ ├── services/ # Business logic
│ │ ├── kafka/ # Kafka producers/consumers
│ │ ├── routes/ # API routes
│ │ └── utils/ # Utility functions
│ ├── dockerfile
│ └── package.json
├── user-service/ # User Management Service
│ ├── src/
│ │ ├── controllers/ # Request handlers
│ │ ├── models/ # Database models
│ │ ├── services/ # Business logic
│ │ ├── kafka/ # Kafka producers/consumers
│ │ └── routes/ # API routes
│ ├── dockerfile
│ └── package.json
├── catalog-service/ # Product Catalog Service
│ ├── src/
│ │ ├── controllers/ # Request handlers
│ │ ├── models/ # Database models
│ │ ├── services/ # Business logic
│ │ ├── kafka/ # Kafka producers/consumers
│ │ ├── utils/ # Utility functions (including Elasticsearch)
│ │ └── routes/ # API routes
│ ├── dockerfile
│ └── package.json
├── docker-compose.yml # Docker services configuration
├── package.json # Root package configuration
├── .prettierrc # Prettier configuration
├── .eslintrc.js # ESLint configuration
├── commitlint.config.js # Commit message rules
└── tsconfig.base.json # Shared TypeScript configuration
- Node.js >= 18.0.0
- Docker >= 20.0.0
- Docker Compose >= 2.0.0
- Git
-
Clone repository:
git clone https://github.com/hnagnurtme/ecomerce-microservice.git cd ecomerce-microservice
-
Cài đặt dependencies:
npm install
-
Cài đặt dependencies cho từng service:
# Cài đặt cho tất cả services npm run install:all # Hoặc cài đặt riêng cho từng service cd auth-service && npm install cd ../user-service && npm install cd ../catalog-service && npm install cd ../api-gateway && npm install
# Khởi động tất cả services
docker-compose up -d
# Khởi động và theo dõi logs
docker-compose up
# Khởi động service cụ thể
docker-compose up auth-service user-service
# Dừng tất cả services
docker-compose down
# Dừng và xóa volumes
docker-compose down -v
# Auth Service
cd auth-service
npm run dev
# User Service
cd user-service
npm run dev
# Catalog Service
cd catalog-service
npm run dev
# API Gateway
cd api-gateway
npm run dev
- Chức năng: Routing requests, Authentication middleware, Rate limiting
- Endpoint:
http://localhost:3000
- Health Check:
GET /health
- Chức năng: User authentication, JWT token management, User registration/login
- Database: MongoDB (auth-database)
- Kafka Topics: User events
- Endpoints:
POST /auth/register
- User registrationPOST /auth/login
- User loginPOST /auth/refresh
- Refresh tokenGET /auth/profile
- Get user profile
- Chức năng: User profile management, User data operations
- Database: MongoDB (user-database)
- Kafka Topics: User profile events
- Endpoints:
GET /users/profile
- Get user profilePUT /users/profile
- Update user profileDELETE /users/profile
- Delete user profile
- Chức năng: Product catalog management, Search functionality
- Database: MongoDB (catalog-database)
- Search Engine: Elasticsearch
- Kafka Topics: Product events
- Endpoints:
GET /products
- Get products listPOST /products
- Create productGET /products/:id
- Get product detailsPUT /products/:id
- Update productDELETE /products/:id
- Delete productGET /products/search
- Search products
- Database: Document database cho tất cả services
- Credentials: root/secret
- Databases: auth-database, user-database, catalog-database
- Message Broker: Event streaming giữa các services
- Zookeeper: Service coordination
- Kafka UI:
http://localhost:8080
- Search Engine: Full-text search cho catalog service
- Version: 8.13.0
- Endpoint:
http://localhost:9200
- Visualization: Data visualization cho Elasticsearch
- Dashboard:
http://localhost:5601
1. User Registration/Login → Auth Service
2. JWT Token Generation → Auth Service
3. Token Validation → API Gateway Middleware
4. Request Routing → Target Service
1. User Registration → Auth Service → Kafka → User Service
2. Profile Updates → User Service → Kafka → Auth Service
3. Product Events → Catalog Service → Kafka → Search Indexing
- Application logs: Winston logging trong từng service
- Container logs:
docker-compose logs [service-name]
- Real-time logs:
docker-compose logs -f [service-name]
- MongoDB: Connection ping
- Kafka: Topic listing
- Elasticsearch: Cluster health
- Kafka UI:
http://localhost:8080
- Kibana:
http://localhost:5601
- Elasticsearch:
http://localhost:9200/_cluster/health
# Lint tất cả code
npm run lint
# Fix linting issues
npm run lint:fix
# Format code
npm run format
# Type checking
npm run type-check
-
Tạo branch mới:
git checkout -b feature/your-feature-name
-
Development:
# Chạy services cần thiết docker-compose up -d mongodb kafka elasticsearch # Chạy service đang phát triển cd [service-name] npm run dev
-
Commit changes:
git add . npm run commit # Interactive commit với Commitizen
-
Push và tạo Pull Request:
git push origin feature/your-feature-name
Dự án sử dụng Conventional Commits:
<type>(<scope>): <description>
[optional body]
[optional footer]
Types:
feat
: Tính năng mớifix
: Sửa lỗidocs
: Cập nhật tài liệustyle
: Formatting, missing semi colons, etcrefactor
: Refactoring codetest
: Adding testschore
: Updating build tasks, packages, etc
Examples:
feat(auth): add user registration endpoint
fix(catalog): resolve elasticsearch connection issue
docs: update API documentation
refactor(user): improve user service architecture
# Kiểm tra logs
docker-compose logs [service-name]
# Restart service
docker-compose restart [service-name]
# Rebuild container
docker-compose build [service-name]
# Kiểm tra MongoDB health
docker-compose exec mongodb mongosh --eval "db.adminCommand('ping')"
# Restart MongoDB
docker-compose restart mongodb
# Kiểm tra Kafka health
docker-compose exec kafka kafka-topics --bootstrap-server localhost:9092 --list
# Restart Kafka cluster
docker-compose restart zookeeper kafka
# Kiểm tra Elasticsearch health
curl http://localhost:9200/_cluster/health
# Restart Elasticsearch
docker-compose restart elasticsearch
# Kiểm tra ports đang sử dụng
lsof -i :3000 # hoặc port khác
netstat -tlnp | grep :3000
# Dừng service sử dụng port
docker-compose down
# Xóa unused containers và images
docker system prune -a
# Monitoring resource usage
docker stats
# MongoDB performance monitoring
docker-compose exec mongodb mongostat
- Hot reload: Sử dụng
npm run dev
để auto-restart khi có thay đổi - Debug: Sử dụng VS Code debugger với attach to container
- Testing: Chạy integration tests với Docker Compose
- Environment: Sử dụng
.env
files cho từng service
ISC
Developed by: Your Team Name
Version: 1.0.0
Last Updated: July 2025