1.DDD限界上下文
申請 領域驅動設計 將 POD 系統劃分為明確界定的上下文。
┌──────────────────────────────────────────────────────────────────────────┐
│ Fashion POD Platform │
│ │
│ ┌──────────────┐ ┌──────────────┐ ┌──────────────┐ │
│ │ Design │ │ Catalog │ │ Commerce │ │
│ │ Context │ │ Context │ │ Context │ │
│ │ │ │ │ │ │ │
│ │ • Canvas │ │ • Product │ │ • Cart │ │
│ │ • AI Gen │ │ • SKU │ │ • Checkout │ │
│ │ • Assets │ │ • Mockup │ │ • Payment │ │
│ │ • Templates │ │ • Channel │ │ • Pricing │ │
│ └──────┬───────┘ └──────┬───────┘ └──────┬───────┘ │
│ │ │ │ │
│ ▼ ▼ ▼ │
│ ┌────────────────────────────────────────────────┐ │
│ │ Event Bus (Kafka) │ │
│ └────────────────────────────────────────────────┘ │
│ │ │ │ │
│ ▼ ▼ ▼ │
│ ┌──────────────┐ ┌──────────────┐ ┌──────────────┐ │
│ │ Order │ │ Production │ │ Fulfillment │ │
│ │ Context │ │ Context │ │ Context │ │
│ │ │ │ │ │ │ │
│ │ • OMS │ │ • Print Queue│ │ • Shipping │ │
│ │ • Saga │ │ • Supplier │ │ • Tracking │ │
│ │ • Split │ │ • QC │ │ • Returns │ │
│ │ • Timeline │ │ • Routing │ │ • Logistics │ │
│ └──────────────┘ └──────────────┘ └──────────────┘ │
│ │
│ ┌──────────────┐ ┌──────────────┐ ┌──────────────┐ │
│ │ Identity & │ │ Analytics │ │ AI/ML │ │
│ │ Access │ │ Context │ │ Platform │ │
│ │ │ │ │ │ │ │
│ │ • Auth │ │ • Warehouse │ │ • Feature │ │
│ │ • RBAC │ │ • Dashboard │ │ Store │ │
│ │ • Teams │ │ • Reports │ │ • Training │ │
│ │ • API Keys │ │ • BI │ │ • Serving │ │
│ └──────────────┘ └──────────────┘ └──────────────┘ │
└──────────────────────────────────────────────────────────────────────────┘
2. 上下文圖——關係
| 上游 | 下游 | 關係 | 整合 |
|---|---|---|---|
| 設計 | 目錄 | 客戶-供應商 | 設計資產 → 產品圖片 |
| 目錄 | 商業 | 發布語言 | 產品資料 → 店面 |
| 商業 | 訂單 | 客戶-供應商 | 結帳 → 訂單已建立 |
| 訂單 | 生產 | 客戶-供應商 | 訂單 → 列印作業 |
| 生產 | 執行 | 客戶-供應商 | 印刷 → 出貨 |
| 所有上下文 | 分析 | 墨守成規者 | 事件 → 資料倉儲 |
| 人工智慧/機器學習 | 設計、目錄、生產 | 開放主機服務 | 機器學習推理 API |
| 頻道(Shopify/Etsy) | 目錄、訂單 | 反貪腐層 | 外部API→內部模型 |
3. 事件驅動架構
核心領域事件
// Design Context Events
interface DesignCreatedEvent {
type: 'design.created';
designId: string;
designerId: string;
imageUrl: string;
metadata: { width: number; height: number; dpi: number };
timestamp: string;
}
interface DesignApprovedEvent {
type: 'design.approved';
designId: string;
qcScore: number;
ipCheckResult: 'clean' | 'flagged';
}
// Catalog Context Events
interface ProductPublishedEvent {
type: 'product.published';
productId: string;
designId: string;
channels: string[]; // ['shopify', 'etsy', 'amazon']
variants: Array<{ sku: string; size: string; color: string; price: number }>;
}
// Order Context Events
interface OrderCreatedEvent {
type: 'order.created';
orderId: string;
customerId: string;
items: Array<{ sku: string; quantity: number; designId: string }>;
shippingAddress: Address;
totalAmount: number;
}
interface OrderSplitEvent {
type: 'order.split';
orderId: string;
subOrders: Array<{
subOrderId: string;
supplierId: string;
items: Array<{ sku: string; quantity: number }>;
}>;
}
// Production Context Events
interface PrintJobCompletedEvent {
type: 'print_job.completed';
subOrderId: string;
supplierId: string;
qcResult: 'pass' | 'fail';
completedAt: string;
}
// Fulfillment Context Events
interface ShipmentCreatedEvent {
type: 'shipment.created';
shipmentId: string;
orderId: string;
carrier: string;
trackingNumber: string;
estimatedDelivery: string;
}
事件流——訂單生命週期
Commerce Order Production Fulfillment
│ │ │ │
│ checkout.completed │ │ │
│─────────────────────────▶│ │ │
│ │ order.created │ │
│ │─────────────────────────▶│ │
│ │ order.split │ │
│ │─────────────────────────▶│ │
│ │ │ print_job.queued │
│ │ │──────────────────────▶│
│ │ │ print_job.completed │
│ │◀─────────────────────────│ │
│ │ │ │
│ │ sub_order.ready_to_ship │ │
│ │──────────────────────────────────────────────────▶│
│ │ │ shipment.created │
│ │◀─────────────────────────────────────────────────│
│ │ order.shipped │ │
│ │──▶ (notify customer) │ │
4.CQRS模式
// Command Side — Write path
interface CreateProductCommand {
designId: string;
baseProductIds: string[];
pricing: PricingConfig;
publishTo: string[];
}
// Query Side — Read path (denormalized for performance)
interface ProductReadModel {
id: string;
title: string;
designThumbnail: string;
mockupImages: string[];
variants: Array<{
sku: string;
size: string;
color: string;
price: number;
inStock: boolean;
}>;
rating: number;
reviewCount: number;
salesCount: number;
// Denormalized — no JOINs needed
}
5. 技術棧
| 圖層 | 科技 | 原因 |
|---|---|---|
| API網關 | Kong / AWS API網關 | 速率限制、身份驗證、路由 |
| 後端服務 | Node.js (NestJS) / Go | 高吞吐量,TypeScript 生態系統 |
| 人工智慧/機器學習服務 | Python(快速API) | ML生態系、GPU推理 |
| 前端 | Next.js / React | 用於 SEO 的 SSR、設計工作室 SPA |
| 活動總線 | 阿帕契·卡夫卡 | 高吞吐量、事件溯源 |
| 主資料庫 | PostgreSQL | ACID、JSONB、全文搜索 |
| 快取 | Redis集群 | 會話、產品快取、速率限制 |
| 搜尋 | Elasticsearch/美麗搜索 | 產品搜尋、分面 |
| 物件儲存 | S3/MinIO | 設計文件、模型、列印文件 |
| CDN | CloudFront/Cloudflare | 影像傳送、邊緣緩存 |
| 任務佇列 | BullMQ(Redis) | 影像處理、電子郵件、同步 |
| GPU推理 | NVIDIA Triton/vLLM | AI設計生成,CLIP |
| 編排 | Kubernetes(EKS/GKE) | 自動縮放、多區域 |
| 監控 | 普羅米修斯+格拉法納 | 指標、警報、儀表板 |
| 資料倉儲 | ClickHouse/BigQuery | 分析、報告 |
6. 高層架構圖
┌─────────────┐
│ CDN Edge │
│ (Cloudflare) │
└──────┬───────┘
│
┌──────▼───────┐
│ API Gateway │
│ (Kong) │
└──────┬───────┘
│
┌──────────────────┼──────────────────┐
▼ ▼ ▼
┌────────────────┐ ┌───────────────┐ ┌────────────────┐
│ Design Service │ │Catalog Service│ │Commerce Service│
│ (NestJS) │ │ (NestJS) │ │ (NestJS) │
└───────┬────────┘ └───────┬───────┘ └───────┬────────┘
│ │ │
▼ ▼ ▼
┌────────────────────────────────────────────────────┐
│ Kafka Cluster │
└────────────────────────┬───────────────────────────┘
┌───────────────┼───────────────┐
▼ ▼ ▼
┌────────────────┐ ┌──────────────┐ ┌────────────────┐
│ Order Service │ │ Production │ │ Fulfillment │
│ (NestJS) │ │ Service (Go) │ │ Service (NestJS│
└───────┬────────┘ └──────┬───────┘ └───────┬────────┘
│ │ │
▼ ▼ ▼
┌──────────────┐ ┌──────────────┐ ┌──────────────┐
│ PostgreSQL │ │ PostgreSQL │ │ PostgreSQL │
│ (Orders) │ │ (Production) │ │ (Shipping) │
└──────────────┘ └──────────────┘ └──────────────┘
┌────────────────┐ ┌──────────────┐ ┌────────────────┐
│ AI/ML Service │ │ Analytics │ │ Channel Sync │
│ (FastAPI+GPU) │ │ (ClickHouse) │ │ (Workers) │
└────────────────┘ └──────────────┘ └────────────────┘
7. 資料流模式
| 流量 | 圖案 | 延遲 |
|---|---|---|
| 設計→樣機 | 同步(即時預覽) | < 2秒 |
| 設計 → QC/IP 檢查 | 非同步(隊列) | 5-30秒 |
| 產品 → 通道同步 | 非同步(事件 → 工作執行緒) | 1-5分鐘 |
| 訂單→生產 | 非同步(saga 協調器) | 秒。秒 |
| 生產→品質控制 | 同步(內聯) | < 5秒 |
| 全部 → 分析 | 非同步(CDC→Kafka→DW) | 1-5分鐘 |
| 產品搜尋 | CQRS讀取模型 | <100毫秒 |
八、總結
9 有界上下文 — 設計、目錄、商務、訂單、生產、履行、身分、分析、AI/ML
事件驅動 — Kafka 事件匯流排,鬆散耦合的領域事件
連續QRS — 單獨的讀取/寫入效能(產品目錄、搜尋)
傳奇模式 — 訂單生命週期的分散式交易
技術堆疊 — NestJS + Go + FastAPI + PostgreSQL + Kafka + Redis + K8s