Hệ thống microservices bao gồm:
- User Service (Port 8001): Quản lý người dùng
- Product Service (Port 8002): Quản lý sản phẩm
- Order Service (Port 8003): Quản lý đơn hàng
- API Gateway (Port 8000): Điểm truy cập duy nhất cho client
# Kiểm tra Docker đã cài đặt
docker --version
docker-compose --version# Build tất cả services
docker-compose build
# Chạy tất cả services
docker-compose up -d
# Xem logs
docker-compose logs -f
# Kiểm tra trạng thái
docker-compose ps# API Gateway
curl http://localhost:8000/
# Health check
curl http://localhost:8000/health# Dừng tất cả
docker-compose down
# Dừng và xóa volumes (xóa dữ liệu)
docker-compose down -v# User Service
cd user-service
pip install -r requirements.txt
# Product Service
cd ../product-service
pip install -r requirements.txt
# Order Service
cd ../order-service
pip install -r requirements.txt
# API Gateway
cd ../api-gateway
pip install -r requirements.txtTerminal 1 - User Service:
cd user-service
uvicorn app.main:app --reload --port 8001Terminal 2 - Product Service:
cd product-service
uvicorn app.main:app --reload --port 8002Terminal 3 - Order Service:
cd order-service
export USER_SERVICE_URL=http://localhost:8001
export PRODUCT_SERVICE_URL=http://localhost:8002
uvicorn app.main:app --reload --port 8003Terminal 4 - API Gateway:
cd api-gateway
export USER_SERVICE_URL=http://localhost:8001
export PRODUCT_SERVICE_URL=http://localhost:8002
export ORDER_SERVICE_URL=http://localhost:8003
uvicorn app.main:app --reload --port 8000python test_api.py1. Tạo User:
curl -X POST http://localhost:8000/api/users/ \
-H "Content-Type: application/json" \
-d '{
"username": "john_doe",
"email": "john@example.com",
"full_name": "John Doe",
"password": "secret123"
}'2. Login:
curl -X POST http://localhost:8000/api/users/login \
-H "Content-Type: application/json" \
-d '{
"username": "john_doe",
"password": "secret123"
}'3. Tạo Product:
curl -X POST http://localhost:8000/api/products/ \
-H "Content-Type: application/json" \
-d '{
"name": "Laptop Dell XPS 13",
"description": "Laptop cao cấp",
"price": 25000000,
"stock": 10,
"category": "Electronics"
}'4. Lấy danh sách Products:
curl http://localhost:8000/api/products/5. Tạo Order:
curl -X POST http://localhost:8000/api/orders/ \
-H "Content-Type: application/json" \
-d '{
"user_id": 1,
"items": [
{"product_id": 1, "quantity": 2}
]
}'6. Lấy Orders của User:
curl "http://localhost:8000/api/orders/?user_id=1"Sau khi chạy services, truy cập Swagger UI:
- API Gateway: http://localhost:8000/docs
- User Service: http://localhost:8001/docs
- Product Service: http://localhost:8002/docs
- Order Service: http://localhost:8003/docs
┌─────────────┐
│ Client │
└──────┬──────┘
│
▼
┌─────────────────┐
│ API Gateway │ (Port 8000)
│ Routing Layer │
└────────┬────────┘
│
┌────┴────┬────────┬────────┐
▼ ▼ ▼ ▼
┌────────┐ ┌────────┐ ┌────────┐
│ User │ │Product │ │ Order │
│Service │ │Service │ │Service │
│ :8001 │ │ :8002 │ │ :8003 │
└───┬────┘ └───┬────┘ └───┬────┘
│ │ │
└──────────┴──────────┘
│
┌─────▼──────┐
│ PostgreSQL │
│ Redis │
└────────────┘
# Tất cả services
docker-compose logs -f
# Một service cụ thể
docker-compose logs -f user-service
docker-compose logs -f product-service
docker-compose logs -f order-service
docker-compose logs -f api-gateway-
Authentication & Authorization
- JWT tokens
- OAuth2
- Role-based access control
-
Message Queue
- RabbitMQ hoặc Kafka
- Event-driven architecture
- Async communication giữa services
-
Caching
- Redis caching
- Response caching
- Database query caching
-
Service Discovery
- Consul
- Eureka
-
Load Balancing
- Nginx
- HAProxy
-
Monitoring
- Prometheus + Grafana
- ELK Stack (Elasticsearch, Logstash, Kibana)
-
Circuit Breaker Pattern
- Resilient communication
- Fallback mechanisms
# Tìm và kill process đang dùng port
lsof -i :8000 # Mac/Linux
netstat -ano | findstr :8000 # Windows# Kiểm tra network
docker-compose exec user-service ping product-servicedocker-compose down -v
docker-compose up -dBạn có thể mở rộng hệ thống bằng cách:
- Thêm services mới (Payment, Notification, etc.)
- Implement API versioning
- Thêm unit tests và integration tests
- Setup CI/CD pipeline
- Deploy lên cloud (AWS, GCP, Azure)
Happy Coding! 🚀