跨Spring Web应用程序的分布式事务 [英] Distributed transactions across Spring web applications

查看:200
本文介绍了跨Spring Web应用程序的分布式事务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

想象一下一个Java生态系统,其中三个单独的Spring Web应用程序在不同的JVM和不同的机器上运行(不涉及应用程序服务器,只是简单的servlet容器).这些应用程序中有两个正在使用通过JPA访问的自己的数据库.现在,第三个应用程序(协调器)向外界提供服务,并且一些服务功能执行远程操作,这需要其他两个应用程序以事务方式参与,这意味着,如果其中一个应用程序无法在数据库,另一个也应回滚.问题是:使用Spring如何实现?

Imagine a Java ecosystem where three separate Spring web application is running in separate JVMs and on separate machines (no application server involved, just simple servlet containers). Two of these applications are using their own database accessed using JPA. Now the third application (a coordinator) provides services to the outside world and some service function executes remote operations which requires participation from the other two apps in a transactional manner, which means that if one of the applications fails to do the data manipulation in the database, the other should be rolled back as well. The problem is: how can this be achieved using Spring?

当前,我们正在使用REST在应用程序之间进行通信.显然,即使正在努力做到这一点.

Currently we are using REST to communicate between the applications. Clearly this cannot support transactions, even though there are efforts to make this happen.

我发现JTA能够组织全球交易. JTA涉及创建XAResource实例,这些实例正在参与全局管理的事务.如果我正确理解,那么这些XAResource实例可以驻留在单独的JVM上.资源的初始化,提交和回滚是通过JMS通信进行的,这意味着需要消息代理在参与者之间传输消息.有各种各样的JTA实现,我发现Atomikos似乎是最常用的.

I've found JTA which is capable of organizing global transactions. JTA involves creating XAResource instances which are participating in the globally managed transactions. If i understood correctly, these XAResource instance can reside on separate JVMs. Initialization, commit and rollback of resources happens via JMS communication which means it requires a message broker to transfer messages between participants. There are various JTA implementation exists, I've found Atomikos which seems to be the most used.

现在我看不到的是,如果我在每个应用程序端都有一个Spring应用程序,这一切是怎么出现的.我还没有找到通过网络执行JTA的任何示例项目.我也不理解XAResources代表什么.如果我使用JPA,并说我在存储用户余额的应用程序中有一个Account对象,并且我必须从协调器中减少余额,我是否应该创建一个允许减少余额的XAResource实现?还是XAResource是由诸如JDBC驱动程序或Spring Data JPA之类的较低层次的东西实现的?在后一种情况下,我如何为事务协调器提供高级CRUD操作.

Now the thing i don't see is how this all comes up if i have a Spring application on each application side. I've not found any example projects yet which is doing JTA over a network. Also i don't undertstand what are XAResources representing. If i use JPA, and say i have a Account object in an application which stores a user's balance, and i have to decrease the balance from the coordinator, should i create an XAResource implementation which allows decreasing the balance? Or XAResource is implemented by a lower level thing like the JDBC driver or Spring Data JPA? In the latter case how can i provide high level CRUD operations for the transaction coordinator.

推荐答案

XAResource是较低级别的API.您可以为协调员编写自己的文档,但是我认为这不是必需的.而是在协调器上使用JMS + JTA,在应用程序服务器上使用JTA.

XAResource is a lower level API. You could write your own for the coordinator, but I think that isn't necesary. Instead, leverage JMS + JTA on the coordinator and JTA on the app servers.

在通常情况下,您会这样:

In the normal case, you'd have this:

  1. 协调员收到请求并开始JTA交易
  2. 协调器通过JMS调用应用程序1
    • 应用1收到JMS消息
    • App 1使用JTA调用DB 1
  1. Coordinator receives request and starts JTA transaction
  2. Coordinator calls app 1 over JMS
    • App 1 receives JMS message
    • App 1 calls DB 1 using JTA
  • 应用2收到JVM消息
  • App 2使用JTA调用DB 2

请注意,JTA用于所有事务-这将是在所有服务器之间共享的全局TX.如果这些步骤中的任何一个失败,那么它们将被回滚.

Note that JTA is used for all the transactions - this will be a global TX that's shared across all the servers. If any of these steps fail, then they will be rolled back.

所有设置完成后,Spring应该能够使此透明.只要确保您的DAO&服务呼叫是事务性的.需要配置Atomikos,以便每个服务器使用相同的JTA tx管理器.

Spring should be able to make this transparent once you get it all set up. Just make sure your DAO & service calls are transactional. Atomikos will need to be configured so that each server uses the same JTA tx manager.

这篇关于跨Spring Web应用程序的分布式事务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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