分布式事务框架TX-LCN

TX-LCN 主要有两个模块:

Tx-Client(TC):每个微服务都可以是一个TC,只需引入TC的依赖

Tx-Manager(TM):TM是独立的微服务,注册到注册中心

 

搭建步骤:

一 Tx-Manager服务搭建

  1. 创建MySQL数据库, 名称为: tx-manager
    CREATE DATABASE IF NOT EXISTS  `tx-manager` DEFAULT CHARSET utf8 COLLATE utf8_general_ci;
    USE `tx-manager`;
    
    SET NAMES utf8mb4;
    SET FOREIGN_KEY_CHECKS = 0;
    
    DROP TABLE IF EXISTS `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,
      `ex_state` tinyint(4) NULL DEFAULT NULL COMMENT '0 待处理 1已处理',
      `remark` varchar(10240) NULL DEFAULT NULL COMMENT '备注',
      `create_time` datetime(0) NULL DEFAULT NULL,
      PRIMARY KEY (`id`) USING BTREE
    ) ENGINE = InnoDB AUTO_INCREMENT = 967 CHARACTER SET = utf8mb4 COLLATE = utf8mb4_general_ci ROW_FORMAT = Dynamic;
    
    SET FOREIGN_KEY_CHECKS = 1;
    

     

  2. 创建TM微服务,使用SpringBoot创建工程

  3. 添加依赖

            <!-- text报错,添加一下依赖-->
    		<dependency>
    			<groupId>org.springframework.boot</groupId>
    			<artifactId>spring-boot-starter-test</artifactId>
    			<scope>test</scope>
    		</dependency>
    		<!-- 参照例子引入需要的依赖jar -->
    		<dependency>
    			<groupId>com.codingapi.txlcn</groupId>
    			<artifactId>txlcn-tm</artifactId>
    			<version>5.0.2.RELEASE</version>
    		</dependency>
    

     

  4. 修改application.properties文件,Tx-Manager需要中间件redis的支持

    spring.application.name=TransactionManager
    server.port=7970
    eureka.client.serviceUrl.defaultZone=http://127.0.0.1:5001/eureka/eureka/
    
    spring.datasource.driver-class-name=com.mysql.jdbc.Driver
    spring.datasource.url=jdbc:mysql://127.0.0.1:3306/tx-manager?characterEncoding=UTF-8
    spring.datasource.username=root
    spring.datasource.password=root
    spring.jpa.database-platform=org.hibernate.dialect.MySQL5InnoDBDialect
    spring.jpa.hibernate.ddl-auto=update
    logging.file=tx-manage.log
    
    mybatis.configuration.map-underscore-to-camel-case=true
    mybatis.configuration.use-generated-keys=true
    
    
    # TM监听IP. 默认为 127.0.0.1
    tx-lcn.manager.host=127.0.0.1
    
    # TM监听Socket端口. 默认为 ${server.port} - 100
    tx-lcn.manager.port=8070
    
    # 心跳检测时间(ms). 默认为 300000
    tx-lcn.manager.heart-time=300000
    
    # 分布式事务执行总时间(ms). 默认为36000
    tx-lcn.manager.dtx-time=8000
    
    # 参数延迟删除时间单位ms  默认为dtx-time值
    tx-lcn.message.netty.attr-delay-time=${tx-lcn.manager.dtx-time}
    
    # 事务处理并发等级. 默认为机器逻辑核心数5倍
    tx-lcn.manager.concurrent-level=160
    
    # TM后台登陆密码,默认值为codingapi
    tx-lcn.manager.admin-key=123456
    
    # 分布式事务锁超时时间 默认为-1,当-1时会用tx-lcn.manager.dtx-time的时间
    tx-lcn.manager.dtx-lock-time=${tx-lcn.manager.dtx-time}
    
    # 雪花算法的sequence位长度,默认为12位.
    tx-lcn.manager.seq-len=12
    
    # 异常回调开关。开启时请制定ex-url
    tx-lcn.manager.ex-url-enabled=false
    
    logging.level.com.codingapi=debug
    
    # 开启日志,默认为false
    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}
    
    # redis 的设置信息. 线上请用Redis Cluster
    spring.redis.cluster.nodes=127.0.0.1:7001,127.0.0.1:7002,127.0.0.1:7003,127.0.0.1:7004
    spring.redis.password=123456
    #最大重定向次数 不小于主节点数
    spring.redis.cluster.max-redirects=4
    jedis.pool.config.maxTotal=8
    jedis.pool.config.maxWaitMillis=5000
    jedis.pool.config.maxIdle=1
    # 连接超时时间(毫秒)
    spring.redis.timeout=3000ms
    # Redis数据库索引(默认为0)
    spring.redis.database=0
    
    #单节点redis配置
    #spring.redis.host=127.0.0.1
    #spring.redis.port=6379
    #spring.redis.password=

    5.在启动类添加注解 @EnableTransactionManagerServer

    import com.codingapi.txlcn.tm.config.EnableTransactionManagerServer;
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
    
    @SpringBootApplication
    @EnableDiscoveryClient
    @EnableTransactionManagerServer
    public class TxManageApplication {
    
    	public static void main(String[] args) {
    		SpringApplication.run(TxManageApplication.class, args);
    	}
    
    }
    

     

 二  Tx-Client微服务改造

       1.引入依赖

        <!-- springcloud 分布式事物 -->
		<dependency>
			<groupId>com.codingapi.txlcn</groupId>
			<artifactId>txlcn-tc</artifactId>
			<version>5.0.2.RELEASE</version>
			<exclusions>
				<exclusion>
					<groupId>com.google.guava</groupId>
					<artifactId>guava</artifactId>
				</exclusion>
			</exclusions>
		</dependency>
		<dependency>
			<groupId>com.codingapi.txlcn</groupId>
			<artifactId>txlcn-txmsg-netty</artifactId>
			<version>5.0.2.RELEASE</version>
			<exclusions>
				<exclusion>
					<groupId>com.google.guava</groupId>
					<artifactId>guava</artifactId>
				</exclusion>
			</exclusions>
		</dependency>
		<dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-jdbc</artifactId>
        </dependency>

     2.配置文件添加TM地址、监听端口,如果TM是默认8070端口,且跟TC部署在同一台机器,可以忽略这个配置,并且开启日志,开发阶段最好开启日志,并设置为debug等级,这样方便追踪排查问题

# 是否启动LCN负载均衡策略(优化选项,开启与否,功能不受影响)
tx-lcn.ribbon.loadbalancer.dtx.enabled=true

# tx-manager 的配置地址,可以指定TM集群中的任何一个或多个地址
# tx-manager 下集群策略,每个TC都会从始至终<断线重连>与TM集群保持集群大小个连接。
# TM方,每有TM进入集群,会找到所有TC并通知其与新TM建立连接。
# TC方,启动时按配置与集群建立连接,成功后,会再与集群协商,查询集群大小并保持与所有TM的连接
tx-lcn.client.manager-address=127.0.0.1:8070

# 开启日志,默认为false
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

    3.在启动类上使用 @EnableDistributedTransaction

    4.在事务的发起方法上添加@LcnTransaction分布式事务注解,在被调用的服务方法上添加@LcnTransaction或@TxcTransaction或@TccTransaction

     PS:@LcnTransaction,@TxcTransaction,@TccTransaction的target是在方法上的

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值