SpringCloud + springboot+LCN 实现分布式服务事务管理 回滚

前言

本文借鉴于:https://blog.csdn.net/qq_39057639/article/details/100436496
这篇博文。 原本想集成到项目中, 但因为集成 springboot版本 2.2.2.RELEASE 和springcloud
<spring-cloud.version>Hoxton.RELEASE</spring-cloud.version>
版本失败,再加上还需要 安装Consul 去使用,所以放弃了。 随后自己又集成了一个 eureka 的版本 ,无需 安装Consul
的一个版本。

0、项目源码地址

https://download.csdn.net/download/fangchao2011/12100373

1、项目结构图:

在这里插入图片描述
eureka-server: eureka 微服务注册中心。
txlcn-tm :LCN 管理端,用于管理服务事务。
test_a: 微服务 A
test_b: 微服务 B

2、测试服务事务流程:

启动 eureka-server 服务注册与发现。

在这里插入图片描述

启动 txlcn-tm 事务管理。

在这里插入图片描述

最后启动 test_a 和 test_b 微服务

数据库原始状态:

test_a_tx:
在这里插入图片描述
test_b_tx:
在这里插入图片描述

开始调用test_a 的 controller 接口进行测试

调用 A 服务controller ,在impl实现层 调用 B 服务的接口保存数据,抛异常,然后再调用本服务保存数据。检测是否B服务是否有回滚。

A controller:
在这里插入图片描述
A impl:
在这里插入图片描述

调用方法:

http://localhost:8080/tx/pay?money=100&user=xiaoming

已经进入我们预期的异常!
在这里插入图片描述
test_a :
在这里插入图片描述
test_b:
在这里插入图片描述
然后看数据库B表(值没有改变,说明事务已经回滚成功!):
test_b_tx
在这里插入图片描述

项目依赖

lcn-demo(父节点):pom.xml

父节点的包主要做版本控制

<!--<?xml version="1.0" encoding="UTF-8"?>-->
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>org.txlcn.demo</groupId>
    <artifactId>lcn-demo</artifactId>
    <version>1.0.0</version>
    <packaging>pom</packaging>
    <name>lcn-demo</name>

    <modules>
        <module>test_a</module>
        <module>test_b</module>
        <module>txlcn-tm</module>
    </modules>

    <properties>
        <!-- project -setting -->
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>

        <codingapi.txlcn.version>5.0.2.RELEASE</codingapi.txlcn.version>
        <springcloud.version>Greenwich.RELEASE</springcloud.version>
        <lombok.version>1.18.0</lombok.version>
    </properties>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.2.RELEASE</version>
        <relativePath/>
    </parent>

    <dependencies>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
        </dependency>
    </dependencies>

    <dependencyManagement>
        <dependencies>

            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>${springcloud.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>

            <dependency>
                <groupId>org.projectlombok</groupId>
                <artifactId>lombok</artifactId>
                <version>${lombok.version}</version>
            </dependency>


            <dependency>
                <groupId>com.codingapi.txlcn</groupId>
                <artifactId>txlcn-tc</artifactId>
                <version>${codingapi.txlcn.version}</version>
            </dependency>

            <dependency>
                <groupId>com.codingapi.txlcn</groupId>
                <artifactId>txlcn-tm</artifactId>
                <version>${codingapi.txlcn.version}</version>
            </dependency>

            <dependency>
                <groupId>com.codingapi.txlcn</groupId>
                <artifactId>txlcn-txmsg-netty</artifactId>
                <version>${codingapi.txlcn.version}</version>
            </dependency>
        </dependencies>
    </dependencyManagement>

</project>

txlcn-tm(事务管理)关键配置:

启动类加上 事务注解
@EnableTransactionManagerServer
在这里插入图片描述

application.properties:

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

# JDBC 数据库配置
spring.datasource.driver-class-name=com.mysql.cj.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

#eureka 地址
eureka.client.service-url.defaultZone=http://127.0.0.1:8761/eureka/
eureka.instance.prefer-ip-address=true

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

# 第一次运行可以设置为: create, 为TM创建持久化数据库表
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=jack

# 分布式事务锁超时时间 默认为-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/***@**.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.database=0
# Redis服务器地址
spring.redis.host=localhost
# Redis服务器连接端口
spring.redis.port=7000
# Redis服务器连接密码(默认为空)
spring.redis.password=
# 连接池最大连接数(使用负值表示没有限制)
spring.redis.jedis.pool.max-active=8
# 连接池最大阻塞等待时间(使用负值表示没有限制)
spring.redis.jedis.pool.max-wait=-1
# 连接池中的最大空闲连接
spring.redis.jedis.pool.max-idle=8
# 连接池中的最小空闲连接
spring.redis.jedis.pool.min-idle=0
# 连接超时时间(毫秒)
spring.redis.timeout=20000

pom.xml
关键包:

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

完整包:

<?xml version="1.0" encoding="UTF-8"?>

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <parent>
        <groupId>org.txlcn.demo</groupId>
        <artifactId>lcn-demo</artifactId>
        <version>1.0.0</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>txlcn-tm</artifactId>

    <name>txlcn-tm</name>

    <dependencies>

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

        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-openfeign</artifactId>
        </dependency>

    </dependencies>
</project>

test_a(服务A)关键配置:

启动类加上事务注解配置
@EnableDistributedTransaction
在这里插入图片描述
application.yml:

server:
  port: 8080
spring:
  application:
    name: test-a
  datasource:
      url: jdbc:mysql://localhost:3306/txlcn-demo?useUnicode=true&characterEncoding=utf-8&useSSL=false
      username: root
      password: root
      # MySQL 8.x: com.mysql.cj.jdbc.Driver
      driver-class-name: com.mysql.cj.jdbc.Driver
      type: com.alibaba.druid.pool.DruidDataSource
mybatis:
  configuration:
    log-impl: org.apache.ibatis.logging.stdout.StdOutImpl

#eureka 地址
eureka:
  client:
    service-url:
      defaultZone: http://127.0.0.1:8761/eureka/

pom.xml
关键包:

	<dependency>
            <groupId>com.codingapi.txlcn</groupId>
            <artifactId>txlcn-tc</artifactId>
        </dependency>

        <dependency>
            <groupId>com.codingapi.txlcn</groupId>
            <artifactId>txlcn-txmsg-netty</artifactId>
        </dependency>

完整包:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <parent>
        <groupId>org.txlcn.demo</groupId>
        <artifactId>lcn-demo</artifactId>
        <version>1.0.0</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>test_a</artifactId>
    <version>0.0.1-SNAPSHOT</version>

    <dependencies>

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

        <dependency>
            <groupId>com.codingapi.txlcn</groupId>
            <artifactId>txlcn-tc</artifactId>
        </dependency>

        <dependency>
            <groupId>com.codingapi.txlcn</groupId>
            <artifactId>txlcn-txmsg-netty</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-openfeign</artifactId>
        </dependency>

        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>8.0.18</version>
        </dependency>

        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid-spring-boot-starter</artifactId>
            <version>1.1.10</version>
        </dependency>

        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>2.1.0</version>
        </dependency>

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

        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
        </dependency>

    </dependencies>

</project>

test_b(服务B)关键配置:

application.yml:

server:
  port: 8081
spring:
  application:
    name: test-b
  datasource:
    url: jdbc:mysql://localhost:3306/txlcn-demo?useUnicode=true&characterEncoding=utf-8&useSSL=false
    username: root
    password: root
    # MySQL 8.x: com.mysql.cj.jdbc.Driver
    driver-class-name: com.mysql.cj.jdbc.Driver
    type: com.alibaba.druid.pool.DruidDataSource
mybatis:
  configuration:
    log-impl: org.apache.ibatis.logging.stdout.StdOutImpl

#eureka 地址
eureka:
  client:
    service-url:
      defaultZone: http://127.0.0.1:8761/eureka/

pom.xml

关键包:

	<dependency>
            <groupId>com.codingapi.txlcn</groupId>
            <artifactId>txlcn-tc</artifactId>
        </dependency>

        <dependency>
            <groupId>com.codingapi.txlcn</groupId>
            <artifactId>txlcn-txmsg-netty</artifactId>
        </dependency>

完整包:


<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <parent>
        <groupId>org.txlcn.demo</groupId>
        <artifactId>lcn-demo</artifactId>
        <version>1.0.0</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>test_b</artifactId>
    <version>0.0.1-SNAPSHOT</version>

    <dependencies>

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

        <dependency>
            <groupId>com.codingapi.txlcn</groupId>
            <artifactId>txlcn-tc</artifactId>
        </dependency>

        <dependency>
            <groupId>com.codingapi.txlcn</groupId>
            <artifactId>txlcn-txmsg-netty</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-openfeign</artifactId>
        </dependency>

        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>8.0.18</version>
        </dependency>

        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid-spring-boot-starter</artifactId>
            <version>1.1.10</version>
        </dependency>

        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>2.1.0</version>
        </dependency>

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

        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
        </dependency>

    </dependencies>


</project>
  • 1
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
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 ]

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值