SpringCloud整合TX-LCN5.0.2使用LCN模式实现分布式事务

一、TM配置

pom.xml文件中添加依赖:

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

修改application.properties文件:

tx-lcn.manager.port=8070这个配置,用于配置LCN的监听端口,在下面配置客户端服务里需要用到这个端口号

server.port=7970
spring.application.name=tx-manager
#mysql
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://127.0.0.1:3306/tx-manager?characterEncoding=UTF-8
spring.datasource.username=root
spring.datasource.password=123456
spring.jpa.database-platform=org.hibernate.dialect.MySQL5InnoDBDialect
spring.jpa.hibernate.ddl-auto=update
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
#TM后台登陆密码,默认值为codingapi
tx-lcn.manager.admin-key=123456
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
spring.redis.host=127.0.0.1
spring.redis.port=6379
spring.redis.password=

修改启动类,添加@EnableTransactionManagerServer注解:

@EnableTransactionManagerServer
@SpringBootApplication
public class TxManagerApplication {

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

}

应用启动后,访问http://localhost:7970/,密码更改为123456,进入TxManager系统后台界面

在这里插入图片描述

二、TC配置

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>

修改application.properties文件:

tx-lcn.client.manager-address=127.0.0.1:8070
#是否启动LCN负载均衡策略(优化选项,开启与否,功能不受影响)
tx-lcn.ribbon.loadbalancer.dtx.enabled=true
#是否开启日志记录,当开启以后需要配置对应logger的数据库连接配置信息
tx-lcn.logger.enabled=true
tx-lcn.logger.driver-class-name=com.mysql.cj.jdbc.Driver
tx-lcn.logger.jdbc-url=jdbc:mysql://127.0.0.1:3306/tx-manager?characterEncoding=UTF-8
tx-lcn.logger.username=root
tx-lcn.logger.password=123456

修改启动类,添加@EnableDistributedTransaction注解:

@EnableDiscoveryClient
//开启分布式事务
@EnableDistributedTransaction
@SpringBootApplication
public class BankProviderApplication {

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

}

修改业务类,配置事务发起方:

@Service
public class AccountServiceImpl implements AccountService {
    @Autowired
    private AccountMapper accountMapper;

    @Autowired
    private BankProviderClient bankProviderClient;

    @Override
    @LcnTransaction(propagation = DTXPropagation.REQUIRED) //分布式事务注解
    @Transactional
    public Boolean transferAccounts(int money, String userFrom, String userTo) {
        Account myAccount = new Account();
        myAccount.setMoney(money);
        myAccount.setUser(userFrom);
        accountMapper.subMoney(myAccount);
        bankProviderClient.addMoney(money, userTo);//调用bank-provider服务
        int i = 1 / 0;
        return true;
    }
}

DTXPropagation源码:

public enum DTXPropagation {
    /**
     * 当前没有分布式事务,就创建。当前有分布式事务,就加入
     */
    REQUIRED,

    /**
     * 当前没有分布式事务,非分布式事务运行。当前有分布式事务,就加入
     */
    SUPPORTS;

所以一般事务发起方使用REQUIRED,事务参与方使用SUPPORTS

配置事务参与方:

@Service
public class AccountServiceImpl implements AccountService {
    @Autowired
    private AccountMapper accountMapper;

    @Override
    //事务的参与方
    @LcnTransaction(propagation = DTXPropagation.SUPPORTS)
    @Transactional
    public Boolean addMoney(int money, String user) {
        Account account = new Account();
        account.setMoney(money);
        account.setUser(user);
        accountMapper.addMoney(account);
        return true;
    }
}

LCN 5.0.2版本修改tx-client不配置tx-manager地址,改从redis主动拉取

添加依赖:

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

修改application.properties文件,添加redis相关配置:

#是否启动LCN负载均衡策略(优化选项,开启与否,功能不受影响)
tx-lcn.ribbon.loadbalancer.dtx.enabled=true
#是否开启日志记录,当开启以后需要配置对应logger的数据库连接配置信息
tx-lcn.logger.enabled=true
tx-lcn.logger.driver-class-name=${spring.datasource.driver-class-name}
tx-lcn.logger.jdbc-url=jdbc:mysql://127.0.0.1:3306/tx-manager?characterEncoding=UTF-8
tx-lcn.logger.username=root
tx-lcn.logger.password=123456
#redis
spring.redis.host=127.0.0.1
spring.redis.port=6379
spring.redis.password=

SpringCloud整合TX-LCN 5.0.2 Demo的代码已经上传到GitHub上了,地址:https://github.com/hxt970311/tx-lcn-demo

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 5
    评论
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 ]

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

邋遢的流浪剑客

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值