如何回滚分布式事务? [英] How to rollback distributed transactions?

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

问题描述

我有三个不同的Spring Boot项目,它们具有分开的数据库,例如帐户剩余,付款剩余,网关剩余.

I have three different Spring boot Projects with separated databases e.g account-rest, payment-rest, gateway-rest.

  1. 帐户休息:创建一个新帐户
  2. 剩余付款:创建新的付款
  3. gateway-rest:呼叫其他端点

在网关休息处,有一个终结点叫另外两个终结点.

at gateway-rest there is an endpoint which calls the other two endpoints.

@GetMapping("/gateway-api")    
@org.springframework.transaction.annotation.Transactional(rollbackFor = RuntimeException.class)
public String getApi()
{
    String accountId = restTemplate.getForObject("http://localhost:8686/account", String.class); 
    restTemplate.getForObject("http://localhost:8585/payment?accid="+accountId, String.class);
    throw new RuntimeException("rollback everything");      
}

当我在网关或任何其他端点上引发异常时,我想回滚事务并还原所有内容.

I want to rollback transactions and revert everything when I throw exception at gateway or anyother endpoints.

我该怎么做?

推荐答案

不可能通过rest或类似的东西回滚外部依赖项. 唯一可以做的就是补偿误差,可以使用SAGA之类的模式

It is impossible rollback external dependencies accessible via rest or something like that. The only think that you can do is compensate errors, you can use pattern like SAGA

我希望能为您提供帮助

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

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