Chuyển đến nội dung chính

第 3 課:系統架構概述 — 微服務、事件驅動與 DDD

高階系統架構、有界上下文(設計、目錄、訂單、生產、履行、分析)、事件驅動架構、CQRS、技術堆疊選擇、C4 圖。

🏗️ 建築 — 第 3 課 第 3 課:系統架構概述 — 微服務、事件驅動和 DDD

時裝設計與按需印刷系統架構-從領域分析到生產

第 1 部分:時裝設計領域和 POD 概述

亞洲開發網

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
活動總線阿帕契·卡夫卡高吞吐量、事件溯源
主資料庫PostgreSQLACID、JSONB、全文搜索
快取Redis集群會話、產品快取、速率限制
搜尋Elasticsearch/美麗搜索產品搜尋、分面
物件儲存S3/MinIO設計文件、模型、列印文件
CDNCloudFront/Cloudflare影像傳送、邊緣緩存
任務佇列BullMQ(Redis)影像處理、電子郵件、同步
GPU推理NVIDIA Triton/vLLMAI設計生成,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