springboot+tx-lcn实现分布式事务
2020-06-05 07:41
169 查看
tx-lcn分布式事务
-
TX-LCN 结构分析
[ol] 搭建tx-manager事务控制服务器
tx-lcn的整体结构类型eureka 分为一个事务控制端服务器 tx-manager(类似eureka注册中心)和事务发起端 tx-client(类似各个注册到eureka注册中心的服务)
创建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 DEFAULT NULL, `unit_id` varchar(32) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci DEFAULT NULL, `mod_id` varchar(128) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci DEFAULT NULL, `transaction_state` int(4) DEFAULT NULL, `registrar` smallint(4) DEFAULT NULL, `remark` varchar(4096) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci DEFAULT NULL, `ex_state` smallint(4) DEFAULT NULL COMMENT '0 未解决 1已解决', `create_time` datetime DEFAULT NULL, PRIMARY KEY (`id`) USING BTREE ) ENGINE=InnoDB DEFAULT CHARSET=utf8 ROW_FORMAT=DYNAMIC;
- 新建springboot项目tx-manager
添加依赖
<dependency> <groupId>com.codingapi.txlcn</groupId> <artifactId>txlcn-tm</artifactId> <version>5.0.2.RELEASE</version> </dependency>
修改配置文件
spring.application.name=pic-manage #事务控制端服务器启动ip server.port=7970 spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver spring.datasource.url=jdbc:mysql://127.0.0.1:3307/tx-manager?characterEncoding=UTF-8&serverTimezone=Asia/Shanghai spring.datasource.username=root spring.datasource.password=root spring.jpa.database-platform=org.hibernate.dialect.MySQL5InnoDBDialect spring.jpa.hibernate.ddl-auto=validate #TM后台登陆密码 tx-lcn.manager.admin-key=123456 #事务发起端连接该事务控制端服务器ip #若事务发起端与事务控制端服务器不在同一主键不能使用127.0.0.1否则无法连接上建议使用对应主机ip tx-lcn.manager.host=127.0.0.1 #事务发起端连接该事务控制端服务器连接端口 tx-lcn.manager.port=8070 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 #redis端口 spring.redis.port=6379 #redis密码 spring.redis.password=
修改启动类添加 @EnableTransactionManagerServer注解
@SpringBootApplication @EnableTransactionManagerServer public class PicTxManageApplication { public static void main(String[] args) { SpringApplication.run(PicTxManageApplication.class, args); } }
登陆TM后台页面: http://服务地址:端口/admin/index.html#/login
此时表示事务管理服务已经启动成功
- tx-client 服务端配置(既需要参与分布式事务的服务配置)
添加分布式事务依赖
<dependency> <groupId>com.codingapi.txlcn</groupId> <artifactId>txlcn-txmsg-netty</artifactId> <version>5.0.2.RELEASE</version> </dependency>
注意如果有swagger依赖一定要在swagger下面,否则会报依赖冲突;
在启动类加上分布式事务注解 @EnableDistributedTransaction
@SpringBootApplication @EnableEurekaClient @EnableDistributedTransaction public class TestRoleApplication { public static void main(String[] args) { SpringApplication.run(TestRoleApplication.class, args); } }
配置文件添加连接事务控制端服务器
tx-lcn: # 是否开启负载均衡 ribbon: loadbalancer: dtx.enabled: true # 若不配置默认为127.0.0.1:8070 client: resource-order: 0 manager-address: 192.168.188.94:8070
在需要做分布式事务处理的方法上添加本地事务与分布式事务注解
@LcnTransaction
@Transactional(rollbackFor = Exception.class)
@LcnTransaction @Transactional(rollbackFor = Exception.class) @Override public boolean userDeleteRole(UserVo userVo) { LogSystem logSystem = new LogSystem(); logSystem.setIp("测试"); logSystem.setUserName("测试"); logSystem.setUserId("测试"); userLogApi.addSystemLog(logSystem); return true; }
启动服务即可
到tm服务器查看服务是否注册上去
若能找到对应服务既说明配置成功
注意:参与分布式事务的服务与方法均要配置
相关文章推荐
- Springboot-dubbo-fescar 阿里分布式事务的实现方法
- spring boot+Mybatis+automatic+jta实现分布式事务
- 基于SpringBoot 1.5.9+SpringCloud E版+RabbitMq+redis实现分布式事务
- springboot结合atomikos+mybaits实现分布式事务及读写分离【最简配置】
- Springboot+atomikos+jta实现分布式事务统一管理
- SpringCloud系列——TX-LCN分布式事务管理
- 超简单 springboot+ mybatisplus+atomikos实现多数据源分布式事务
- TX-LCN分布式事务框架
- Spring Boot分布式系统实践【扩展1】shiro+redis实现session共享、simplesession反序列化失败的问题定位及反思改进
- Spring事务隔离级别与传播机制详解,spring+mybatis+atomikos实现分布式事务管理
- 一个简单的Spring Boot框架中的多数据源和分布式事务例子
- XA式、非XA式Spring分布式事务的实现
- 分布式事务JTA实现Atomikos与Spring集成实践
- [redis分布式锁]redisson分布式锁的实现及spring-boot-starter封装
- Spring Boot2.0之多数据源分布式事务问题
- 如何使用MongoDB+Springboot实现分布式ID
- 分布式事务JTA实现Atomikos与Spring集成实践
- 如何使用MongoDB+Springboot实现分布式ID?
- spring项目加入jta分布式事务的实现方式: Atomikos
- 如何使用MongoDB+Springboot实现分布式ID?