springcloud 集成seata1.4.2

2023-09-24 6 0

前言

官网上的demo中的seata版本很多都不是最新的,写得好的博客中的版本也不是最新的,大多是1.0.0,1.1.0版本的,或者没有集成seata-spring-boot-starter,还需要配置.conf 文件,或者没有集成spring-cloud-starter-alibaba-seata,还需要自己写传递xid的过滤器.新的版本集成起来确实更加便捷,但是由于没有现成的demo,所以还是费了不少劲.由于官方没有比较新的版本的demo,所以就只有通读官方官方文档,找版本的更新日志和说明,慢慢尝试集成.

难点

关于事务分组的理解:
我开始比较疑惑,其实就是一个资源的逻辑分组,当一个组的TC失效后,可以马上切换到另外的分组

新版本集成

官网说明.

  1. seata-spring-boot-starter:
    支持yml、properties配置(.conf可删除),内部已依赖seata-all
  2. spring-cloud-alibaba-seata:
    内部集成了seata,并实现了xid传递
  3. spring-cloud-starter-alibaba-seata:
    集成了seata-spring-boot-starter,所以拥有了seata-spring-boot-starter的功能,并实现了xid传递,自己不需要写传递xid的过滤器了
  4. 所以集成后的pom为:
	<!--spring-cloud-starter-alibaba-seata 中默认依赖的seata-spring-boot-starter 版本是1.1.0,官方说要这样用最新的版本,关于为什么用2.2.1.RELEASE,这个版本可以用在我的springcloud版本中 --><dependency><groupId>io.seata</groupId><artifactId>seata-spring-boot-starter</artifactId><version>1.4.2</version></dependency><dependency><groupId>com.alibaba.cloud</groupId><artifactId>spring-cloud-starter-alibaba-seata</artifactId><version>2.2.1.RELEASE</version><exclusions><exclusion><groupId>io.seata</groupId><artifactId>seata-spring-boot-starter</artifactId></exclusion></exclusions></dependency>
  1. 然后集成后的项目不需要.conf文件.可以通过.properties或者.yml文件配置
    在这里插入图片描述

我的demo 说明

  1. AT 模式

  2. 库初始化脚本在 try-business-service模块下的db文件夹中

  3. 服务之间的调用通过restTemplate,如果通过openFeign调用也不需要改动任何配置

  4. 为了简单,seata-server采用默认的file模式存储分布式事务会话信息,如果要改成db,这个看看官网改起来比较简单(官方demo.)

  5. 在我的demo中,seata-server 采用配置方式为file,从官网下载后啥都不用改,直接双击seata-server.bat(如下截图)启动server就行了(seata-server下载地址)
    在这里插入图片描述

  6. 架构,down下官方demo,然后改了改
    在这里插入图片描述

  7. 核心业务是在 try-business-service 的BusinessController 的purchaseCommit方法,对应BusinessService中的purchase方法
    在这里插入图片描述

  8. 测试方法
    BusinessService.purchase()中 // int i=1/0;(处于注释状态),业务正常进行,数据库产生业务数据
    BusinessService.purchase()中 int i=1/0;(会抛出异常),出现异常,全局回滚

  9. 其他要点说明
    9.1 这个是为了生成一个固定的事务分组名称,因为默认是每个微服务都有各自的分组(官方参考),注意需要和配置文件中的seata.service.vgroup-mapping.xxx保持一致
    在这里插入图片描述

在这里插入图片描述

  1. 从gitee上down下demo,试一下吧,很简单的~
    在这里插入图片描述
    在这里插入图片描述

我的demo gitee.

代码编程
赞赏

相关文章

最长公共子序列问题LCS Longest Common Subsequence
在使用Eclipse Debug JDK时,看不到变量值的解决办法
实例化bean
飞行器系统辨识
什么是旋翼变体技术
我国航天发展史上的里程碑