SpringCloud第十九章 Alibaba Seata处理分布式事务(分布式事务问题、Seata简介、Seata-Server安装)

十九、SpringCloud Alibaba Seata处理分布式事务

1、分布式事务问题

  • 分布式前

    • 单机单库没这个问题

    • 从1:1 -> 1:N -> N:N

  • 分布式之后

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

  • 一句话

    一次业务操作需要跨多个数据源或需要跨多个系统进行远程调用,就会产生分布式事务问题

2、Seata简介

  • 是什么

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

    官网地址 http://seata.io/zh-cn/

  • 能干嘛

    一个典型的分布式事务过程

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

      • Transaction ID XID

        全局唯一的事务ID

      • 3组件概念

        • Transaction Coordinator (TC)

          事务协调器,维护全局事务的运行状态,负责协调并驱动全局事务的提交或回滚;

        • Transaction Manager ™

          控制全局事务的边界,负责开启一个全局事务,并最终发起全局提交或全局回滚的决议;

        • Resource Manager (RM)

          控制分支事务,负责分支注册、状态汇报,并接收事务协调器的指令,驱动分支(本地)事务的提交和回滚

    • 处理过程

      1. TM 向 TC 申请开启一个全局事务,全局事务创建成功并生成一个全局唯一的 XID;

      2. XID 在微服务调用链路的上下文中传播;

      3. RM 向 TC 注册分支事务,将其纳入 XID 对应全局事务的管辖;

      4. TM 向 TC 发起针对 XID 的全局提交或回滚决议;

      5. TC 调度 XID 下管辖的全部分支事务完成提交或回滚请求。

        在这里插入图片描述

  • 去哪下

    发布说明: https://github.com/seata/seata/releases

  • 怎么玩

    • 本地@Transactional

    • 全局@GlobalTransactional

      SEATA 的分布式交易解决方案

      在这里插入图片描述

3、Seata-Server安装

  • 官网地址 http://seata.io/zh-cn/

  • 下载 https://github.com/seata/seata/releases

  • seata-server-0.9.0.zip解压到指定目录并修改conf目录下的file.conf配置文件

    • 先备份原始file.conf文件

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

    • file.conf

      • service模块

        service {
        
          vgroup_mapping.my_test_tx_group = "fsp_tx_group"
        
          default.grouplist = "127.0.0.1:8091"
          enableDegrade = false
          disable = false
          max.commit.retry.timeout = "-1"
          max.rollback.retry.timeout = "-1"
        }
        
        
      • store模块

        ## transaction log store
        store {
          ## store mode: file、db
          mode = "db"
        
          ## file store
          file {
            dir = "sessionStore"
        
            # branch session size , if exceeded first try compress lockkey, still exceeded throws exceptions
            max-branch-session-size = 16384
            # globe session size , if exceeded throws exceptions
            max-global-session-size = 512
            # file buffer size , if exceeded allocate new buffer
            file-write-buffer-cache-size = 16384
            # when recover batch read size
            session.reload.read_size = 100
            # async, sync
            flush-disk-mode = async
          }
        
          ## database store
          db {
            ## the implement of javax.sql.DataSource, such as DruidDataSource(druid)/BasicDataSource(dbcp) etc.
            datasource = "dbcp"
            ## mysql/oracle/h2/oceanbase etc.
            db-type = "mysql"
            driver-class-name = "com.mysql.jdbc.Driver"
            url = "jdbc:mysql://127.0.0.1:3306/seata"
            user = "root"
            password = "你自己密码"
            min-conn = 1
            max-conn = 3
            global.table = "global_table"
            branch.table = "branch_table"
            lock-table = "lock_table"
            query-limit = 100
          }
        }
        
        
  • mysql5.7数据库新建库seata

  • 在seata库里建表

    • 建表db_store.sql在\seata-server-0.9.0\seata\conf目录里面 db_store.sql

    • SQL

      -- the table to store GlobalSession data
      drop table if exists `global_table`;
      create table `global_table` (
        `xid` varchar(128)  not null,
        `transaction_id` bigint,
        `status` tinyint not null,
        `application_id` varchar(32),
        `transaction_service_group` varchar(32),
        `transaction_name` varchar(128),
        `timeout` int,
        `begin_time` bigint,
        `application_data` varchar(2000),
        `gmt_create` datetime,
        `gmt_modified` datetime,
        primary key (`xid`),
        key `idx_gmt_modified_status` (`gmt_modified`, `status`),
        key `idx_transaction_id` (`transaction_id`)
      );
      
      -- the table to store BranchSession data
      drop table if exists `branch_table`;
      create table `branch_table` (
        `branch_id` bigint not null,
        `xid` varchar(128) not null,
        `transaction_id` bigint ,
        `resource_group_id` varchar(32),
        `resource_id` varchar(256) ,
        `lock_key` varchar(128) ,
        `branch_type` varchar(8) ,
        `status` tinyint,
        `client_id` varchar(64),
        `application_data` varchar(2000),
        `gmt_create` datetime,
        `gmt_modified` datetime,
        primary key (`branch_id`),
        key `idx_xid` (`xid`)
      );
      
      -- the table to store lock data
      drop table if exists `lock_table`;
      create table `lock_table` (
        `row_key` varchar(128) not null,
        `xid` varchar(96),
        `transaction_id` long ,
        `branch_id` long,
        `resource_id` varchar(256) ,
        `table_name` varchar(32) ,
        `pk` varchar(36) ,
        `gmt_create` datetime ,
        `gmt_modified` datetime,
        primary key(`row_key`)
      );
      
      

      在这里插入图片描述

  • 修改seata-server-0.9.0\seata\conf目录下的registry.conf配置文件

    registry {
      # file 、nacos 、eureka、redis、zk、consul、etcd3、sofa
      type = "nacos"
     
      nacos {
        serverAddr = "localhost:8848"
        namespace = ""
        cluster = "default"
      }
    

    目的是:指明注册中心为nacos,及修改nacos连接信息

  • 先启动Nacos端口号8848

  • 再启动seata-server seata-server.bat

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值