欢迎使用 FlowZap,快速、清晰、掌控的绘图应用。

事件驱动领域事件架构

Architecture

领域事件架构图,展示聚合根如何引发领域事件,这些事件既在进程内分发给本地处理器,也跨边界分发给其他限界上下文中的集成事件消费者。该模板模拟 DDD 事件模式,领域逻辑通过干净的事件分发器触发副作用,保持领域和基础设施关注点的分离。对于实施基于事件集成的领域驱动设计的团队至关重要。

完整 FlowZap 代码

Domain { # Domain Layer
n1: circle label:"Business Operation"
n2: rectangle label:"Aggregate Root"
n3: rectangle label:"Apply Domain Logic"
n4: rectangle label:"Raise Domain Event"
n5: rectangle label:"Commit Unit of Work"
n1.handle(right) -> n2.handle(left)
n2.handle(right) -> n3.handle(left) [label="State Transition"]
n3.handle(right) -> n4.handle(left) [label="OrderShipped"]
n4.handle(right) -> n5.handle(left) [label="Transactional"]
n5.handle(bottom) -> Dispatcher.n6.handle(top) [label="After Commit"]
}
Dispatcher { # Event Dispatcher
n6: rectangle label:"Collect Pending Events"
n7: rectangle label:"Resolve Event Handlers"
n8: rectangle label:"Dispatch In-Process"
n9: rectangle label:"Dispatch Cross-Boundary"
n6.handle(right) -> n7.handle(left) [label="Event Queue"]
n7.handle(right) -> n8.handle(left) [label="Same Bounded Context"]
n7.handle(bottom) -> n9.handle(top) [label="External Context"]
n8.handle(bottom) -> Handlers.n10.handle(top) [label="Sync"]
n9.handle(bottom) -> Integration.n14.handle(top) [label="Async"]
}
Handlers { # In-Process Handlers
n10: rectangle label:"Update Read Cache"
n11: rectangle label:"Send Notification"
n12: rectangle label:"Trigger Side Effect"
n13: rectangle label:"Log Audit Entry"
n10.handle(right) -> n11.handle(left)
n11.handle(right) -> n12.handle(left)
n12.handle(right) -> n13.handle(left)
}
Integration { # Integration Events
n14: rectangle label:"Publish to Message Bus"
n15: rectangle label:"Billing Context Subscribes"
n16: rectangle label:"Shipping Context Subscribes"
n17: rectangle label:"Analytics Context Subscribes"
n14.handle(right) -> n15.handle(left) [label="BillingEvent"]
n14.handle(bottom) -> n16.handle(top) [label="ShippingEvent"]
n15.handle(right) -> n17.handle(left)
n16.handle(right) -> n17.handle(left)
}

为什么需要这个工作流?

Tightly coupling side effects (notifications, cache updates, audit logs) to domain logic makes business rules harder to test and modify. Domain events decouple the "what happened" from the "what should happen next," enabling clean separation between core business logic and infrastructure concerns.

工作原理

  1. Step 1: An aggregate root performs a business operation and raises a domain event.
  2. Step 2: The unit of work commits the transaction and hands events to the dispatcher.
  3. Step 3: In-process handlers execute synchronous side effects within the same bounded context.
  4. Step 4: Cross-boundary events are published to a message bus for other contexts.
  5. Step 5: Billing, Shipping, and Analytics contexts subscribe to relevant integration events.
  6. Step 6: Each handler operates independently, so failures in one do not affect others.

替代方案

Direct method calls between services create tight coupling. Mediator patterns (MediatR) handle in-process events but not cross-service. This template shows both in-process and cross-boundary event dispatching.

Key Facts

Template Name事件驱动领域事件架构
CategoryArchitecture
Steps6 workflow steps
FormatFlowZap Code (.fz file)

相关模板

领域驱动设计限界上下文架构

Architecture

DDD 限界上下文架构图,订单、客户、物流和计费上下文通过防腐层、共享内核和定义集成关系的上下文映射连接。该模板可视化战略 DDD 模式,将复杂领域分解为通过明确定义的集成事件通信的自治限界上下文。对于将领域驱动设计应用于大规模企业系统的架构师至关重要。

按业务能力分解微服务架构

Architecture

按业务能力组织的微服务分解架构图:身份认证、产品目录、定价和订单履行,每个都有独立的数据存储和 API。该模板展示如何将单体应用拆分为与业务领域对齐的服务,使用 Backend-for-Frontend (BFF) 模式进行客户端特定的聚合。适合规划领域驱动微服务边界的架构师。

事件驱动编排架构

Architecture

事件驱动编排架构图,微服务通过事件协调而无需中央编排器,订单、支付、库存和物流服务相互响应对方的领域事件。该模板模拟去中心化协调模式,每个服务只了解自己的职责并发布事件供其他服务消费。适合偏好服务自治而非集中式工作流控制的团队。

Saga 编舞模式架构

Architecture

Saga 编舞架构图,订单、支付和库存服务通过领域事件协调而无需中央编排器,每个服务发布和订阅驱动事务前进或触发补偿的事件。该模板模拟去中心化的 Saga 方法,服务自主响应事件,减少单点故障但增加了跟踪 Saga 状态的复杂性。适合偏好服务自治而非集中控制的团队。

微服务 API 网关架构

Architecture

微服务 API 网关架构图,展示请求路由、JWT 身份验证、速率限制、服务发现以及跨分布式后端服务的响应聚合。该模板模拟微服务生态系统中所有客户端流量的入口点,在请求到达内部服务之前执行安全策略。适合设计具有集中式横切关注点的可扩展 API 基础设施的平台工程师。

微服务服务网格架构

Architecture

服务网格架构图,展示 Istio 或 Linkerd 边车代理处理 mTLS 加密、流量策略、熔断器和跨微服务的分布式追踪。该模板可视化服务网格如何将网络关注点从应用代码中抽象出来,实现服务间的零信任通信。对于采用服务网格基础设施以提升可观测性和安全性的团队至关重要。

返回所有模板