跨REST微服务交易? [英] Transactions across REST microservices?

查看:133
本文介绍了跨REST微服务交易?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

让我们说我们有一个用户,钱包REST微服务和胶合在一起的东西的API网关。当Bob在我们的网站注册时,我们的API网关需要通过创建用户和微服务用户通过钱包微服务一个钱包。

Let's say we have a User, Wallet REST microservices and an API gateway that glues things together. When Bob registers on our website, our API gateway needs to create a user through the User microservice and a wallet through the Wallet microservice.

现在这里有几个方案,事情可能出错:

Now here are a few scenarios where things could go wrong:


  • 用户bob创建失败:没关系,我们只是返回一个错误信息给鲍勃。我们使用SQL事务,所以没有人见过鲍勃在系统中。一切都很好:)

  • User Bob creation fails: that's OK, we just return an error message to the Bob. We're using SQL transactions so no one ever saw Bob in the system. Everything's good :)

用户bob创建,但可以创造我们的钱包之前,我们的API网关硬崩溃。我们现在有一个用户不带钱包(数据不一致)。

User Bob is created but before our Wallet can be created, our API gateway hard crashes. We now have a User with no wallet (inconsistent data).

用户bob创建和我们正在创建的钱包,HTTP连接下降。钱包可能创造成功,也可能没有。

User Bob is created and as we are creating the Wallet, the HTTP connection drops. The wallet creation might have succeeded or it might have not.

什么解决方案可为prevent这种数据不一致的情况发生?是否有模式,允许事务跨越多个REST请求?我读过关于两相的维基百科页面提交这似乎在这个问题上碰,但我不知道如何在实践中应用它。这原子分布式事务:一个RESTful设计,虽然我没有看过纸似乎也有趣它没有。

What solutions are available to prevent this kind of data inconsistency from happening? Are there patterns that allow transactions to span multiple REST requests? I've read the Wikipedia page on Two-phase commit which seems to touch on this issue but I'm not sure how to apply it in practice. This Atomic Distributed Transactions: a RESTful design paper also seems interesting although I haven't read it yet.

另外,我知道REST可能只是不适合这种使用情况。或许会以正确的方式来处理这种情况完全省略REST和使用不同的通信协议如消息队列系统?或者我应该在我的应用程序code(强制例如,具有检测到存在不一致和修复它们后台作业或让我的用户模型创造一个国家属性一致性,创造的价值观,等等)?

Alternatively, I know REST might just not be suited for this use case. Would perhaps the correct way to handle this situation to drop REST entirely and use a different communication protocol like a message queue system? Or should I enforce consistency in my application code (for example, by having a background job that detects inconsistencies and fixes them or by having a "state" attribute on my User model with "creating", "created" values, etc.)?

推荐答案

所有分布式系统与事务一致性的麻烦。要做到这一点,最好的方法就是像你说的,有一个两阶段提交。有钱包和用户处于挂起状态创建。它创建后,进行单独的调用激活用户。

All distributed systems have trouble with transactional consistency. The best way to do this is like you said, have a two-phase commit. Have the wallet and the user be created in a pending state. After it is created, make a separate call to activate the user.

这最后一个电话应该是安全地重复(如果你的连接断开)。

This last call should be safely repeatable (in case your connection drops).

这将需要的最后一个呼叫知道两个表(以便它可以在一个单一的JDBC事务完成)。

This will necessitate that the last call know about both tables (so that it can be done in a single JDBC transaction).

另外,你可能要想想为什么你这么担心没有钱包的用户。你相信这将导致一个问题?如果是这样,也许有这些作为单独的休息通话是一个坏主意。如果用户不应该在没有一个钱包存在,那么你或许应该钱包添加到用户(在原岗位调用创建的用户)。

Alternatively, you might want to think about why you are so worried about a user without a wallet. Do you believe this will cause a problem? If so, maybe having those as separate rest calls are a bad idea. If a user shouldn't exist without a wallet, then you should probably add the wallet to the user (in the original POST call to create the user).

这篇关于跨REST微服务交易?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆