SpringCloud-LCN 分布式事务

boot版本:2.0.6
cloud版本:Finchley.SR2
lcn版本:5.0.2

 

TX-LCN的核心控制流程(官网)

官网地址:https://www.codingapi.com/docs/txlcn-lesson02/

 

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

创建项目 yu-txlcn-tm,

pom.xml

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

更新配置文件bootstrap.yml

tx-lcn:
  client:
    manager-address: localhost:8070
  ribbon:
    loadbalancer:
      dtx:
        enabled: true
  logger:
    enabled: true
    driver-class-name: com.mysql.jdbc.Driver
    jdbc-url: jdbc:mysql://localhost:3306/yq?useUnicode=true&characterEncoding=UTF-8&useLegacyDatetimeCode=false&serverTimezone=Asia/Shanghai&useSSL=false&zeroDateTimeBehavior=convertToNull&allowMultiQueries=true
    username: root
    password: root

yu-txlcn-tm 结构体

application.properties

spring.application.name=yu-txlcn-tm
server.port=7970

#JDBC 数据库配置
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/yq?useUnicode=true&characterEncoding=UTF-8&useLegacyDatetimeCode=false&serverTimezone=Asia/Shanghai&useSSL=false&zeroDateTimeBehavior=convertToNull&allowMultiQueries=true
spring.datasource.username=root
spring.datasource.password=root

#数据库方言
spring.jpa.database-platform=org.hibernate.dialect.MySQL5InnoDBDialect

# 第一次运行可以设置为: create, 为TM创建持久化数据库表
#spring.jpa.hibernate.ddl-auto=validate
spring.jpa.hibernate.ddl-auto=update

# 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=codingapi

# 分布式事务锁超时时间 默认为-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

# 事务异常通知(任何http协议地址。未指定协议时,为TM提供内置功能接口)。默认是邮件通知
tx-lcn.manager.ex-url=/provider/email-to/xxxxxx@qq.com



# 开启日志,默认为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.host=localhost
spring.redis.port=6379
spring.redis.password=redis

使用之前的 yu-user-service 与 yu-third-service 测试

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>
        <!-- 分布式事物 结束 -->

修改启动类 及 调用接口(这里只用user服务做示范,third也需要添加注解的)

/**
 * @author Yuqiang
 */
@SpringBootApplication
@EnableFeignClients(basePackages = "com.crayon.*") //激活feign
@EnableCircuitBreaker//激活 hystrix
@EnableDistributedTransaction//激活 分布式事务lcn
public class YuUserServiceApplication {

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

}

调用接口

//user服务

    @Override
    @LcnTransaction
    @Transactional
    public Message insertName(String name) {
        Message  message =userInvokeService.getUser("嗯嗯");
        log.info("调用third:  ", message.toString());
        UserModel userModel = new UserModel();
        userModel.setMobile("13071813602");
        userModel.setName("小强");
        int count = userDao.insertUser(userModel);
        log.info("count: " + count);
        int a = 1/0;//故意出错
        return new Message("成功", ErrorType.SUCCESS);
    }

//third服务
    @Override
    @LcnTransaction
    @Transactional
    public int insertThirdRecord(ThirdRecordModel model) {
        return thirdRecordDao.insertThirdRecord(model);
    }

//测试事务  user,third 都插入完数据后 user异常了

user模块

third模块

但是 数据库 并没有插入数据 ---> 表示事务已生效(回滚数据)

 

PS:

 @LcnTransaction,这个注解会导致数据库连接被分布式事物接管,进而导致连接延迟释放,接口响应时间变长,所以使用时请注意

 

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值