seata分布式事务

目录

1.分布式事务由来

2.安装步骤

3. 准备业务库

4.order module 搭建

5.seata之原理


1.分布式事务由来

分布式事务之前:单机单库没这个问题

seata是一款开源的分布式事务解决方案,致力于在微服务架构下提供高性能和简单易用的分布式事务服务。

ID+三组件模型

ID:全局唯一的id

三组件:

Transaction Coordinator TC:事务协调器

维护全局和分支事务的状态,驱动全局事务提交或回滚

Transaction Manager TM:定义全局事务的范围:开始全局事务、提交、回滚

Resource Manager:管理分支事务处理的资源,与TC交谈以注册分支事务和报告分支事务的状态,并驱动分支事务提交或回滚。

2.安装步骤

下载版本

将压缩包解压到指定目录并修改conf目录下的file.conf配置文件

file.conf主要修改:自定义事务组名称+事务日志存储模式为db+数据库连接信息

service模块:

        vgroup_mapping.my._test_tx_group = "自定义名称"

store模块:

        mode = db

修改数据库的链接配置

mysql新建seata库

在seata库里建表

修改seata/conf目录下的registry.conf配置文件

注册到nacos

3. 准备业务库

创建订单库,库存库,账户库

订单库中建立订单表,库存库中建立库存表,账户库中建立账户表。同时在各个库中建立事务回滚表,建表sql  seata/conf/db_undo_log.sql

4.order module 搭建

pom配置:

<dependency>
    <groupId>com.alibaba.cloud</groupId>
    <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>
<dependency>
   <groupId>com.alibaba.cloud</groupId>
   <artifactId>spring-cloud-starter-alibaba-seata</artifactId>
   <exclusions>
        <exclusion>
             <groupId>io.seata</groupId>
             <artifactId>seata-all</artifactId>
        </exclusion>
    </exclusions>
</dependency>
<dependency>
  <groupId>io.seata</groupId>
  <artifactId>seata-all</artifactId>
  <version>0.9.0</version>
</dependency>
<denpendency>
   <groupId>org.springframework.cloud</groupId>
   <artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>

yml配置:

spring:
    cloud:
        alibaba:
            seata:
               tx-service-group: 和 seata-server里面配置的组一致

config配置:

mybatis配置:

@Configuration
@MapperScan("{com.xxx.springcloud.alibaba.dao}")
public class MyBatisConfig{
    

}
@Configuration
public class DataSourceProxyConfig{
    
    @Value("${mybatis.mapperLocations}")
    private String mapperLocations;
    
    @Bean
    @ConfigurationProperties(prefix="spring.datasource")
    public DataSource druidDataSource(){
        return new DruidDataSource();
    }
    
    @Bean
    public DataSourceProxy dataSourceProxy(DataSource dataSource){
            return new DataSourceProxy(dataSource);
    }
    
    @Bean
    public SqlSessionFactory sqlSessionFactoryBean(DataSourceProxy dataSourceProxy) throws Exception{
            SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();
            sqlSessionFactoryBean.setDataSource(dataSourceProxt);
            sqlSessionFactoryBean.setMapperLocations(new PathMathingResourcePatternResolver().getResources(mapperLocations));
            sqlSessionFactoryBean.setTransactionFactory(new SpringManagedTransactionFactory());
            return sqlSessionFactoryBean.getObject();
    }
}

file.conf,registry.conf 配置文件拷贝到resources目录下

主启动类:

@EnableDiscoveryClient
@EnableFeignClients
@SpringBootApplication(exclude = DataSourceAutoConfiguration.class)//取消数据源的自动创建    
public class SeataOrderAPP{

    public static void main(String[] args){
        SpringApplication.run(SeataOrderApp.class ,args);
    
        }

}

@GlobalTransactional

5.seata之原理

seata简写: 简单可扩展的自治事务框架。

阿里云把seata改成GTS

seata特色功能:

        TCC模式,AT模式,XA模式,SAGA模式

默认是AT模式:提供无侵入自动补偿的事务模式,目前支持mysql,oracle。

AT模式:

两阶段提交协议的演变:+

一阶段:业务数据和回滚日志记录在同一个本地事务中提交,释放本地锁和连接资源。

二阶段:提交异步化,非常快速的完成。

回滚通过一阶段的回滚日志进行反向补偿。

百度开源的分布式唯一id生成器-UidGenerator

Leaf美团点评分布式id生成系统

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值