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

六边形(端口与适配器)架构

Architecture

六边形架构图,展示领域核心、入站适配器(REST、GraphQL、CLI、消息消费者)、出站适配器(PostgreSQL、Stripe、邮件、Redis)以及连接它们的端口接口之间的清晰分离。该模板可视化端口与适配器模式,领域核心对基础设施零依赖,使其完全可测试且技术无关。对于在代码库中采用整洁架构原则的团队至关重要。

完整 FlowZap 代码

InboundAdapters { # Inbound Adapters (Driving)
n1: rectangle label:"REST Controller"
n2: rectangle label:"GraphQL Resolver"
n3: rectangle label:"CLI Command Handler"
n4: rectangle label:"Message Consumer"
n1.handle(bottom) -> Ports.n5.handle(top) [label="Port: CreateOrder"]
n2.handle(bottom) -> Ports.n5.handle(top) [label="Port: CreateOrder"]
n3.handle(bottom) -> Ports.n6.handle(top) [label="Port: RunReport"]
n4.handle(bottom) -> Ports.n6.handle(top) [label="Port: ProcessEvent"]
}
Ports { # Ports (Interfaces)
n5: rectangle label:"Inbound Port: OrderUseCase"
n6: rectangle label:"Inbound Port: ReportUseCase"
n7: rectangle label:"Outbound Port: OrderRepository"
n8: rectangle label:"Outbound Port: PaymentGateway"
n5.handle(bottom) -> Core.n9.handle(top) [label="Implement"]
n6.handle(bottom) -> Core.n10.handle(top) [label="Implement"]
}
Core { # Domain Core (Hexagon)
n9: rectangle label:"Order Service"
n10: rectangle label:"Report Service"
n11: rectangle label:"Domain Model (Entities)"
n12: rectangle label:"Business Rules (Value Objects)"
n9.handle(right) -> n11.handle(left) [label="Use"]
n10.handle(right) -> n11.handle(left) [label="Use"]
n11.handle(right) -> n12.handle(left) [label="Enforce"]
n9.handle(bottom) -> Ports.n7.handle(top) [label="Call Port"]
n9.handle(bottom) -> Ports.n8.handle(top) [label="Call Port"]
}
OutboundAdapters { # Outbound Adapters (Driven)
n13: rectangle label:"PostgreSQL Repository"
n14: rectangle label:"Stripe Payment Adapter"
n15: rectangle label:"Email Notification Adapter"
n16: rectangle label:"Redis Cache Adapter"
n7.handle(bottom) -> OutboundAdapters.n13.handle(top) [label="Implements"]
n8.handle(bottom) -> OutboundAdapters.n14.handle(top) [label="Implements"]
n13.handle(right) -> n15.handle(left)
n14.handle(right) -> n16.handle(left)
}

为什么需要这个工作流?

When business logic depends directly on frameworks, databases, and external services, it becomes impossible to test in isolation and expensive to change infrastructure. Hexagonal architecture inverts these dependencies through ports (interfaces) and adapters (implementations), keeping the domain core pure and technology-agnostic.

工作原理

  1. Step 1: Inbound adapters (REST controllers, GraphQL resolvers, CLI handlers) receive external input.
  2. Step 2: Adapters call inbound port interfaces defined by the domain core.
  3. Step 3: The domain core implements business logic using entities and value objects.
  4. Step 4: When the domain needs infrastructure, it calls outbound port interfaces.
  5. Step 5: Outbound adapters (PostgreSQL repository, Stripe payment, email sender) implement these ports.
  6. Step 6: Swapping infrastructure requires only changing the adapter, not the domain code.

替代方案

Layered architecture is simpler but allows infrastructure dependencies to leak into business logic. Clean Architecture adds more layers but follows the same dependency inversion principle. This template shows the hexagonal approach with concrete adapter examples.

Key Facts

Template Name六边形(端口与适配器)架构
CategoryArchitecture
Steps6 workflow steps
FormatFlowZap Code (.fz file)

相关模板

整洁架构层次

Architecture

整洁架构层次图,展示四个同心层:实体(领域)、用例(应用)、接口适配器(控制器、展示器、仓储)和框架与驱动(Web 服务器、数据库、云 SDK)。该模板模拟 Uncle Bob 的整洁架构及其依赖规则——内层永远不依赖外层,确保领域模型保持纯净且框架无关。对于强制架构边界的软件架构师来说是必备参考。

微服务 API 网关架构

Architecture

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

微服务服务网格架构

Architecture

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

微服务每服务独立数据库架构

Architecture

每服务独立数据库架构图,每个微服务拥有其专用数据存储,通过 Kafka 进行事件驱动同步以实现跨服务数据一致性。该模板展示了微服务数据隔离的核心原则,展示 PostgreSQL 和 MongoDB 如何在多语言持久化策略中共存。对于在保持最终一致性的同时强制服务自治的架构师至关重要。

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

Architecture

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

微服务绞杀者模式迁移架构

Architecture

绞杀者模式迁移架构图,展示使用路由层在新旧系统之间分流流量,逐步用新微服务替换遗留单体应用。该模板模拟经过验证的迁移策略,新功能作为微服务构建,遗留端点逐步退役。对于在不进行高风险大爆炸重写的情况下现代化遗留系统的团队至关重要。

返回所有模板