我们正计划从单体架构迁移到基于微服务的架构。现在我有责任从单体中谈论一个模块。
现有单体:
1) 代码耦合非常紧密。
2) 使用不同的参数递归调用 API。
3)我计划提取的模块中的一些调用包含对系统的调用,大约需要 9 分钟才能完成。不幸的是,这是一个同步的。
注意事项:
1) 我从单个 api 迁移开始,这是一个非常重要的迁移,但表现不佳。2) 这个 api 包含对另一个系统的并行调用,用于执行一堆任务。所有呼叫都是阻塞且耗时的(考虑平均响应时间为 5-6 分钟)
迁移到基于微服务的架构:在将上述 api 从单体应用迁移到单独的微服务时,我想到了两种方法,同时解决了由于阻塞调用时间而阻塞线程的问题。
a) 分阶段进行:
- Create a separate module
- In this module provide an api to push events to kafka, another
module will in-turn process the request and push the response back
to kafka
- monolith for now will call above mentioned api to push events to
kafka
- New module will inturn call back the monolith when the task
complete (received response on a separate topic in kafka)
- Monolith once get response for all the tasks will trigger some post
processing activity.
Advantage:
1) It will solve the problem of sync- blocking call.
Disadvantage:
1) Changes are required in the monolith, which could introduce some
bugs.
2) No fallbacks are available for the case if bug gets introduced.
b) 立即将 API 移动到微服务:
最初,它将与单体共享公共数据源,并通过在新微服务和需要时间处理请求的模块之间引入 kafka 来解决阻塞调用的问题。
优势:
1) 后备在单体应用中可用
坏处:
1) 最初数据源在系统之间共享。
完成这些复杂任务的最佳方法应该是什么?