# 绞杀榕模式（二）接缝：从哪里切开旧系统

> 渐进汰换的前提是系统里有地方可以切；接缝可以在网络入口、消息、前端页面、数据库、程序内部，甚至是人。各层的做法和代价不同。

发布 2026-11-09・更新 2026-11-09・网址 https://wizerdbache.com/zh-hans/notes/strangler-fig-seams/


这是系列第二篇。[第一篇](https://wizerdbache.com/zh-hans/notes/strangler-fig-overview/)讲到绞杀榕模式的骨架是「在调用端和旧系统之间插一层，一块一块搬」。这一篇谈前提：系统里要有地方可以切。

## 先讲结论

- **接缝 (seam) 是可以把系统切开、让新旧分头处理的地方。** 设计良好、天生有接缝的系统很少见，多数时候要自己找出或插入（Fowler, 2024）。
- **接缝可以出现在很多层**：网络入口、消息、前端页面、数据库、程序内部，以及人的操作（Cartwright et al., 2024a）。
- **数据库层是最晚的拦截点。** 原文说变更走到数据库才拦，可以说已经太晚；在这里改变既有的写入行为要特别小心，可能破坏重要的隐性契约（Cartwright et al., 2024a）。
- **拦截器本身也要建、要维运**；改调用方可以省掉它，但调用方得接下它原本的责任（Cartwright et al., 2024a）。

## 一、什么是接缝

Fowler 说，软件理想上由可以单独替换的组件组成，但旧系统很少这样，所以要花相当功夫弄清楚怎么拆成小块，这涉及找出可以插入系统、把它切开的接缝。他还说，设计良好、天生就有接缝的系统少见到可以叫独角兽（[Fowler, 2024](https://martinfowler.com/bliki/StranglerFigApplication.html)）。

切的方向，Fowler 建议看系统支撑了哪些业务需求，把个别需求抽成新组件（[Fowler, 2024](https://martinfowler.com/bliki/StranglerFigApplication.html)）。Cartwright 等人也说，对遗留系统通常是找出大型方案所满足的多项业务需求之间的接缝，再看能否个别抽出来独立交付；他们也观察到，许多组织因为只单看技术或只单看业务流程，就认定这条路走不通而放弃（[Cartwright et al., 2024b, “Decide how to break the problem into smaller parts”](https://martinfowler.com/articles/patterns-legacy-displacement/#DecideHowToBreakTheProblemIntoSmallerParts)）。

## 二、接缝可以在哪一层

Cartwright 等人的 Event Interception 一文，把做法定义为利用旧系统组件之间现成的集成点：让组件之间传递的事件被拦下、改送到新组件，目标是尽量少改旧组件（[Cartwright et al., 2024a, “How it works”](https://martinfowler.com/articles/patterns-legacy-displacement/event-interception.html#how)）。各篇谈到的位置如下：

| 层次 | 做法 | 出处 |
|---|---|---|
| 网络入口 | 反向代理、API 闸道、负载平衡器依网址分流。Hammant 的航空公司案例用负载平衡器依 URL 把请求分到新旧两套系统，两边共用工作阶段 (session) 保存 | [Cartwright et al., 2024a, “API gateway”](https://martinfowler.com/articles/patterns-legacy-displacement/event-interception.html#ApiGateway)；[Hammant, 2013, “An airlines booking app”](https://paulhammant.com/2013/07/14/legacy-application-strangulation-case-studies/#an-airlines-booking-app) |
| 消息 | 在消息基础设施上依标头过滤，或由路由组件依内容分流 | [Cartwright et al., 2024a, “Messaging infrastructure”](https://martinfowler.com/articles/patterns-legacy-displacement/event-interception.html#MessagingInfrastructure) |
| 前端页面 | 对旧页面做一次小改动，放入能从外部加载行为的挂钩，之后的新行为就不必跟着旧系统的发布周期走 | [Cartwright et al., 2024a, “Within a web app”](https://martinfowler.com/articles/patterns-legacy-displacement/event-interception.html#WithinAWebApp-ProgressiveEnhancement) |
| 数据库 | 触发器或异动数据截取 (CDC)。Microsoft 举的例子是逻辑写在预存进程里、以数据库为中心、代码又改不动的系统 | [Cartwright et al., 2024a, “Database layer – triggers”](https://martinfowler.com/articles/patterns-legacy-displacement/event-interception.html#DatabaseLayer-Triggers)；[Microsoft, n.d.-b, “Problems and considerations”](https://learn.microsoft.com/en-us/azure/architecture/patterns/strangler-fig#problems-and-considerations) |
| 程序内部 | 在程序里包一层抽象，让调用端经过它（抽象分支，见[下一篇](https://wizerdbache.com/zh-hans/notes/strangler-fig-coexistence/)） | [Fowler, 2014](https://martinfowler.com/bliki/BranchByAbstraction.html) |
| 人 | 事件来自用户操作时，可以让用户在一段时间里部分操作在新系统做、部分在旧系统做（所谓旋转椅集成），代价是这段期间体验与效率可能变差 | [Cartwright et al., 2024a, “The human event interceptor”](https://martinfowler.com/articles/patterns-legacy-displacement/event-interception.html#TheHumanEventInterceptor) |

我的看法是：要换的是程序内部的模块、函数库或框架时，在程序内部包一层抽象是自然的选择；这是我的推论，原文没有这样分。

## 三、数据库层要特别小心

Event Interception 一文对数据库层有个重要提醒：变更走到数据库时，可以说拦截已经太晚；在这里改变既有的写入行为要特别小心，可能破坏重要的隐性契约（[Cartwright et al., 2024a, “Database layer – triggers”](https://martinfowler.com/articles/patterns-legacy-displacement/event-interception.html#DatabaseLayer-Triggers)）。

文中也区分了几种挂钩：

- **写入前触发的触发器**可以拦下写入。
- **写入后触发的触发器**不拦截写入，只产生通知事件（[Cartwright et al., 2024a, “Database triggers – post-commit”](https://martinfowler.com/articles/patterns-legacy-displacement/event-interception.html#DatabaseTriggers-Post-commit)）。
- **CDC** 从交易日志产生事件串流，严格说也不算拦截（[Cartwright et al., 2024a, “Change data capture”](https://martinfowler.com/articles/patterns-legacy-displacement/event-interception.html#ChangeDataCapturecdc)）。

## 四、拦截器的代价

拦截器是多出来的一件过渡组件。文中讨论了另一条路：直接改调用方，让它自己决定要找旧的还是新的。这样可以少一件过渡组件，可能出错、要维运的东西也少一件，但调用方要承担原本属于拦截器的责任，例如理解契约、处理错误、记录日志与监控（[Cartwright et al., 2024a, “Routing events within legacy”](https://martinfowler.com/articles/patterns-legacy-displacement/event-interception.html#RoutingEventsWithinLegacy)）。

Hammant 也提醒，网址若有对应应用子区块的目录结构，会比较容易一区一区处理（[Hammant, 2013, “For those considering strangulation”](https://paulhammant.com/2013/07/14/legacy-application-strangulation-case-studies/#for-those-considering-strangulation)）；他的航空案就是靠前置的负载平衡器依 URL 分流（[Hammant, 2013, “An airlines booking app”](https://paulhammant.com/2013/07/14/legacy-application-strangulation-case-studies/#an-airlines-booking-app)）。

## 这个系列

1. [为什么不整个重写，绞杀榕模式又是什么](https://wizerdbache.com/zh-hans/notes/strangler-fig-overview/)
2. 接缝：从哪里切开旧系统（本篇）
3. [新旧共存：防腐层、过渡架构、抽象分支、平行变更](https://wizerdbache.com/zh-hans/notes/strangler-fig-coexistence/)
4. [切换与验证：把不能回头的那一步留到最后](https://wizerdbache.com/zh-hans/notes/strangler-fig-cutover/)
5. [陷阱与不适合的情况](https://wizerdbache.com/zh-hans/notes/strangler-fig-pitfalls/)
6. [学术界怎么看：证据、缺口与一份检查清单](https://wizerdbache.com/zh-hans/notes/strangler-fig-evidence/)

## 来源

- [Fowler, M. (2024, August 22). Strangler fig. martinfowler.com.](https://martinfowler.com/bliki/StranglerFigApplication.html)（博客文章）
- [Cartwright, I., Horn, R., & Lewis, J. (2024a, March 5). Event interception. martinfowler.com.](https://martinfowler.com/articles/patterns-legacy-displacement/event-interception.html)（模式文章）
- [Cartwright, I., Horn, R., & Lewis, J. (2024b, March 5). Patterns of legacy displacement. martinfowler.com.](https://martinfowler.com/articles/patterns-legacy-displacement/)（模式集总览）
- [Hammant, P. (2013, July 14). Legacy application strangulation: Case studies. Paul Hammant's blog.](https://paulhammant.com/2013/07/14/legacy-application-strangulation-case-studies/)（案例集）
- [Microsoft. (n.d.-b). Strangler Fig pattern. Azure Architecture Center. Retrieved November 8, 2026.](https://learn.microsoft.com/en-us/azure/architecture/patterns/strangler-fig)（官方文档）
- [Fowler, M. (2014, January 7). Branch by abstraction. martinfowler.com.](https://martinfowler.com/bliki/BranchByAbstraction.html)（博客文章）
