9.10(SpringCloudAlibaba)分布式事物简介

分布式事物

事物:

为保证一个数据库中数据的ACID

我们需要做事物,一个数据库中,多个sql,要么都执行要么都不执行

里面存在多种条件,项隔离级别

分布式事物

现在架构都是分布式架构,为了保证多个数据库之间可以做到

A数据库,B数据库,C数据库。中的Sql要么都执行要么都不执行。就是分布式事物

单体应用被拆分成微服务应用,原来的三个模块被拆分成三个独立的应用,分别使用三个独立的数据源,
业务操作需要调用三个服务来完成。此时每个服务内部的数据一致性由本地事务来保证,但是全局的数据一致性问题没法保证。

在这里插入图片描述

Seata

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

官网

http://seata.io/zh-cn/

在这里插入图片描述

可以在官网看文档,下载

术语

  1. TC (Transaction Coordinator) - 事务协调者
    维护全局和分支事务的状态,驱动全局事务提交或回滚。
  2. TM (Transaction Manager) - 事务管理器
    定义全局事务的范围:开始全局事务、提交或回滚全局事务。
  3. RM (Resource Manager) - 资源管理器
    管理分支事务处理的资源,与TC交谈以注册分支事务和报告分支事务的状态,并驱动分支事务提交或回滚。

处理过程

分布式事务处理过程的一个ID+三组件模型

  1. TM 向 TC 申请开启一个全局事务,全局事务创建成功并生成一个全局唯一的 XID;
  2. XID 在微服务调用链路的上下文中传播;
  3. RM 向 TC 注册分支事务,将其纳入 XID 对应全局事务的管辖;
  4. TM 向 TC 发起针对 XID 的全局提交或回滚决议;
  5. TC 调度 XID 下管辖的全部分支事务完成提交或回滚请求。

在这里插入图片描述

Seata的安装和配置

这个部分网上的说法很多

安装肯定很简单啦,这不用说了。

下载下来就是一个文件。

windows版本和Linux版本式不一样的

在这里插入图片描述

配置

我们都知道配置文件一定在conf目录下

为了保证安全我们可以先将文件clone一份在进行操作配置

我们接下来要操作的式file.conf文件

在这里插入图片描述

主要修改:
自定义事物组的名称

service模块

在这里插入图片描述

事物日志存储模式为DB+数据库连接信息

store模块

在这里插入图片描述

在这里插入图片描述

数据库添加数据库与表

我们在配置的时候指定了数据库的信息。所以为了之后使用。我们在数据中对应操作。

创建一个seata数据库

在这里插入图片描述

在Seata中建表

在这里插入图片描述

Seata中提供了见表的文件

在这里插入图片描述

修改gistry.conf配置

指定注册中心

在这里插入图片描述

启动

启动启动Nacos在启动Seata

在这里插入图片描述

在这里插入图片描述

alibaba的技术很厉害。将我们的分布式需要的技术都整合在一个一个组件的技术中。

  1. nacos:注册中
  2. sentinel:服务熔断
  3. seata:分布式事务

搭建环境

分布式事物这里使用的Seata。我根据硅谷搭建环境

如果可以的还是去看一下硅谷的原视频。我自己下面都觉得很low

如果可以的还是去看一下硅谷的原视频。我自己下面都觉得很low

如果可以的还是去看一下硅谷的原视频。我自己下面都觉得很low

尚硅谷SpringCloud框架开发教程(SpringCloudAlibaba微服务分布式架构丨Spring Cloud)_哔哩哔哩_bilibili

下面的内容可以说是写给我自己看的。。。。。

导入依赖
 <dependencies>
        <!--nacos-->
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
        </dependency>
        <!--seata-->
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-seata</artifactId>
            <exclusions>
                <exclusion>
                    <artifactId>seata-all</artifactId>
                    <groupId>io.seata</groupId>
                </exclusion>
            </exclusions>
        </dependency>
     
<!--  ##     这里的编写是为了不同版本对应版本的依赖更加合适  -->
        <dependency>
            <groupId>io.seata</groupId>
            <artifactId>seata-all</artifactId>
            <version>0.9.0</version>
        </dependency>
        <!--feign-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-openfeign</artifactId>
        </dependency>
        <!--web-actuator-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
        <!--mysql-druid-->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.37</version>
        </dependency>
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid-spring-boot-starter</artifactId>
            <version>1.1.10</version>
        </dependency>
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>2.0.0</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
    </dependencies>

pom
spring:
  application:
    name: seata-order-service
  cloud:
    alibaba:
      seata:
        #自定义事务组名称需要与seata-server中的对应
        tx-service-group: fsp_tx_group
    nacos:
      discovery:
        server-addr: localhost:8848
  datasource:
    driver-class-name: com.mysql.jdbc.Driver
    url: jdbc:mysql://localhost:3306/seata_order
    username: root
    password: 123456
file.conf

这里引入自己的seata的配置文件

registry.conf

同样引入自己的seata的配置文件

其他层

dao层,实体层,业务逻辑成,控制器层等

这些都是自行编写的。

配置类

硅谷这里将Mybatis的配置以配置类的形式展示出来。

说是因为yaml可能会与Seata中的起冲突

/**
 * @auther zzyy
 * @create 2019-12-11 16:57
 */
@Configuration
@MapperScan({"com.atguigu.springcloud.alibaba.dao"})
public class MyBatisConfig {
}
/**
 * @auther zzyy
 * @create 2019-12-11 16:58
 * 使用Seata对数据源进行代理
 */
@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(dataSourceProxy);
        sqlSessionFactoryBean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources(mapperLocations));
        sqlSessionFactoryBean.setTransactionFactory(new SpringManagedTransactionFactory());
        return sqlSessionFactoryBean.getObject();
    }

}

主启动
/**
 * @auther zzyy
 * @create 2019-12-11 17:02
 */
@EnableDiscoveryClient
@EnableFeignClients
@SpringBootApplication(exclude = DataSourceAutoConfiguration.class)//取消数据源的自动创建
public class SeataOrderMainApp2001
{
@GlobalTransactional注解

在控制器中

调用其他微服务的数据库操作的时候进行添加。从而开启全局的数据库事物

当然其他的微服务配置也要和这边的类似

在这里插入图片描述

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值