SpringBoot2.X + Consul+ Mybatis + Redis + LCN5.0.2 分布式事务解决方案demo

本篇以SpringBoot2.X,Mybatis为框架,结合LCN实现简单Demo,作为分布式事务解决方案

TM事务管理器服务端配置:

1.下载LCN源码: git clone git@github.com:codingapi/tx-lcn.git
2.修改 txlcn-tm\src\main\resources下的application.properties文件,修改后如下:

spring.application.name=TransactionManager
server.port=7970
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://192.168.10.101:3306/tx-manager?characterEncoding=UTF-8
spring.datasource.username=root
spring.datasource.password=Root!!123456
spring.jpa.database-platform=org.hibernate.dialect.MySQL5InnoDBDialect
spring.jpa.hibernate.ddl-auto=update

mybatis.configuration.map-underscore-to-camel-case=true
mybatis.configuration.use-generated-keys=true

#tx-lcn.logger.enabled=true
# TxManager Host Ip
#tx-lcn.manager.host=127.0.0.1
# TxClient连接请求端口
#tx-lcn.manager.port=8070
# 心跳检测时间(ms)
#tx-lcn.manager.heart-time=15000
# 分布式事务执行总时间
#tx-lcn.manager.dtx-time=30000
#参数延迟删除时间单位ms
#tx-lcn.message.netty.attr-delay-time=10000
#tx-lcn.manager.concurrent-level=128
# 开启日志
#tx-lcn.logger.enabled=true
#logging.level.com.codingapi=debug
#redis 主机
spring.redis.host=192.168.10.101
##redis 端口
spring.redis.port=6379
##redis 密码
spring.redis.password=123456

3.修改txlcn-tm模块的pom.xml文件,把docker 构建注释掉:
在这里插入图片描述
去掉**deploy **下面的注释:
在这里插入图片描述
3.新建MySQL数据库tx-manager,新建表t_tx_exception,建表语句如下:

CREATE TABLE `t_tx_exception`  (
  `id` bigint(20) NOT NULL AUTO_INCREMENT,
  `group_id` varchar(64) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL,
  `unit_id` varchar(32) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL,
  `mod_id` varchar(128) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL,
  `transaction_state` tinyint(4) NULL DEFAULT NULL,
  `registrar` tinyint(4) NULL DEFAULT NULL,
  `remark` varchar(4096) NULL DEFAULT  NULL,
  `ex_state` tinyint(4) NULL DEFAULT NULL COMMENT '0 未解决 1已解决',
  `create_time` datetime(0) NULL DEFAULT NULL,
  PRIMARY KEY (`id`) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 1 CHARACTER SET = utf8mb4 COLLATE = utf8mb4_general_ci ROW_FORMAT = Dynamic;

4.下载consul,选择Windows-64bit,LCN默认以此作为服务注册中心,可以直接采用缺省配置,而无需额外配置,在项目启动前要先启动consul.
5.启动TMApplication,访问 http://localhost:7970/admin/index.html#/
默认登录密码为 codingapi,登陆后如下:
在这里插入图片描述
上述已经完成TM服务端配置,下面进行客户端配置。

TM事务管理器客户端配置:

1.新建springboot maven项目bank-demo,添加子模块:bank-a ,bank-b.项目list如下:
在这里插入图片描述
项目Demo GitHub地址:https://github.com/yjxwylb/bank-demo
注意事项
1.父pom.xml中添加如下依赖:注意版本号一定要和txlcn-tm中版本相同。

		<dependency>
			<groupId>com.codingapi.txlcn</groupId>
			<artifactId>txlcn-tc</artifactId>
			<version>5.0.2.RELEASE</version>
		</dependency>
		<dependency>
			<groupId>com.codingapi.txlcn</groupId>
			<artifactId>txlcn-txmsg-netty</artifactId>
			<version>5.0.2.RELEASE</version>
		</dependency>

2.启动类上都要加上@EnableDistributedTransaction注解
在这里插入图片描述
3.调用方法和被调用方法上都要添加注解@LcnTransaction

原理简介

官网
LCN框架不会创建事务,而是作为事务的协调者,对分布式的各个事务统一协调,从而达到一致性。
分为三个步骤:
①.创建事务组
请求进来,事务发起方在执行业务之前,调用TxManager创建事务组对象,得到事务组id(groupId),根据隔离性,每次请求都会生成一个新的事务组Id,后续的所有操作都会带着返回的groupId标识。
②.添加事务组
参与方模块代码执行完后,把当前模块事务添加到事务组,会采用假关闭,而当前模块不会提交事务。
③.关闭事务组
发起方执行完业务代码后(执行过程中无异常抛出),会将发起方执行结果通知给TxManager。在执行完关闭事务组的方法以后,TxManager将根据事务组信息来通知相应的参与模块提交或回滚事务。

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值