分布式事务 seata

本文档详细记录了在项目中集成Seata进行分布式事务处理的过程,包括从下载Seata服务、配置SpringCloud、数据源代理设置,到业务模块的集成,数据库的准备,以及测试全局事务的实现。特别强调了在遇到问题时的解决思路和细节处理,旨在帮助开发者避免常见问题,顺利进行分布式事务的实施。
摘要由CSDN通过智能技术生成

分布式事务 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配置联调实际业务场景完成后补充更新

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值