Spring Cloud集成TX-LCN分布式事务框架

LCN 5.0.2版本,spring boot 2.1.8 数据库是mysql ,注册中心eureka,

注意:lcn 5.0版本以上的都需要spring boot 2.0以上的。

LCN分布式事务官网:http://www.txlcn.org/

springcloud的demo:https://github.com/codingapi/springcloud-lcn-demo

txlcn-tm 事务控制器服务端:https://github.com/codingapi/tx-lcn

一、配置事务控制器

txlcn-tm事务控制器服务端代码:

1、maven

<dependency>
			<groupId>com.codingapi.txlcn</groupId>
			<artifactId>txlcn-tm</artifactId>
			<version>5.0.2.RELEASE</version>
		</dependency>

其他的依赖如spring boot、mysql之类的,和一般的web工程一样的。

2、application.properties

server.port=7970

################################### mysql #########################################
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/springcloud_1?characterEncoding=utf-8&serverTimezone=UTC
spring.datasource.username=root
spring.datasource.password=123456

################################### spring cloud 配置 #########################################
################################### 配置eureak 客户端
#客户端注册进eureka服务列表内
eureka.client.service-url.defaultZone=http://localhost:7001/eureka
#自定义服务名称信息,就是在微服务列表哪里,点击它会显示info信息的名称,可以不设置
eureka.instance.instance-id=tx-manager
#微服务名称 必须要这么写
spring.application.name=tx-manager
#访问路径可以显示IP地址
eureka.instance.prefer-ip-address=true

################################### TX-LCN分布式事务框架 服务端的配置 #########################################
#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
#后台登陆密码  登陆页:http://127.0.0.1:7970/admin/index.html
tx-lcn.manager.admin-key=123456
#日志
logging.level.com.qw=debug
#redis 主机
spring.redis.host=192.168.194.135
#redis 端口
#spring.redis.port=6379
#redis 密码
#spring.redis.password=

3、启动类,加上两个注解

@EnableTransactionManagerServer //开启 TX-LCN分布式事务框架的 服务端
@EnableEurekaClient //本服务启动后会自动注册进eureka服务中

二、使用

业务场景:
1、用户下单购买商品。
2、订单微服务中,新增订单表信息
3、库存微服务中,库存商品数量表中商品数量减去订单中商品的数量。

订单微服务和库存微服务的配置如下:

maven:

<!--txlcn 分布式事务-->
		<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>

配置文件:

################################### TX-LCN分布式事务框架 客户端的配置 #########################################
tm.manager.url=http://127.0.0.1:8070/tx/manager/

启动类:

@EnableDistributedTransaction // 这是一个Lcn的客户端

都在方法的上面加入注解:

@LcnTransaction//分布式事务注解
@Transactional(rollbackFor = Exception.class)//本地事务注解,这个注解都要必须添加的。

上面的亲自测试有效果。

demo代码:

链接:https://pan.baidu.com/s/1r7mTg_nSYCNhhYJW_RwY7A

提取码:odsk

demo代码里面的spring cloud至配置了一个eureka,其他的没有配置,但是分布式事务还是可以用的。

注意:除了txlcn-tm事务控制服务端的spring boot是2.0以上的,订单服务和库存服务都不是。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值