SpringCloud - LCN分布式事务框架

官网:http://www.txlcn.org/

LCN原理:https://github.com/codingapi/tx-lcn/wiki/LCN%E5%8E%9F%E7%90%86

入门测试:

1.启动redis服务

注意:自己的是否配置过密码,否则会导致最后测试失败

​​​​2.启动eureka服务

 

3.启动tx-manager

1.下载资源:https://github.com/codingapi/tx-lcn

2.先配置:

 

3.然后启动 - cmd执行命令  java -jar tx-manager-4.1.0.jar

4.测试:http://127.0.0.1:8899/

4.配置client客户端

Tx-client:

<lcn.last.version>4.1.0</lcn.last.version>
 
<dependency>
    <groupId>com.codingapi</groupId>
    <artifactId>tx-plugins-db</artifactId>
    <version>{lcn.last.version}</version>
</dependency>
<dependency>
   <groupId>com.codingapi</groupId>
   <artifactId>transaction-springcloud</artifactId>
   <version>{lcn.last.version}</version>
</dependency>

配置:

也可看官方案例 - 两个client配置

演示:可下载官方给出的springcloud LCN分布式事务示例demo --> https://github.com/codingapi/springcloud-lcn-demo

①  

Controller:

service:

②中可看具体案例:

然后数据库建库建表

  

最后运行测试:启动 

访问http://127.0.0.1:7001/   

http://127.0.0.1:8083/demo/save 数据库会多出2条数据

放开demo1中异常  回滚数据没有数据添加

如果没有异常数据库两条数据添加(test和test1库中各1条)

  

demo2

①分布式事务起始服务方

@Transactional
@TxTransaction(isStart = true)
public int addAcount(String logId){
   int i = yiViUserAccountMapper.increaseAccount("100","71d8beff-9e70-11e7-9a60-00163e0a3457");
 
   int n =  paymentDispatchFeignClient.test(logId); //调用失败
 
   if(n == 0){
       throw new RuntimeException("服务调用失败");
   }
    return i + n;
}

②被调用服务方

@Override
@Transactional
@TxTransaction
public int test(String logId) throws YiViException {
    int i = yiViDispatchOrderStatusLogMapper.deleteByPrimaryKey(logId);
    return i;
}

具体可下载源码分析哦:https://pan.baidu.com/s/19IzIYiccV3vkDrnt8hjGeQ
--------------------- 
转自:https://blog.csdn.net/qq_38225558/article/details/86133637 
 

猜你喜欢

转载自blog.csdn.net/sanyaoxu_2/article/details/87902899