# 六边形架构（软件）

**六边形架构**，又称**端口与适配器**，是由阿利斯泰尔·科伯恩（Alistair Cockburn）于2005年提出的一种软件架构模式。该模式将所有交互都经由称为“端口”的接口进行路由，每个端口由特定技术的“适配器”来实现，从而将应用的业务逻辑与用户界面、数据库及其他外部技术隔离开来。<sup>[1](https://alistair.cockburn.us/hexagonal-architecture)</sup><sup> • </sup><sup>[2](https://docs.aws.amazon.com/en_en/prescriptive-guidance/latest/cloud-design-patterns/hexagonal-architecture.html)</sup>

| 关键事实 | 详情 |
|---|---|
| 起源 | 由阿利斯泰尔·科伯恩提出；原始文章为 HaT Technical Report 2005.02，日期为2005-09-04<sup>[1](https://alistair.cockburn.us/hexagonal-architecture)</sup> |
| 别名 | 端口与适配器——科伯恩将此名视为正名，六边形架构则为别名<sup>[1](https://alistair.cockburn.us/hexagonal-architecture)</sup> |
| 明确意图 | 使应用能够同等地被用户、程序、自动化测试或批处理脚本驱动，并能脱离其最终的运行时设备和数据库进行开发与测试<sup>[1](https://alistair.cockburn.us/hexagonal-architecture)</sup> |
| 核心规则 | 涉及应用内部的代码不应泄漏到外部；依赖指向内部，因此适配器依赖于应用的端口，而非相反<sup>[1](https://alistair.cockburn.us/hexagonal-architecture)</sup><sup> • </sup><sup>[3](https://docs.aws.amazon.com/prescriptive-guidance/latest/hexagonal-architectures/adapt-to-change.html)</sup> |
| 层次 | 端口与适配器只规定两个层次——内部（应用）和外部（其余一切），并要求外部参与者连接到特定的端口<sup>[4](https://alistaircockburn.com/hexarch%20v1.1b%20DIFFS%2020250420-1012%20paper%2Bepub.docx.pdf)</sup> |
| 为何是六边形 | 形状与数字六无关；它在视觉上将内部与外部隔开，并留出空间绘制任意数量的端口和适配器，而不像一维的分层堆叠<sup>[1](https://alistair.cockburn.us/hexagonal-architecture)</sup> |
| 主要代价 | 额外的适配器代码增加了维护开销，并引入一个可能增加延迟的附加层；主要是在应用拥有多个输入源和输出目标，或其数据存储随时间变化时，这一开销才值得<sup>[2](https://docs.aws.amazon.com/en_en/prescriptive-guidance/latest/cloud-design-patterns/hexagonal-architecture.html)</sup> |

## 该模式所解决的问题

科伯恩2005年的文章围绕两种对称的失败来构建这一模式。在用户侧，业务逻辑渗入界面代码，导致系统无法用自动化测试套件干净地测试，无法从人工驱动切换为批处理运行，也无法轻易被另一个程序驱动。在数据侧，应用逻辑与特定数据库或外部服务绑定，导致开发人员在数据库宕机或重构时无法工作。两个问题源于同一个设计错误：业务逻辑与外部实体交互之间的纠缠。<sup>[1](https://alistair.cockburn.us/hexagonal-architecture)</sup>

反复尝试的修复办法——增加一个新层并承诺不会有业务逻辑进入其中——之所以失败，是因为组织没有机制来检测这一承诺何时被打破。科伯恩的补救办法是让每一份应用功能都通过 API 提供，这样自动化回归测试就能在逻辑泄漏发生时将其检测出来。<sup>[1](https://alistair.cockburn.us/hexagonal-architecture)</sup>

## 端口与适配器

端口是一个与技术无关的接口，应用通过它与外部世界通信。这个词意在让人联想到计算机和电子设备上的物理端口——任何遵循端口协议的东西都可以插入；协议以 API 的形式呈现，由对话的目的来定义。<sup>[1](https://alistair.cockburn.us/hexagonal-architecture)</sup><sup> • </sup><sup>[2](https://docs.aws.amazon.com/en_en/prescriptive-guidance/latest/cloud-design-patterns/hexagonal-architecture.html)</sup>

适配器是连接端口与具体外部参与者的技术特定代码。当事件从外部世界到达端口时，适配器将其转换为可用的过程调用或消息并传递给应用，而应用对输入设备的性质一无所知；向外通信则以相反方向同样运作。<sup>[1](https://alistair.cockburn.us/hexagonal-architecture)</sup>一个端口可以有多个适配器，而不会对端口或应用构成风险：一个 REST 适配器和一个 GraphQL 适配器可以服务于同一个端口；在数据侧，一个 SQL 适配器、一个平面文件适配器和一个内存模拟数据库适配器都可以实现同一个仓储端口。<sup>[1](https://alistair.cockburn.us/hexagonal-architecture)</sup><sup> • </sup><sup>[2](https://docs.aws.amazon.com/en_en/prescriptive-guidance/latest/cloud-design-patterns/hexagonal-architecture.html)</sup>

**驱动侧与被驱动侧。** 科伯恩区分了主（驱动）端口与适配器——由用户、测试框架或批处理脚本等外部参与者发起交互——和次（被驱动）端口与适配器——由应用发起调用，例如对数据库或支付网关的调用。在他后来的著作术语中，只有驱动/被驱动和主/次这两对词适用于全部三个概念：参与者、适配器和端口；对于端口和适配器，也可用入站/出站和 API/SPI 表示这种区分，但这些称谓不适用于参与者。<sup>[4](https://alistaircockburn.com/hexarch%20v1.1b%20DIFFS%2020250420-1012%20paper%2Bepub.docx.pdf)</sup>

**强符合与弱符合。** 一种弱但合法的实现以已知技术来表达被驱动端口，例如用 SQL 定义端口。在强实现中，应用不能知道任何关于外部技术的信息：被驱动端口纯粹以应用自身语言中有意义的概念来表达，因此甚至不能透露数据库的存在。<sup>[4](https://alistaircockburn.com/hexarch%20v1.1b%20DIFFS%2020250420-1012%20paper%2Bepub.docx.pdf)</sup>

## 与分层、洋葱和整洁架构的关系

科伯恩指出端口与适配器同分层、洋葱和[整洁架构](https://www.edgepedia.org/zh/clean-architecture)之间有两个区别：它只有两个层次——内部和外部；它要求外部参与者连接到特定端口。洋葱架构和整洁架构共享相同的依赖结构，但各自增加了层次，且不要求端口；一旦端口与适配器就位，增加这些层次便是可选的，属于该模式之外的事情。<sup>[4](https://alistaircockburn.com/hexarch%20v1.1b%20DIFFS%2020250420-1012%20paper%2Bepub.docx.pdf)</sup>微软的 .NET 架构指南将六边形架构列为这一设计家族的最早名称之一，后来被称为洋葱架构和整洁架构，它们都反转了依赖关系，使基础设施依赖于应用核心，而非相反。<sup>[5](https://learn.microsoft.com/en-us/dotnet/architecture/modern-web-apps-azure/common-web-application-architectures)</sup>

依赖反转是使该模式运作的机制。领域位于核心，所有其他模块——端口和适配器——都依赖于领域模型；领域通过端口向外通信，因此领域模型可以自由更改而不会破坏代码的其他部分。<sup>[3](https://docs.aws.amazon.com/prescriptive-guidance/latest/hexagonal-architectures/adapt-to-change.html)</sup>

## 测试与开发实践

该模式最直接的回报是可测试性。由于应用核心仅依赖于端口抽象，业务逻辑可以使用模拟或伪适配器进行隔离测试，而集成测试则保留用于验证次要适配器是否正确调用外部服务；集成测试不应重新验证业务逻辑。<sup>[6](https://docs.aws.amazon.com/prescriptive-guidance/latest/hexagonal-architectures/improve-software-quality.html)</sup>这支持测试驱动开发——针对端口编写的测试在真实适配器存在之前就产生模拟实现——以及行为驱动开发——本地的主适配器和次适配器让 BDD 套件无需调用外部服务即可运行应用。<sup>[7](https://docs.aws.amazon.com/prescriptive-guidance/latest/hexagonal-architectures/best-practices.html)</sup>

本地单元测试还缩短了反馈循环。AWS 的指南将以分钟计的云部署周期与以秒计完成的本地单元测试进行对比，并指出将组件隔离在已定义的接口之后可以让团队并行开发不同组件。<sup>[8](https://docs.aws.amazon.com/prescriptive-guidance/latest/hexagonal-architectures/improve-dev-cycle.html)</sup>一种推荐的项目布局直接映射这些概念：入口点（主适配器）、包含命令、事件、模型和端口的领域文件夹，以及用于次适配器实现的适配器文件夹。<sup>[7](https://docs.aws.amazon.com/prescriptive-guidance/latest/hexagonal-architectures/best-practices.html)</sup>

## 代价与适用性

适配器层并非没有成本。AWS 的模式指南列出了维护开销——只有当组件拥有多个输入源和输出目标，或其数据存储必须随时间变化时，可插拔的适配器代码才值得——以及附加层可能带来的延迟。它还指出了复杂性：粗心处理业务逻辑与基础设施的分离，会产生本身就很难解决的问题。<sup>[2](https://docs.aws.amazon.com/en_en/prescriptive-guidance/latest/cloud-design-patterns/hexagonal-architecture.html)</sup>该模式与领域驱动设计契合良好，当多种客户端类型使用同一领域逻辑时运作良好，并在 UI 和数据库组件需要定期技术更新而不应触及应用逻辑时有所帮助。<sup>[2](https://docs.aws.amazon.com/en_en/prescriptive-guidance/latest/cloud-design-patterns/hexagonal-architecture.html)</sup>

## 后续发展

科伯恩持续完善这一模式。2022年9月，他发表了《组件 + 策略泛化端口与适配器》，描述了“组件 + 策略”——配置子系统以适应略有不同的环境——作为端口与适配器的更一般形式；在该框架中，端口与适配器是组件 + 策略的一种具体运用，组件边界恰好置于外部技术之前，测试替身可以作为策略对象或适配器对象发挥作用。<sup>[9](https://alistaircockburn.com/Articles/Component-Strategy-generalizes-Ports-Adapters)</sup><sup> • </sup><sup>[10](https://alistaircockburn.com/Component%20plus%20strategy.pdf)</sup>他与胡安·曼努埃尔·加里多·德帕斯（Juan Manuel Garrido de Paz）合作撰写了一本书《六边形架构详解》，于2025年通过预览版和完整第一版扩充了材料，包括 BlueZone 停车应用等实例以及文件夹命名指南。<sup>[4](https://alistaircockburn.com/hexarch%20v1.1b%20DIFFS%2020250420-1012%20paper%2Bepub.docx.pdf)</sup>

## References

1. [Alistair Cockburn, "Hexagonal architecture: the original 2005 article" (HaT Technical Report 2005.02)](https://alistair.cockburn.us/hexagonal-architecture)
2. [Hexagonal architecture pattern, AWS Prescriptive Guidance](https://docs.aws.amazon.com/en_en/prescriptive-guidance/latest/cloud-design-patterns/hexagonal-architecture.html)
3. [Adapting to change, Building hexagonal architectures on AWS, AWS Prescriptive Guidance](https://docs.aws.amazon.com/prescriptive-guidance/latest/hexagonal-architectures/adapt-to-change.html)
4. [Alistair Cockburn, Hexagonal Architecture Explained, updates from v0.9 to v1.1 (2025)](https://alistaircockburn.com/hexarch%20v1.1b%20DIFFS%2020250420-1012%20paper%2Bepub.docx.pdf)
5. [Common web application architectures, .NET documentation, Microsoft Learn](https://learn.microsoft.com/en-us/dotnet/architecture/modern-web-apps-azure/common-web-application-architectures)
6. [Quality by design, Building hexagonal architectures on AWS, AWS Prescriptive Guidance](https://docs.aws.amazon.com/prescriptive-guidance/latest/hexagonal-architectures/improve-software-quality.html)
7. [Best practices, Building hexagonal architectures on AWS, AWS Prescriptive Guidance](https://docs.aws.amazon.com/prescriptive-guidance/latest/hexagonal-architectures/best-practices.html)
8. [Improving the development cycle, Building hexagonal architectures on AWS, AWS Prescriptive Guidance](https://docs.aws.amazon.com/prescriptive-guidance/latest/hexagonal-architectures/improve-dev-cycle.html)
9. [Alistair Cockburn, "Component + Strategy generalizes Ports & Adapters" (September 2022)](https://alistaircockburn.com/Articles/Component-Strategy-generalizes-Ports-Adapters)
10. [Alistair Cockburn, Component plus strategy (PDF)](https://alistaircockburn.com/Component%20plus%20strategy.pdf)

---
*Initially written Sep 17, 2026 · Reviewed: Oct 15, 2026 · Edited: Oct 15, 2026 · Last review: Oct 15, 2026*

Topic: 技术与人造世界 › 计算技术与数字系统 › 软件与编程 › Software engineering and development process › Software design and architecture › Titles 4 to Mo

Copyright 2026 Edgepedia

License: Edgepedia Community License 1.0, https://www.edgepedia.org/edgepedia/license

Source: https://www.edgepedia.org/zh/hexagonal-architecture-software

English original: https://www.edgepedia.org/hexagonal-architecture-software
