一、下载与运行

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

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

二、结合 MyBatis 使用

以 Service1 为例

2.1 添加引用

<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-seata</artifactId>
</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 配置

spring:
cloud:
alibaba:
seata:
tx-service-group: my_test_tx_group

注意:

file.conf 中 vgroup_mapping 的 key 值为 my_test_tx_group:

service {
vgroup_mapping.my_test_tx_group = "default"
}

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

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

2.2.4 添加数据源配置

@Configuration
public class DataSourceProxyConfig { @Bean
@ConfigurationProperties(prefix = "spring.datasource")
public DataSource druidDataSource() {
DruidDataSource druidDataSource = new DruidDataSource();
return druidDataSource;
} @Bean
public SqlSessionFactory sqlSessionFactory(DataSource dataSource) throws Exception {
SqlSessionFactoryBean factoryBean = new SqlSessionFactoryBean();
factoryBean.setDataSource(dataSource);
factoryBean.setMapperLocations(new PathMatchingResourcePatternResolver()
.getResources("classpath*:/mapper/*.xml"));
return factoryBean.getObject();
}
}

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

2.2.5 添加 undo_log 表

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

CREATE TABLE `undo_log`
(
`id` BIGINT(20) NOT NULL AUTO_INCREMENT,
`branch_id` BIGINT(20) NOT NULL,
`xid` VARCHAR(100) NOT NULL,
`context` VARCHAR(128) NOT NULL,
`rollback_info` LONGBLOB NOT NULL,
`log_status` INT(11) NOT NULL,
`log_created` DATETIME NOT NULL,
`log_modified` DATETIME NOT NULL,
`ext` VARCHAR(100) DEFAULT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `ux_undo_log` (`xid`, `branch_id`)
) ENGINE = InnoDB
AUTO_INCREMENT = 1
DEFAULT CHARSET = utf8

2.3 使用

添加 @GlobalTransactional 注解即可

@Service
public class BusinessServiceImpl implements BusinessService { @Reference
private StorageService storageService;
@Reference
private OrderService orderService; @GlobalTransactional
@Override
public void purchase(Order order) throws Exception {
storageService.deduct(order.getCommodityCode(), order.getCount());
orderService.create(order);
}
}

三、结合 MyBatis-Plus 使用

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

3.1 错误配置

@Configuration
public class DataSourceProxyConfig { @Bean
@ConfigurationProperties(prefix = "spring.datasource")
public DataSource druidDataSource() {
DruidDataSource druidDataSource = new DruidDataSource();
return druidDataSource;
} // @Bean
// public SqlSessionFactory sqlSessionFactory(DataSource dataSource) throws Exception {
// SqlSessionFactoryBean factoryBean = new SqlSessionFactoryBean();
// factoryBean.setDataSource(dataSource);
// factoryBean.setMapperLocations(new PathMatchingResourcePatternResolver()
// .getResources("classpath*:/mapper/*.xml"));
// return factoryBean.getObject();
// } @Bean
@ConfigurationProperties(prefix = "mybatis-plus")
public MybatisSqlSessionFactoryBean sqlSessionFactoryBean(DataSource dataSource) throws IOException {
// 这里用 MybatisSqlSessionFactoryBean 代替了 SqlSessionFactoryBean,否则 MyBatisPlus 不会生效
MybatisSqlSessionFactoryBean mybatisSqlSessionFactoryBean = new MybatisSqlSessionFactoryBean();
mybatisSqlSessionFactoryBean.setDataSource(dataSource);
mybatisSqlSessionFactoryBean.setMapperLocations(new PathMatchingResourcePatternResolver()
.getResources("classpath*:/mapper/mapper/*.xml"));
return mybatisSqlSessionFactoryBean;
}
}

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

3.2 正确配置

public class DataSourceProxyConfig {

    @Bean
@ConfigurationProperties(prefix = "spring.datasource")
public DataSource druidDataSource() {
DruidDataSource druidDataSource = new DruidDataSource();
return druidDataSource;
}
}

仅配置 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. 超实用的14个 Spring MVC “隐藏”技巧,用了都说好!

    通常,在Spring MVC中,我们编写一个控制器类来处理来自客户端的请求.然后,控制器调用业务类来处理与业务相关的任务,然后将客户端重定向到逻辑视图名称,该名称由Spring的调度程序Servlet ...

  2. simulink产生周期矩形波和8421码

    初次入门simulink,由于学习了数字逻辑,试图进行仿真,首先需要的就是8421码,但是没找到simulink里面相关模块,如果各位知道怎么弄可以评论告诉我分享分享哈哈 我用的是matlab2016 ...

  3. tcpack---1简述

    TCP重传机制 TCP要保证所有的数据包都可以到达,所以,必需要有重传机制. 超时重传机制 一种是不回ack,死等3,当发送方发现收不到3的ack超时后,会重传3.一旦接收方收到3后,会ack 回 4 ...

  4. 极客mysql38

    InnoDB 引擎把数据放在主键索引上,其他索引上保存的是主键 id.这种方式,我们称之为索引组织表(Index Organizied Table). 而 Memory 引擎采用的是把数据单独存放,索 ...

  5. Java 获取微信小程序二维码(可以指定小程序页面 与 动态参数)

    一.准备工作 微信公众平台接口调试工具 小程序的唯一标识(appid) 小程序的密钥(secret) 二.获取access_token 打开微信公众平台接口调试工具,在参数列表中输入小程序的appid ...

  6. 虚拟机、ip地址

    使用的系统 虚拟机:VMware workstations+win10:注:系统装好后先切换成Administrator,给VMware装VMware Tools linux发行版本  rhel-se ...

  7. 内存使用过高点检checklist

    正在运行的程序按照内存段来组织,内存段的类型有如下几种: 代码段:用户程序指令,长期存在内存中 数据段:全局变量等,长期存在内存中 堆:局部变量,参数参数等,短期存在内存中 栈:动态存储,可变 代码段 ...

  8. kali 系列学习06 - 攻击之密码破解

    参考书 <kali linux渗透测试技术详解> 清华大学出版社  2015 杨波 一.medusa使用 原理:并行登陆破解 1.只输入单个命令,是查看帮助 root@kali:/tool ...

  9. Guitar Pro教程之组织小节

    上一章节我们讲述了关于Guitar Pro 7的主界面的相关功能的介绍,对于初学作曲,又是吉他的初学者,刚刚接触{cms_selflink page='index' text='Guitar Pro' ...

  10. 从Guarded Block来看Java中的wait和notify方法

    目录 预备知识 概览 线程同步 wait()方法 wait() wait(long timeout) wait(long timeout, int nanos) notify() & noti ...