architecture - 使用rabbitmq队列处理微服务之间的实时数据同步
问题描述
我们已经将我们的微服务设置为使用rabbitmq
队列来处理任何即将到来的数据。
所以,我们有service A
、service B
和。service C
service D
当请求创建一个用户时service A
,它会创建一个用户并将一个事件推送到newUserIsCreated
队列中。
现在,service B
需要service C
为新添加的用户更新他们的数据库。
问题:
如果用户创建在任何service B
and中失败怎么办service C
。
可能的失败情况可能是验证失败,或者service B
关闭service C
。
如果service D
尝试访问同一用户,也会出现问题。
service B
会有什么service C
反应?因为到那时他们将没有新创建的用户。
总结一下,
如何处理这些服务之间的验证失败或任何类型的失败?
如果需要实时同步怎么办?(队列可能需要一些时间来处理)
解决方案
您可以拥有B
并且C
(以及任何其他需要确认用户已创建的服务)向 RabbitMQ 发布确认消息以供A
; A
然后在收到确认之前不确认用户已创建。这不是万无一失的:它很有可能在所有地方都取得了成功,但A
没有及时得到确认。对于那种(理想情况下很少见)的情况,您可以有一个清理过程。
It's worth noticing though, that this sort of synchronous relation is basically reimplementing request/response via a message queue, so if going down this path, why not just do direct request/response (e.g. HTTP or gRPC)?
If you have an operation which needs to affect multiple services, it's often useful to model it as a saga, which will make requests to the various services and handle failures (and potentially give up if there have been too many failures, rolling back the changes it's made). By persisting the state of the saga (e.g. updated B
, but C
has not yet acknowledged the update), it can be queried.
如果您发现大多数操作B
都是 sagas touch 的一部分A
,那么可能值得考虑是否B
并且A
应该是相同的服务,这将提供更强的一致性保证并可能简化事情。
推荐阅读
- nlp - 按个人从消息/对话中提取常用/典型短语
- sql - 在 SQL 中计数时如何分隔列?
- r - DiagrammeR - Digraph 流程图 - 箭头上标签的位置和方向
- google-cloud-platform - 如何在 Vertex AI 中安排自定义训练作业的重复运行
- deep-learning - 如何从头开始构建“ner_ontonotes_bert_mult”模型
- ios - 核心数据检索问题
- angular - Angular 2 Lifecycle Hooks、变更检测和 ngZone
- api - 使用基于 Office 365 登录的访问令牌创建 mvc rest api
- javascript - Electron javascript 仅在 devtools 打开时工作
- python - 在 FastAPI 中连接数据库的最佳方法是什么?