分布式事务TX-LCN安装注册使用

TX-LCN

介绍

TX-LCN分布式事务框架,LCN并不生产事务,LCN只是本地事务的协调工,LCN是一个高性能的分布式事务框架,兼容dubbo、springcloud框架,支持RPC框架拓展,支持各种ORM框架、NoSQL、负载均衡、事务补偿

特性

1、一致性,通过TxManager协调控制与事务补偿机制确保数据一致性

2、易用性,仅需要在业务方法上添加@TxTransaction注解即可

3、高可用,项目模块不仅可高可用部署,事务协调器也可集群化部署

4、扩展性,支持各种RPC框架扩展,支持通讯协议与事务模式扩展

代码

参考文档:参考文档地址

TransactionManager

1.下载最新版本的TM项目,下载地址
在这里插入图片描述

2.找到tm项目的配置文件。
在这里插入图片描述
3.配置文件的配置如下:

spring.application.name=TransactionManager
server.port=7970

spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/tx-manager?characterEncoding=UTF-8&serverTimezone=Asia/Shanghai
spring.datasource.username=root
spring.datasource.password=root
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

# TM后台登陆密码
tx-lcn.manager.admin-key=123456

tx-lcn.manager.host=192.168.1.130
tx-lcn.manager.port=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

#redis 主机
spring.redis.host=192.168.1.130
#redis 端口
spring.redis.port=6379
#redis 密码
spring.redis.password=123456

4.启动项目,登录地址(http://localhost:7970/),输入密码即可访问,tm后台管理,效果如下:
在这里插入图片描述

Tx-Client

1.引入依赖

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

2.配置配置文件

#分布式事物配置(yml配置)
#tx-lcn:
#  ribbon:
#    loadbalancer:
#      dtx:
#        enabled: true
#  client:
#    manager-address: 192.168.1.130:8070
#  logger:
#    enabled: true
#    driver-class-name: ${spring.datasource.driver-class-name}
#    jdbc-url: ${spring.datasource.url}
#    username: ${spring.datasource.username}
#    password: ${spring.datasource.password}
#logging:
#  level:
#    com:
#      codingapi:
#        txlcn: debug  #分布式事物打开



#分布式事物配置(properties配置)
# 是否启动LCN负载均衡策略(优化选项,开启与否,功能不受影响)
tx-lcn.ribbon.loadbalancer.dtx.enabled=true
# 默认之配置为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 //启用分布式事物

//省略其他代码...
@EnableDistributedTransaction
public class MyspringbootApplication {
    public static void main(String[] args) {
        SpringApplication.run(MyspringbootApplication.class, args);
    }
}

4、在提交本地事务的地方添加@LcnTransaction
5、启动项目,启动成功可以在注册中心看到注册项目。
在这里插入图片描述
在这里插入图片描述
6.再注册一个项目就可以开始测试分布式事物了,加上注解@LcnTransaction即可,例如:

@Override
    @LcnTransaction//分布式事务
    @Transactional //本地事务
    public Result<Description> save(Description description) {
        UserVo userVo = new UserVo();
        userVo.setUsername("huanzi");
        userVo.setPassword("123");
        //调用springdatejpa服务保存userVo
        Result<UserVo>  result = myspringbootFeign.save(userVo);
        System.out.println(result);

        //myspringboot本地服务保存description
        Description save = descriptionRepository.save(description);
        System.out.println(save);
        
        //模拟发生异常
        throw new RuntimeException("business code error");
    }

总结

1、事务是应用程序中一系列严密的操作,所有操作必须成功完成,否则在每个操作中所作的所有更改都会被撤消。

2、事务应该具有4个属性:原子性、一致性、隔离性、持久性。这四个属性通常称为ACID特性

3、分布式事务是指事务的参与者、支持事务的服务器、资源服务器以及事务管理器分别位于不同的分布式系统的不同节点上,分布式事务的出现是为了保证分布式系统的事务一致性

4、TX-LCN定位于一款事务协调性框架,框架其本身并不操作事务,而是基于对事务的协调从而达到事务一致性的效果,兼容LCN、TCC、TXC模式

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

老徐··

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

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

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

打赏作者

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

抵扣说明:

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

余额充值