分布式事务 seata
因为项目业务需要添加分布式事务,查阅资料都是大方面相同很少提及细节
也是踩了无数坑 对于新搭建的实在是折磨 所以自己整理一份出来希望帮助大家避坑
下载
下载链接:https://github.com/seata/seata/releases
版本:0.9 里面有nacos的配置文件后续升级版本也需要用到的
seata服务
一开始搭建可以先不用联调nacos不便于排错
下载完直接解压运行即可
springcloud
可以建一个seata的子模块便于实际业务模块直接引用
seata模块
pom依赖
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-seata</artifactId>
<version>2.2.7.RELEASE</version>
<exclusions>
<exclusion>
<artifactId>seata-all</artifactId>
<groupId>io.seata</groupId>
</exclusion>
<exclusion>
<groupId>io.seata</groupId>
<artifactId>seata-spring-boot-starter</artifactId>
</exclusion>
<exclusion>
<artifactId>spring-boot-starter</artifactId>
<groupId>org.springframework.boot</groupId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>io.seata</groupId>
<artifactId>seata-all</artifactId>
<version>1.3.0</version>
<exclusions>
<exclusion>
<artifactId>fastjson</artifactId>
<groupId>com.alibaba</groupId>
</exclusion>
</exclusions>
</dependency>
数据源代理
我们项目用的是mybatisplus所以是增强的sqlSessionFactoryBean
不知道为什么增强后xml的映射找不到所以手动添加了映射
@Configuration
public class DataSourceConfig {
@Primary
@Bean
public SqlSessionFactory sqlSessionFactoryBean(DataSource dataSource) throws Exception {
// 订单服务中引入了mybatis-plus,所以要使用特殊的SqlSessionFactoryBean
MybatisSqlSessionFactoryBean sqlSessionFactoryBean = new MybatisSqlSessionFactoryBean();
// 设置mapper.xml文件的路径
ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
Resource[] resource = resolver.getResources("classpath*:mapper/**/*Mapper.xml");
sqlSessionFactoryBean.setMapperLocations(resource);
// 代理数据源
sqlSessionFactoryBean.setDataSource(new DataSourceProxy(dataSource));
// 单独给数据源设置驼峰映射
SqlSessionFactory object = sqlSessionFactoryBean.getObject();
assert object != null;
object.getConfiguration().setMapUnderscoreToCamelCase(true);
// 生成SqlSessionFactory
return sqlSessionFactoryBean.getObject();
}
如果用的是mybatis增强dataSource就可以了
@Primary
@Bean("dataSource")
public DataSource dataSource(DruidDataSource druidDataSource) {
return new DataSourceProxy(druidDataSource);
}
业务模块
把新建的seata子模块在pom文件中引入
<dependency>
<groupId>com.ht.scm</groupId>
<artifactId>htscm-test-seata</artifactId>
</dependency>
拷贝seata的 file.conf 和 registry.conf到业务模块的resources目录下
业务模块的yml文件补充配置
# Tomcat
server:
port: 9211
# Spring
spring:
application:
# 应用名称
name: htscm-initplat
profiles:
# 环境配置
active: dev
cloud:
alibaba:
seata:
enabled: true
application-id: ${spring.application.name}
tx-service-group: my_test_tx_group
enable-auto-data-source-proxy: true
数据库
seata的conf下有一个db_undo_log.sql文件里面有一个undo_log表的sql需要建立
drop table `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;
测试
发起点添加注解@GlobalTransactional
两边只要有异常抛出,全局回滚
后续nacos配置联调实际业务场景完成后补充更新