技术架构与部署Architecture & Deployment
架构目标只有三个:业务可独立演进、资金绝对可靠、市场可快速复制。因此采用「多端同源 + 领域服务 + 事件驱动 + 复式记账 + 市场配置化」,而不是一次性堆砌微服务。Three goals: independently evolvable domains, absolutely reliable money, and fast market replication. The answer is a shared client codebase, domain services, event-driven integration, double-entry ledger and market configuration.
4.1 总体架构4.1 System overview
flowchart TB
subgraph CLIENT["客户端 Clients"]
APPS["用户 / 司机 / 骑手 / 商户 / 股东 App"]
ADMIN["管理后台 Web"]
end
subgraph EDGE["接入层 Edge"]
GW["API Gateway
鉴权 · 限流 · 灰度"]
WS["长连接网关
IM · 轨迹 · 派单"]
end
subgraph DOMAIN["领域服务 Domain"]
ID["账户认证"]
ORD["订单中心"]
DSP["调度派单"]
PAY["支付网关"]
LED["结算分账"]
end
subgraph DATA["数据层 Data"]
PG[("PostgreSQL")]
RD[("Redis")]
MQ[("Kafka")]
CH[("ClickHouse")]
end
subgraph EXT["外部服务 Third party"]
ACQ["持牌收单 / 托管"]
MAP["Google Maps"]
KYC["KYC / 合规"]
end
APPS --> GW
APPS --> WS
ADMIN --> GW
GW --> ID
GW --> ORD
WS --> DSP
ORD --> DSP
ORD --> PAY
PAY --> LED
ORD --> MQ
MQ --> CH
LED --> PG
LED --> RD
DSP --> RD
PAY --> ACQ
DSP --> MAP
ID --> KYC
4.2 技术拓扑架构4.2 Technical topology architecture
基础设施Data & InfraLayer 4
→
→
→
→
4.3 技术选型4.3 Technology stack
| 层 Layer | 选型 Choice | 理由 Rationale |
|---|---|---|
| 客户端Clients | Flutter 五端同源 + React + TypeScript 管理后台Flutter for five apps, React + TypeScript for admin | 一套业务代码覆盖 iOS / Android,降低六端维护成本;后台沿用 Web 生态便于表格与图表One codebase for iOS and Android; the admin uses the web ecosystem for tables and charts |
| 接入层Edge | Kong / APISIX + 自研 BFFKong / APISIX with a custom BFF | 鉴权、限流、灰度与路由集中;BFF 做端侧字段裁剪,减少移动端流量Centralized auth, rate limiting, canary and routing; BFF trims payloads for mobile |
| 领域服务Services | Java 21 + Spring Boot 3;Go 用于网关与轨迹Java 21 + Spring Boot 3; Go for gateway and tracking | 资金与订单强事务用 Java 生态成熟;高并发长连接用 Go 更省资源Mature Java ecosystem for transactional orders and money; Go is leaner for high-concurrency connections |
| 数据Data | PostgreSQL 16 + PostGIS、Redis 7、Kafka 3、ClickHouse、S3PostgreSQL 16 + PostGIS, Redis 7, Kafka 3, ClickHouse, S3 | 强一致事务、地理检索、缓存与派单队列、事件回放、分析聚合与对象存储Strong transactions, geospatial search, caching and dispatch queues, event replay, analytics and object storage |
| 搜索Search | OpenSearchOpenSearch | 门店 / 商品 / POI 检索,支持中文分词与多语言同义词Store, product and POI search with Chinese tokenization and multilingual synonyms |
| 实时通信Realtime | WebSocket 集群;MQTT 为车载设备预留WebSocket cluster; MQTT reserved for in-vehicle devices | IM、派单推送与轨迹上报;MQTT 为硬件接入预留IM, dispatch push and tracking; MQTT reserved for hardware |
| 基础设施Infrastructure | Kubernetes + Terraform + Argo CD + GitHub ActionsKubernetes + Terraform + Argo CD + GitHub Actions | 多市场部署、声明式环境、可回滚发布与镜像扫描Multi-market deployment, declarative environments, rollback and image scanning |
| 可观测Observability | OpenTelemetry + Prometheus + Grafana + Loki + SentryOpenTelemetry + Prometheus + Grafana + Loki + Sentry | 统一 trace / metric / log,按订单号与请求号串联Unified traces, metrics and logs correlated by order and request ID |
4.4 服务拆分与职责边界4.4 Service decomposition
| 服务 Service | 核心职责 Responsibility | 数据所有权 Owns |
|---|---|---|
| identity | 注册登录、KYC、角色权限、设备与会话Sign-up, login, KYC, roles, devices and sessions | 用户、身份、角色、会话user, identity, role, session |
| order | 下单、状态机、统一订单与子单、售后Ordering, state machine, main/sub orders, after-sales | 主订单、业务子单、状态日志order_main, sub-orders, status log |
| dispatch | 派单评分、扩圈、抢单、司机在线状态、接单限制Scoring, expansion, claiming, driver online state, acceptance limits | 派单任务、在线状态、分配日志dispatch_task, driver_online, assign_log |
| fulfillment | 接驾 / 取餐 / 行程 / 送达状态、轨迹、ETA、偏航告警Pickup, food pickup, trip and delivery states, tracking, ETA, deviation alerts | 轨迹、配送任务、ETA 快照trip_track, delivery_task, eta_snapshot |
| catalog | 门店、商品、库存、菜单、营业状态、核销Stores, products, stock, menus, business status, redemption | 商户、门店、商品、核销记录merchant, store, product, redeem_record |
| payment | 收银台、渠道适配、支付单、退款、拒付Cashier, channel adapters, payment orders, refunds, chargebacks | 支付单、退款单、渠道流水payment_order, refund_order, channel_txn |
| ledger | 规则化分账、复式分录、余额、结算、提现Rule-based split, double entries, balances, settlement, payout | 规则、分账、分录、钱包、提现split_rule, order_split, ledger_entry, wallet, payout |
| admin | 审核、台账、营销配置、系统与多语言、报表Review, ledgers, marketing config, system and i18n, reports | 审核任务、后台账号、语言资源audit_task, admin_user, i18n_resource |
服务只写自己的库表,跨域读写一律通过 API 或事件;资金写操作必须幂等且带业务单号;订单状态变更只允许订单服务发起。Each service writes only its own tables; cross-domain access goes through APIs or events. Money writes are idempotent with business numbers. Only the order service mutates order state.
4.5 资金可靠性设计4.5 Money reliability
资金链路的正确性优先于任何性能优化。采用「支付单 → 分账明细 → 复式分录 → 余额快照」四层结构:支付单对接渠道,分账明细记录规则计算结果,分录保证借贷平衡,余额快照用日终快照 + 增量流水双重校验。Correctness outranks performance. The four layers are payment order, split detail, double entry and balance snapshot: channels feed payment orders, split details record rule output, entries keep debits and credits balanced, and snapshots are verified against incremental flows.
幂等Idempotency
所有资金接口以 biz_no 幂等;回调与主动查询双保险。All money APIs are idempotent by biz_no; callbacks and active queries provide dual assurance.
补偿Compensation
本地消息表 + Kafka 保证最终一致;失败重试,超阈值转人工。Outbox plus Kafka for eventual consistency; retries with manual escalation.
余额Balance
余额 = 可用 + 冻结;提现先冻结再出款,失败自动解冻。Balance = available + frozen; payout freezes first and unfreezes on failure.
审计Audit
分录只追加不修改,冲正以反向分录实现,保留操作人与来源单据。Entries are append-only; reversals are new entries; operator and source document are retained.
4.6 部署拓扑与环境4.6 Deployment topology and environments
采用「单区域多可用区起步,市场级隔离扩展」:首城部署在主区域(3 个可用区),数据库主从 + 只读副本;进入第二个国家时新增独立区域,仅共享代码与镜像,不共享用户数据,满足数据驻留要求。Start with a single region across three availability zones, then isolate by market. The first city uses a primary database with replicas; a second country gets its own region sharing only code and images, not user data, to satisfy residency requirements.
计算Compute
K8s 命名空间按域隔离;HPA 按 CPU / QPS / 队列长度扩缩;派单与支付独立资源池。Namespaces per domain; HPA on CPU, QPS and queue depth; dedicated pools for dispatch and payment.
数据Data
PostgreSQL 一主两从 + PITR;按城市分库预留;Kafka 三副本;对象存储跨区复制。Primary plus two replicas with PITR; city-based sharding reserved; three Kafka replicas; cross-region object storage.
网络与安全Network & security
WAF + 网关限流;服务间 mTLS;密钥托管 KMS;生产与办公网隔离。WAF and gateway rate limits; service-to-service mTLS; KMS-managed keys; production isolated from office networks.
| 环境 Environment | 用途 Purpose | 数据 Data | 发布方式 Release |
|---|---|---|---|
| dev | 开发联调Development and integration | 脱敏测试数据Masked test data | 合并自动部署Auto-deploy on merge |
| staging | 验收 / 压测 / UATAcceptance, load test, UAT | 生产同构、脱敏Production-like, masked | 按版本发布,需审批Versioned release with approval |
| production | 正式运营Live operations | 真实数据,加密与备份Real data, encrypted and backed up | 灰度 → 全量,可回滚Canary to full, rollback ready |
| sandbox | 支付 / 地图 / KYC 联调Payment, maps and KYC sandbox | 供应商沙箱Vendor sandbox | 按需On demand |
4.7 安全、合规与性能基线4.7 Security, compliance and performance
支付与隐私Payments & privacy
- 不落地卡号(PCI SAQ-A)、3DS、令牌化、拒付证据包No card data at rest (PCI SAQ-A), 3DS, tokenization, chargeback evidence
- GDPR / CCPA / PDPA:同意管理、DSAR、删除与导出GDPR / CCPA / PDPA: consent, DSAR, erasure and export
- 数据驻留与跨境传输评估Data residency and cross-border transfer assessment
应用与业务合规Application & business compliance
- OWASP Top 10、依赖扫描、渗透测试、密钥轮换OWASP Top 10, dependency scanning, penetration tests, key rotation
- 后台 RBAC + 最小权限 + 双因子 + 敏感操作审批RBAC, least privilege, MFA and approvals for sensitive actions
- 司机资质、保险、税务发票、消费者权益与反洗钱筛查Driver eligibility, insurance, tax invoices, consumer rights and AML screening
| 场景 Scenario | 目标 Target | 说明 Note |
|---|---|---|
| 接口 P95(读)API P95 (read) | < 200 ms | 门店列表、订单详情、钱包查询Store list, order detail, wallet query |
| 接口 P95(写)API P95 (write) | < 400 ms | 下单、支付回调、接单Ordering, payment callback, acceptance |
| 派单下发Dispatch push | < 3 s | 下单到司机端收到派单Order to driver offer received |
| 轨迹上报Tracking | 5 s / 次interval | 支持 1 秒加密1-second mode supported |
| 系统可用性Availability | 99.95% | 资金与订单链路,年停机 < 4.4 小时Money and order paths; under 4.4 hours downtime per year |
| 单城容量City capacity | 5,000 / 日day | 峰值按 10 倍冗余设计Designed for 10× peak |
4.8 多语言与多币种实现4.8 i18n and multi-currency
语言包Language packs
i18n_resource(key, locale, value, status);后台缺失词条看板、批量导入导出、发布版本与回滚;App 内置兜底文案防止空白。i18n_resource(key, locale, value, status); missing-key dashboard, bulk import/export, versioned publishing and rollback; built-in fallbacks prevent blanks.
金额与汇率Amounts & FX
金额统一 numeric(18,4) + currency;下单写 fx_rate_snapshot;分账与结算按订单币种,跨币种另记折算分录。All amounts numeric(18,4) plus currency; FX snapshot at order time; split and settlement in order currency, with separate conversion entries.
若进入阿拉伯语市场,布局需支持 RTL 镜像、数字与日期本地化、姓名与地址结构差异、宗教节日与营业时间差异。建议一期即预留 RTL 开关与双向文本组件。For Arabic markets, plan RTL mirroring, localized numerals and dates, different name/address structures and holiday-aware hours. Reserve an RTL switch and bidirectional text components in phase 1.