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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
TCC-Transaction是一个开源的TCC补偿性分布式事务框架。TCC是Try、Confirm、Cancel的缩写,表示事务的尝试、确认和取消阶段。TCC能够对分布式事务中的各个资源进行分别锁定、提交和释放。它的优点是能够实现严格一致性并且具有较短的执行时间和高实时性要求。同时,TCC也具有一定的缺点,例如对应用的侵入性较强,需要实现每个分支的try、confirm和cancel操作,实现难度较大。 关于Spring Cloud分布式事务和TCC,可以使用TCC-Transaction框架来实现。TCC-Transaction可以作为可靠性事件投递的替代品,并作为Spring Cloud Stream或Spring Cloud Bus的基础组件。此外,TCC还需要在事务管理器(协调器)节点上以高可用集群方式部署,并使用多数派算法来避免集群发生脑裂问题。 在实际应用中,TCC适用于一些需要严格一致性、执行时间短和实时性要求高的场景,例如红包和收付款业务。 更多关于TCC-Transaction框架的详细信息可以在其GitHub地址(https://github.com/changmingxie/tcc-transaction)和项目指南地址(https://github.com/changmingxie/tcc-transaction/wiki/使用指南1.2.x)中找到。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *3* [SpringCloud(6) 分布式事务【概念、常见框架选择 - tx-lcn】](https://blog.csdn.net/qq_38225558/article/details/86103229)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT0_1"}}] [.reference_item style="max-width: 50%"] - *2* [Spring Cloud综合实战 - 基于TCC补偿模式的分布式事务](https://blog.csdn.net/Solarison/article/details/68061157)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT0_1"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值