如何回滚微服务 [英] How to rollback MicroServices

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

问题描述

我对微服务有疑问.假设有5个微服务,例如M1,M2,M3,M3,M4和M5.有4个微服务连接/访问的4个数据库. 例如,M2连接到MySql,M3连接到Cassandra,M4连接到MangoDb,M5连接到Oracle.

I have doubt related to MicroServices. Suppose there are 5 Micro-Services lets say M1, M2, M3, M3, M4 and M5. There are 4 databases which are connected/accessed by 4 micro-services. Example, M2 connected to MySql, M3 connected to Cassandra, M4 connected to MangoDb and M5 connected to Oracle.

现在

步骤1:M1调用M2以更新mySql中的某些用户数据,并成功更新,然后最终得到M2的成功响应

Step-1: M1 making a call to M2 to update some user data in mySql and it updated successfully then finally it got success response from M2

步骤2:M1调用M3来更新Cassandra中的某些数据,并成功更新,最后得到M3的成功响应

Step-2: M1 making a call to M3 to update some data in Cassandra and it updated successfully then finally it got success response from M3

步骤3:M1调用M4以更新MangoDb中的某些数据,但由于某些数据库服务器问题或任何其他问题而失败.

Step-3: M1 making a call to M4 to update some data in MangoDb and it failed due to some DB server problem or any other problem.

在这里我的要求是,我想回滚以前的微服务(M2和M3)所发生的数据库更改

Here my requirement is, I want to rollback DB changes that happened to previous micro-services(M2 and M3)

要实现这种回滚方案,我们需要做些什么?

What should we need to do to achieve this kind of rollback scenario?

推荐答案

这是分布式事务的典型情况.无论您对数据库使用单独的技术还是在不同的服务器上使用相同的技术,无论执行哪种操作都是事务性的. 为了处理这种类型的事务的回滚,您不能在事务和回滚的数据库技术机制上进行中继.您必须自己做.

This is a typical case of distributed transaction. Regardless of the fact that you use separate technology for your database or the same on different server you perform an operation which is transactional. In order to handle a rollback on that type of transaction you can not relay on the database technology mechanism for transactions and rollbacks. You have to do it on your own.

传奇模式

用于微服务体系结构中的分布式事务方案的常见解决方案是 Saga模式 . 分布式sagas是一种用于管理如您所描述的方案中的故障的模式.

Common solution for distributed transaction scenarios in micro-service architecture is the Saga pattern. Distributed sagas is a pattern for managing failures in scenarios as the one that you have described.

佐贺是根据业务流程创建的,例如在网上商店购买产品".此过程可能涉及对多个微服务的多个操作. Saga将控制和管理该流程的执行,如果其中一个步骤失败,它将触发操作以还原失败操作之前执行的操作.

Saga are created based on business process for example "Buy a Product in online shop". This process can involve multiple actions on multiple micro-services. Saga will control and manage this process execution and if one of the steps fail it will trigger actions to revert the actions done before the failing action.

有多种方法可以实现Sagas.这取决于您的体系结构以及微服务之间的通信方式.您是否使用命令和/或事件?

There are multiple ways to implement sagas. It depends on your architecture and the way your micro-services communicate with each other. Do you use Commands and/or Events?

示例

在网上商店购买产品"业务流程.可以说,此业务流程由3个不同的微服务完成了3个简单的步骤:

"Buy a Product in online shop" business process. Lets say this business process has 3 simple steps done by 3 different micro-services:

  • 操作1-在产品-库存-微服务中储备产品
  • 操作2-验证微支付服务中的付款
  • 操作3-通过订单微服务订购产品

使用事件:

您可以发布事件以执行某些操作,如果某个操作失败,则可以发布该事件的还原(或删除)事件.对于上述业务流程,可以说1.动作成功,而2.动作失败.在这种情况下,为了回滚1.操作,您将发布一个事件,例如"RemoveReservationFromProduct",以删除保留并将状态恢复为该业务流程开始事务之前的状态.该事件将由事件处理程序处理,该事件处理程序将在数据库中恢复该状态.由于这是一个事件,因此您可以实现失败的重试机制,或者如果代码中存在某些错误,请稍后重新应用它.

You can publish events to perform some action(or actions) and if one of the action fails you can publish a revert(or delete) event for that event. For the above business process lets say the 1. Action succeeded and the 2. Action failed. In this case in order to rollback the 1. Action you would publish an event like "RemoveReservationFromProduct" in order to remove the reservation and revert the state back to the state as it was before the transaction for that Business process started. This event would be picked up by a event handler which would go and revert that state in your database. Since it is an event you can implement retry mechanism for failures or just reapply it later if there is some bug in the code.

使用命令:

如果您使用某种类型的rest api作为命令直接调用微服务,则可以运行一些delete或update端点以还原所做的更改.对于上述业务流程,可以说1.动作成功,而2.动作失败.在这种情况下,为了回滚1.操作,您将调用delete api来删除特定产品的预留,以便删除预留并将状态恢复为该业务流程开始交易之前的状态. .

If you have direct calls to your micro-services as commands using some kind of rest api you could run some delete or update endpoints to revert the changes that you have done. For the above business process lets say the 1. Action succeeded and the 2. Action failed. In this case in order to rollback the 1. Action you would call the delete api to delete the reservation for a particular product in order to remove the reservation and revert the state back to the state as it was before the transaction for that Business process started.

您可以查看示例如何实现Saga模式.

You can take a look at this example how to implement the Saga pattern.

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

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