Files
pos-system/docs/vi/architecture/service-communication.md
Ho Ngoc Hai 4da46b5b8e Sure! Pl
2025-12-27 01:31:10 +07:00

1.3 KiB

Giao Tiếp Giữa Các Service

Các Mẫu Giao Tiếp

Giao Tiếp Đồng Bộ (HTTP/REST)

Các service giao tiếp đồng bộ qua HTTP REST APIs thông qua Traefik API Gateway.

Ví dụ:

// Web App -> Auth Service
const response = await fetch('http://api.goodgo.vn/api/v1/auth/login', {
  method: 'POST',
  body: JSON.stringify({ email, password }),
});

Giao Tiếp Service-to-Service

Các service có thể giao tiếp trực tiếp qua mạng nội bộ:

// Auth Service -> Notification Service (tương lai)
const response = await fetch('http://notification-service:5003/api/v1/notifications', {
  method: 'POST',
  headers: { 'X-Service-Auth': process.env.INTERNAL_API_KEY },
  body: JSON.stringify({ userId, message }),
});

API Gateway Routing

Traefik định tuyến requests dựa trên:

  • Host header (api.goodgo.vn)
  • Path prefix (/api/v1/auth)

Xử Lý Lỗi

Tất cả services tuân theo định dạng error response nhất quán:

{
  "success": false,
  "error": {
    "code": "AUTH_001",
    "message": "Invalid credentials",
    "details": {}
  },
  "timestamp": "2024-01-01T00:00:00.000Z"
}

Retry và Circuit Breaker

Triển khai trong tương lai:

  • Exponential backoff cho retries
  • Circuit breaker pattern cho fault tolerance
  • Fallback mechanisms