TX-LCN由两大模块组成, TxClient、TxManager,TxClient作为模块的依赖框架,提供TX-LCN的标准支持,TxManager作为分布式事务的控制放。事务发起方或者参与反都由TxClient端来控制。
核心步骤
-
创建事务组
是指在事务发起方开始执行业务代码之前先调用TxManager创建事务组对象,然后拿到事务标示GroupId的过程。 -
加入事务组
添加事务组是指参与方在执行完业务方法以后,将该模块的事务信息通知给TxManager的操作。 -
通知事务组
是指在发起方执行完业务代码以后,将发起方执行结果状态通知给TxManager,TxManager将根据事务最终状态和事务组的信息来通知相应的参与模块提交或回滚事务,并返回结果给事务发起方。
SpringServiceA (发起方 | LCN模式)
SpringServiceB (参与方 | TXC模式)
SpringServiceC (参与方 | TCC模式)
sql:
Create Table |
CREATE TABLE `t_tx_exception` ( |
pom:
<dependency> <groupId>com.codingapi.txlcn</groupId> <artifactId>txlcn-tm</artifactId> <version>5.0.2.RELEASE</version> </dependency>
配置文件:
spring.application.name=tx-manager server.port=7970 [email protected]@ spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver spring.jpa.database-platform=org.hibernate.dialect.MySQL5InnoDBDialect spring.jpa.hibernate.ddl-auto=update # TM后台登陆密码 tx-lcn.manager.admin-key=123456
server.port=7970 spring.datasource.url=jdbc:mysql://localhost:3306/tx-manager?serverTimezone=Asia/Shanghai&useUnicode=true&characterEncoding=utf8&useSSL=false spring.datasource.username=root spring.datasource.password=root tx-lcn.manager.host=127.0.0.1 tx-lcn.manager.port=8070 # 开启日志,默认为false tx-lcn.logger.enabled=true tx-lcn.logger.driver-class-name=${spring.datasource.driver-class-name} tx-lcn.logger.jdbc-url=${spring.datasource.url} tx-lcn.logger.username=${spring.datasource.username} tx-lcn.logger.password=${spring.datasource.password} logging.level.com.codingapi.txlcn=DEBUG #redis 主机 spring.redis.host=127.0.0.1 spring.redis.port=6379 spring.redis.password=123456
Application:
@SpringBootApplication @EnableTransactionManagerServer public class TxManagerApplication { public static void main(String[] args) { SpringApplication.run(TxManagerApplication.class, args); } }