天天看点

Tx-LCN SpringCloud分布式事务处理

TX-LCN官网http://www.txlcn.org/zh-cn/index.html

环境依赖:springboot2.0以上+springcloud+eureka+redis3.2+mysql5.6+feign

1.先新建一个springboot模块

2.在pom文件中添加maven依赖:

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

3.在启动类上添加注解

@EnableTransactionManagerServer

@SpringBootApplication
@EnableTransactionManagerServer
public class TransactionManagerApplication {

  public static void main(String[] args) {
      SpringApplication.run(TransactionManagerApplication.class, args);
  }

}
           

4.填写配置

填写配置前,先在mysql上创建一个数据库,名称为: tx-manager

创建数据表:

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,
  `remark` varchar(4096) NULL DEFAULT  NULL,
  `ex_state` tinyint(4) NULL DEFAULT NULL COMMENT '0 未解决 1已解决',
  `create_time` datetime(0) NULL DEFAULT NULL,
  PRIMARY KEY (`id`) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 1 CHARACTER SET = utf8mb4 COLLATE = utf8mb4_general_ci ROW_FORMAT = Dynamic;
           

根据你自己环境配置,主要是数据库地址和注册中心的地址

spring.application.name=tx-manager
server.port=7970

spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/tx-manager?characterEncoding=UTF-8
spring.datasource.username=root
spring.datasource.password=root
#指定注册中心地址
eureka.client.serviceUrl.defaultZone= http://localhost:8761/eureka/
eureka.instance.instance-id=${spring.cloud.client.ip-address}:${server.port}
eureka.instance.prefer-ip-address=true

mybatis.configuration.map-underscore-to-camel-case=true
mybatis.configuration.use-generated-keys=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
logging.level.com.codingapi=debug
#redis 主机
spring.redis.host=127.0.0.1
#redis 端口
spring.redis.port=6379
#redis 密码
spring.redis.password=
           

5.到此你的tm已经配置完成了,启动这个服务,打开浏览器访问http://localhost:7970/admin/index.html#/login

你可以看到tx-manager的后台管理,类似注册中心那种,可以看到你有哪些服务需要做事务处理的。登录的默认密码为codingapi

Tx-LCN SpringCloud分布式事务处理

已注册的TC就是你要做事务的客服端,这里称为tc-client

六、配置tc-client

就是你要做事务的哪些微服务

在你要做事务处理的每一个tc-client中添加如下maven依赖

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

然后在启动类上加入注解

@EnableDistributedTransaction

@SpringBootApplication
@EnableDistributedTransaction
public class DemoAApplication {

    public static void main(String[] args) {
        SpringApplication.run(DemoDubboClientApplication.class, args);
    }

}
           

然后在你需要做的事务事务的方法上加入注解@LcnTransaction //分布式事务注解

比如:这里两个服务,A服务给B服务转钱,A服务减一百,B服务加一百。这个处理就需要做事务处理了,以保证数据的一致性。

那在A服务Service层上加入注解@LcnTransaction,A的服务层去调用B服务,在B服务中的service成也需要加上@LcnTransaction注解,在每个service方法上也要加上本地事务注解@Transaction

在你处理的整个业务的时候,如果需要事务处理,那你就得在再涉及到你这个业务所有服务的所对应的处理的service层加上注解。

最后说一下,在每一个tc-client配置文件中需要制定tx-manager的地址,因为我用的是默认配置,所以没有写。若有修改tx-manager 在tc-client中添加如下配置

默认之配置为TM的本机默认端口

tx-lcn.client.manager-address=127.0.0.1:8070

七、github地址:https://github.com/zhangjunli16811/tx-lcn-springcloud-transaction

觉得不错的小伙伴可以去star一下哦。

如果通过feign 调用时,feign注解中加了fallbackFactory =xxx.class的话;在Fallback方法里面加上这个

DTXUserControls.rollbackGroup(TracingContext.tracing().groupId());

继续阅读