简体   繁体   English

微服务中的事务

[英]Transactions in microservices

I have read some articles about microservices architecture, but no one takes the topic of transaction. 我已经阅读了一些关于微服务架构的文章,但没有人讨论交易的主题。 All that they says that this is hard to do it. 所有他们说这很难做到。 Maybe someone can describe how to handle this? 也许有人可以描述如何处理这个?

But not from domain side, but from technology side. 但不是来自域方面,而是来自技术方面。 Lets say that we have business case where we need to invoke two different services and both of them make some changes on database. 让我们说我们有业务案例,我们需要调用两个不同的服务,他们都对数据库进行了一些更改。 But how to rollback if some error occurs on the second one? 但是如果在第二个错误发生时如何回滚?

Who knows some libraries or design patter for this problem? 谁知道这个问题的一些库或设计模式?

I may not be the ultimate expert in this, but I'm sure you're heading towards the Distributed Transactions . 我可能不是这方面的终极专家,但我相信你正在走向分布式交易 In order to have them running, all the application service components need a common shared transaction id, and you have to make sure that every component is informed about the state of the transaction. 为了让它们运行,所有应用程序服务组件都需要一个共同的共享事务ID,并且您必须确保每个组件都被告知事务的状态。 It is asynchronous, so you'll require substantial prog skills. 它是异步的,因此您需要大量的编程技能。

Here are distributed transactions mentioned or discussed: 以下是提到或讨论的分布式交易:

https://en.wikipedia.org/wiki/Distributed_transaction https://en.wikipedia.org/wiki/Distributed_transaction

http://contino.co.uk/microservices-not-a-free-lunch/ http://contino.co.uk/microservices-not-a-free-lunch/

http://martinfowler.com/articles/microservices.html http://martinfowler.com/articles/microservices.html

It would seem people try to avoid it as it is difficult. 似乎人们试图避免它,因为它很难。 Maybe that's why you don't find much about. 也许这就是为什么你找不到多少。

Hope this helps a step forward :-) 希望这有助于向前迈进一步:-)

The best design is having isolated services: each service just do its work within its own transaction and your workflow expects failures on the single service. 最好的设计是提供隔离服务:每个服务只在自己的事务中完成工作,而您的工作流程预计单个服务会出现故障。

If you really need to commit only if all the services are called without errors you should create an higher level service that perform those calls inside an external transaction. 如果您确实只需要在没有错误的情况下调用所有服务时提交,那么您应该创建一个更高级别的服务来在外部事务中执行这些调用。

The first raw thing which came to my mind after reading this question is to create every add api with a delete api with ,lets say, an extra boolean flag delFlag. 在阅读完这个问题之后,我想到的第一个原始内容是使用delete api创建每个add api,比方说,一个额外的布尔标志delFlag。

boolean flag delFlag; boolean flag delFlag;

For POST, it will be 0. For DELETE, it will be 1. 对于POST,它将为0.对于DELETE,它将为1。

Now you maintain a Transaction Manager which is a super service to all your micro services. 现在您维护一个事务管理器,它是所有微服务的超级服务。 In this service maintain the calling queue of all the services and the APIs. 在此服务中维护所有服务和API的调用队列。 As and when a service fails, get the calling api and call the delete method of that service and undone whatever u have done. 当服务失败时,获取调用api并调用该服务的delete方法并撤消您所做的任何操作。

PS- Just a raw thought. PS-只是一个原始的想法。 Correct me if you think it is wrong. 如果你认为这是错误的,请纠正我。

Building on top of previous answers, distributed transactions are the solution. 在之前的答案的基础上,分布式事务是解决方案。 In my opinion you don't want to build your own mechanisms for tracking global transactional state, rather you want to use some kind of product - there are several out there. 在我看来,你不想建立自己的机制来跟踪全局事务状态,而是想要使用某种产品 - 有几种产品。 I have written a lengthy blog article about solving this issue with a Java application server: 我写了一篇关于用Java应用服务器解决这个问题的博文:

http://blog.maxant.co.uk/pebble/2015/08/04/1438716480000.html http://blog.maxant.co.uk/pebble/2015/08/04/1438716480000.html

两阶段提交可以是option.Coordinator发送提交请求消息给cohorts.Cohorts发回ok.After协调器发送提交消息到同类群组。如果任何故障happpens协调器发送回滚消息给同类群组。

You can use a workflow engine(like JBPM,Activiti) to orchestrate the logic and handle transaction failures or compensatory transactions in it to achieve data integrity. 您可以使用工作流引擎(如JBPM,Activiti)来编排逻辑并处理其中的事务失败或补偿事务以实现数据完整性。 This is the similar case you use in SOA architecture with ESB,BPMN and Web services 这是您在SOA架构中使用的类似情况,包括ESB,BPMN和Web服务

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM