一、下载与运行

本文使用 Seata 1.1.0:https://github.com/seata/seata/releases

Windows 环境下双击 bin/seata-server.bat 启动 Seata Server

二、结合 MyBatis 使用

以 Service1 为例

2.1 添加引用

  1. <dependency>
  2. <groupId>com.alibaba.cloud</groupId>
  3. <artifactId>spring-cloud-starter-alibaba-seata</artifactId>
  4. </dependency>

2.2 添加配置

2.2.1 registry.conf

Spring Cloud 快速集成 Seata 复制

放置在 resources 目录,内容详见代码,均采用了默认配置

2.2.2 file.conf

Spring Cloud 快速集成 Seata 复制

放置在 resources 目录,内容详见代码,均采用了默认配置

2.2.3 yml 配置

  1. spring:
  2. cloud:
  3. alibaba:
  4. seata:
  5. tx-service-group: my_test_tx_group

注意

file.conf 中 vgroup_mapping 的 key 值为 my_test_tx_group:

  1. service {
  2. vgroup_mapping.my_test_tx_group = "default"
  3. }

则 yml 中 tx-service-group 的值也必须为 my_test_tx_group;如果 vgroup_mapping 的 key 值改为 abcdefg,则 yml 中 tx-service-group 的值也必须为 abcdefg,即这两个值必须保持一致,否则会报错:

  1. no available service 'null' found, please make sure registry config correct

2.2.4 添加数据源配置

  1. @Configuration
  2. public class DataSourceProxyConfig {
  3. @Bean
  4. @ConfigurationProperties(prefix = "spring.datasource")
  5. public DataSource druidDataSource() {
  6. DruidDataSource druidDataSource = new DruidDataSource();
  7. return druidDataSource;
  8. }
  9. @Bean
  10. public SqlSessionFactory sqlSessionFactory(DataSource dataSource) throws Exception {
  11. SqlSessionFactoryBean factoryBean = new SqlSessionFactoryBean();
  12. factoryBean.setDataSource(dataSource);
  13. factoryBean.setMapperLocations(new PathMatchingResourcePatternResolver()
  14. .getResources("classpath*:/mapper/*.xml"));
  15. return factoryBean.getObject();
  16. }
  17. }

在2.2.0.RELEASE及以后,数据源代理自动实现了,不需要再手动去配置一个代理类,官方文档还需要配置 DataSourceProxy 应该是文档没有及时更新

2.2.5 添加 undo_log 表

在业务相关的数据库中添加 undo_log 表,用于保存需要回滚的数据

  1. CREATE TABLE `undo_log`
  2. (
  3. `id` BIGINT(20) NOT NULL AUTO_INCREMENT,
  4. `branch_id` BIGINT(20) NOT NULL,
  5. `xid` VARCHAR(100) NOT NULL,
  6. `context` VARCHAR(128) NOT NULL,
  7. `rollback_info` LONGBLOB NOT NULL,
  8. `log_status` INT(11) NOT NULL,
  9. `log_created` DATETIME NOT NULL,
  10. `log_modified` DATETIME NOT NULL,
  11. `ext` VARCHAR(100) DEFAULT NULL,
  12. PRIMARY KEY (`id`),
  13. UNIQUE KEY `ux_undo_log` (`xid`, `branch_id`)
  14. ) ENGINE = InnoDB
  15. AUTO_INCREMENT = 1
  16. DEFAULT CHARSET = utf8

2.3 使用

添加 @GlobalTransactional 注解即可

  1. @Service
  2. public class BusinessServiceImpl implements BusinessService {
  3. @Reference
  4. private StorageService storageService;
  5. @Reference
  6. private OrderService orderService;
  7. @GlobalTransactional
  8. @Override
  9. public void purchase(Order order) throws Exception {
  10. storageService.deduct(order.getCommodityCode(), order.getCount());
  11. orderService.create(order);
  12. }
  13. }

三、结合 MyBatis-Plus 使用

与在 MyBatis 中使用类似,区别在于数据源的配置

3.1 错误配置

  1. @Configuration
  2. public class DataSourceProxyConfig {
  3. @Bean
  4. @ConfigurationProperties(prefix = "spring.datasource")
  5. public DataSource druidDataSource() {
  6. DruidDataSource druidDataSource = new DruidDataSource();
  7. return druidDataSource;
  8. }
  9. // @Bean
  10. // public SqlSessionFactory sqlSessionFactory(DataSource dataSource) throws Exception {
  11. // SqlSessionFactoryBean factoryBean = new SqlSessionFactoryBean();
  12. // factoryBean.setDataSource(dataSource);
  13. // factoryBean.setMapperLocations(new PathMatchingResourcePatternResolver()
  14. // .getResources("classpath*:/mapper/*.xml"));
  15. // return factoryBean.getObject();
  16. // }
  17. @Bean
  18. @ConfigurationProperties(prefix = "mybatis-plus")
  19. public MybatisSqlSessionFactoryBean sqlSessionFactoryBean(DataSource dataSource) throws IOException {
  20. // 这里用 MybatisSqlSessionFactoryBean 代替了 SqlSessionFactoryBean,否则 MyBatisPlus 不会生效
  21. MybatisSqlSessionFactoryBean mybatisSqlSessionFactoryBean = new MybatisSqlSessionFactoryBean();
  22. mybatisSqlSessionFactoryBean.setDataSource(dataSource);
  23. mybatisSqlSessionFactoryBean.setMapperLocations(new PathMatchingResourcePatternResolver()
  24. .getResources("classpath*:/mapper/mapper/*.xml"));
  25. return mybatisSqlSessionFactoryBean;
  26. }
  27. }

上面配置是参考 seata-samples 中的配置,虽然可以正常使用,但是造成 MyBatis-Plus 的插件如分页等不生效

3.2 正确配置

  1. public class DataSourceProxyConfig {
  2. @Bean
  3. @ConfigurationProperties(prefix = "spring.datasource")
  4. public DataSource druidDataSource() {
  5. DruidDataSource druidDataSource = new DruidDataSource();
  6. return druidDataSource;
  7. }
  8. }

仅配置 DataSource 即可

完整代码:GitHub

Seata 1.1.0 及之后版本支持直接在 application.yml 添加配置,可以替换掉 registry.conf 和 file.conf,本人暂未测试

参考

  1. Spring Cloud 快速集成 Seata
  2. Seata初试采坑
  3. 二、springboot项目使用seata实现分布式事务

Spring Cloud Alibaba 初体验(六) Seata 及结合 MyBatis 与 MyBatis-Plus 的使用的更多相关文章

  1. Spring Cloud Alibaba 初体验(一) Nacos 配置中心

    一.Nacos 下载与初始化配置 本文使用1.2.0,下载地址:https://github.com/alibaba/nacos/releases Nacos 单机模式支持持久化配置到 MySQL 数 ...

  2. Spring Cloud Alibaba 初体验(二) Nacos 服务注册与发现 + 集成 Spring Cloud Gateway

    一.服务注册 添加依赖: <dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>s ...

  3. Spring Cloud Alibaba 初体验(四) Sentinel

    一.Sentinel 下载与运行 本文使用 Sentinel 1.7.1:https://github.com/alibaba/Sentinel/releases 使用自定义端口 8089 运行 Se ...

  4. Spring Cloud Alibaba 初体验(三) Nacos 与 Dubbo 集成

    一.新建项目 新建项目,只放置接口,用于暴露 Dubbo 服务接口 public interface GreetingService { String greeting(); } 二.provider ...

  5. Spring Cloud Alibaba 初体验(五) SkyWalking

    一.下载与运行 本文使用 SkyWalking 7.0.0:https://www.apache.org/dyn/closer.cgi/skywalking/7.0.0/apache-skywalki ...

  6. Spring Cloud Alibaba系列(六)sentinel的实际应用

    一.sentinel的持久化配置 上一章中我们通过Dashboard来为Sentinel客户端设置各种各样的规则,但是这些规则默认是存放在内存中,极不稳定,无法用于生成环境,所以需要将其持久化. Da ...

  7. Spring Cloud Alibaba分布式事务组件 seata 详解(小白都能看懂)

    一,什么是事务(本地事务)? 指作为单个逻辑工作单元执行的一系列操作,要么完全地执行,要么完全地不执行. 简单的说,事务就是并发控制的单位,是用户定义的一个操作序列.      而一个逻辑工作单元要成 ...

  8. Spring Cloud Alibaba | 微服务分布式事务之Seata

    Spring Cloud Alibaba | 微服务分布式事务之Seata 本篇实战所使用Spring有关版本: SpringBoot:2.1.7.RELEASE Spring Cloud:Green ...

  9. Spring Cloud Alibaba基础教程:Nacos配置的多环境管理

    前情回顾: <Spring Cloud Alibaba基础教程:使用Nacos实现服务注册与发现> <Spring Cloud Alibaba基础教程:支持的几种服务消费方式> ...

随机推荐

  1. java 动态增加应用服务器,出现的消息队列的消费者提报错问题

    java 动态增加应用服务器,出现的消息队列的消费者提报错问题 在项目中,有这样的业务场景,在某一个时间段,客户流量瞬间增大,服务器瞬间很大,出现高并发问题.有一种解决方案就是脚本动态增加业务服务器, ...

  2. Java入门(5)

    阅读书目:Java入门经典(第7版) 作者:罗格斯·卡登海德 protected变量只能在其所在的类,该类的子类,以及同一个包里的其他类中使用.包是一组用于完成相同目标的相关类. private变量只 ...

  3. 教你如何 分析 Android ANR 问题

    ANR介绍 ANR 的全称是 Application No Responding,即应用程序无响应,具体是一些特定的 Message (Key Dispatch.Broadcast.Service) ...

  4. 1.python的函数参数传递

    1 Python的函数参数传递 看两个例子: a = 1 def fun(a): a = 2 fun(a) print a # 1 a = [] def fun(a): a.append(1) fun ...

  5. Docker 实战(2)- 配置 Jenkins 容器上的持续集成环境

    如果你还想从头学起 Docker,可以看看这个系列的文章哦! https://www.cnblogs.com/poloyy/category/1870863.html 搭建 Jenkins 容器 就是 ...

  6. ARM的三级流水线结构

    看到汇编中很多关于程序返回与中断返回时处理地址都很特别,仔细想想原来是流水线作用的效果.所以,决定总结学习下ARM流水线. ARM7处理器采用3级流水线来增加处理器指令流的速度,能提供0.9MIPS/ ...

  7. 日志切分神器--logrotate

    Blog:博客园 个人 概述 还在自己写定时切分日志的脚本?试试系统自带的logrotate工具吧! logrotate是一个日志文件管理工具.用于分割日志文件,删除旧的日志文件,并创建新的日志文件, ...

  8. python xmind转Excel(puppet洛洛原创)

    需求:将xmind文件转为Excel文件,并添加UI界面操作以降低操作难度. 这个需求一句话就讲清楚了,但实际上还需要做很多工作: 1,了解Xmind文件结构 2,提取Xmind文件分支内容(重点) ...

  9. padding的讲究

    padding有一个陷阱,你平常可能不太注意. 行内元素上设置的内边距不会影响行高计算:因此,如果一个行内元素既有内边距又有背景,从视觉上看可能会延伸到其他行,有可能还会与其他内容重叠. 对于块元素, ...

  10. linux解释器、内建和外建命令

    查看系统是哪种命令解释器: [root@localhost ~]# echo $SHELL /bin/bash 内建命令:是shell程序的一部分,包含的是一些比较简单的linux系统命令,这些命令由 ...