要成功构建微服务,需要通过成熟的模式来解决分布式系统的挑战——在服务通信、数据管理、弹性和可观测性方面。这些模式有助于管理微服务固有的复杂性。
通信和API模式
✓ API GATEWAY → single entry point; centralizes cross-cutting concerns (routing, auth, etc.)
✓ SERVICE DISCOVERY → services find each other dynamically (registry) as instances change
✓ Sync (REST/gRPC) for request/response; ASYNC (events/queues) for decoupling → prefer
async/events to reduce coupling where possible
✓ BFF (Backend for Frontend) → tailored gateways per client type
数据管理模式
✓ DATABASE PER SERVICE → each service owns its data (no shared database) → independence,
but data is distributed (no cross-service joins/transactions)
✓ SAGA → manage transactions ACROSS services without distributed transactions:
→ a sequence of local transactions + compensating actions on failure (eventual consistency)
✓ EVENT SOURCING / CQRS → event-based state and separate read/write models
✓ Eventual consistency across services (embrace it; avoid distributed transactions)
