SpringCloud集成LCN分布式事务框架

一、整合tx-manager服务

1.下载tx-manager,并整合到自己项目

GitHub地址:https://github.com/ZiXinZhu/SpringCloud
将项目中的tx-manager模块拷贝到自己的项目中。
在这里插入图片描述

2.Mysql导入t_tx_exception.sql脚本

新建数据库名为:tx-manager,再执行tx-manager模块根目录下的脚本完成数据表创建。linux安装mysql点击这里!
在这里插入图片描述
在这里插入图片描述

3.修改application.properties配置文件(下面是完整配置)

在文件中将MySQL数据源和Redis配置修改成自己的,配置eureka注册中心地址。Linux安装Redis点击这里!

#应用名称
spring.application.name=tx-manager
server.port=7970
#DataSource配置
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://123.207.123.159:3306/tx-manager?characterEncoding=UTF-8
spring.datasource.username=root
spring.datasource.password=root
#redis 配置
spring.redis.host=120.24.102.150
spring.redis.port=6379
spring.redis.password=root

#注册中心地址
eureka.client.serviceUrl.defaultZone= http://localhost:8761/eureka/
# 注册名
eureka.instance.instance-id=${spring.cloud.client.ip-address}:${server.port}
# 设置注册到服务的为ip
eureka.instance.prefer-ip-address=true
#开启驼峰
mybatis.configuration.map-underscore-to-camel-case=true
#允许JDBC 生成主键。需要驱动器支持。如果设为了true,这个设置将强制使用被生成的主键,有一些驱动器不兼容不过仍然可以执行。  default:false
mybatis.configuration.use-generated-keys=true

# tx-manager ip(client请求ip)
tx-lcn.manager.host=127.0.0.1
# client 请求端口
tx-lcn.manager.port=8070
# 心跳检测时间 单位:ms
tx-lcn.manager.heart-time=12000
# 事务执行总时间
tx-lcn.manager.dtx-time=10000
# 参数延迟删除时间单位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

4.pom.xml文件可以直接使用不需要修改

二、配置client

1.配置provider服务(提供者)

1)pom.xml加上如下依赖

      <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>

		<!-- redis配置 -->
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-data-redis</artifactId>
		</dependency>

2)配置application.properties,将数据源和Redis改为自己的地址。

server.port=8762
spring.application.name=provider
eureka.client.service-url.defaultZone=http://localhost:8761/eureka/

spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://123.207.123.159:3307/hibernate?characterEncoding=utf8&useSSL=false&zeroDateTimeBehavior=convertToNull&autoReconnect=true
spring.datasource.username=root
spring.datasource.password=root
mybatis.configuration.map-underscore-to-camel-case=true

spring.datasource.hikari.minimum-idle=5
spring.datasource.hikari.maximum-pool-size=15
spring.datasource.hikari.auto-commit=true
spring.datasource.hikari.idle-timeout=30000
spring.datasource.hikari.pool-name=DatebookHikariCP
spring.datasource.hikari.max-lifetime=1800000
spring.datasource.hikari.connection-timeout=30000
spring.datasource.hikari.connection-test-query=SELECT 1

#redis 配置
spring.redis.host=120.24.102.150
spring.redis.port=6379
spring.redis.password=root

3)在启动类上添加:@EnableDistributedTransaction
在这里插入图片描述
4)在方法上添加: @LcnTransaction
在这里插入图片描述

2.配置consumer服务(消费者)

同提供者配置一样。

三、测试

1.启动测试tx-manager控制台

启动eureka,tx-manager,provider,consumer

地址:http://localhost:7970/admin/index.html#/
密码:codingapi
在这里插入图片描述

2.测试分布式事务回滚

GitHub地址:https://github.com/ZiXinZhu/SpringCloud

发布了55 篇原创文章 · 获赞 23 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/Z_Vivian/article/details/101211308
今日推荐