Seata 处理分布式事务

一、分布式事务问题由来

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

  全局数据一致性的保障!!!!

二、Seata 简介

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

  管网地址:https://seata.io/zh-cn/

三、Seata 作用

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

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

一ID:

Transaction ID XID 全局唯一的事务ID

三组件概念:

Transaction Coordinator(TC):事务协调者,维护全局和分支事务的状态,驱动全局事务提交或回滚。

Transaction Manager(TM):事务管理器,定义全局事务的范围,开始全局事务,提交或回滚全局事务。

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

处理过程

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

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

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

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

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

 

 四、下载

下载地址:https://seata.io/zh-cn/blog/download.html

github地址:https://github.com/seata/seata/releases

 

 我这里下载的是0.9.0版本

 

 点击下载即可

 

 五、安装seate-server

5.1解压到指定目录下

5.2 修改conf目录下file.conf文件

 

 修改之前先备份一个~

修改seivice模块(29行)

复制代码
service {
  #vgroup->rgroup
  vgroup_mapping.my_test_tx_group = "fsp_tx_group"
  #only support single node
  default.grouplist = "127.0.0.1:8091"
  #degrade current not support
  enableDegrade = false
  #disable
  disable = false
  #unit ms,s,m,h,d represents milliseconds, seconds, minutes, hours, days, default permanent
  max.commit.retry.timeout = "-1"
  max.rollback.retry.timeout = "-1"
}
复制代码

修改store模块(55行)

复制代码
## 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://IP地址:3306/seata"
    user = "数据库账号"
    password = "数据库密码"
    min-conn = 1
    max-conn = 3
    global.table = "global_table"
    branch.table = "branch_table"
    lock-table = "lock_table"
    query-limit = 100
  }
}
复制代码

 5.3 创建seata的MySQL数据库

创建“seata”数据库,然后执行conf文件夹下的“db_store.sql”脚本创建数据表

 

 

 5.4 修改 registry.conf配置文件

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

复制代码
registry {
  # file 、nacos 、eureka、redis、zk、consul、etcd3、sofa
  type = "nacos"

  nacos {
    serverAddr = "localhost:8848"
    namespace = ""
    cluster = "default"
  }
  eureka {
    serviceUrl = "http://localhost:8761/eureka"
    application = "default"
    weight = "1"
  }
  redis {
    serverAddr = "localhost:6379"
    db = "0"
  }
  zk {
    cluster = "default"
    serverAddr = "127.0.0.1:2181"
    session.timeout = 6000
    connect.timeout = 2000
  }
  consul {
    cluster = "default"
    serverAddr = "127.0.0.1:8500"
  }
  etcd3 {
    cluster = "default"
    serverAddr = "http://localhost:2379"
  }
  sofa {
    serverAddr = "127.0.0.1:9603"
    application = "default"
    region = "DEFAULT_ZONE"
    datacenter = "DefaultDataCenter"
    cluster = "default"
    group = "SEATA_GROUP"
    addressWaitTime = "3000"
  }
  file {
    name = "file.conf"
  }
}

config {
  # file、nacos 、apollo、zk、consul、etcd3
  type = "file"

  nacos {
    serverAddr = "localhost"
    namespace = ""
  }
  consul {
    serverAddr = "127.0.0.1:8500"
  }
  apollo {
    app.id = "seata-server"
    apollo.meta = "http://192.168.1.204:8801"
  }
  zk {
    serverAddr = "127.0.0.1:2181"
    session.timeout = 6000
    connect.timeout = 2000
  }
  etcd3 {
    serverAddr = "http://localhost:2379"
  }
  file {
    name = "file.conf"
  }
}
复制代码

5.5 测试服务

启动Nacos端口8848

启动seata-server

注意:依次启动Nacos和Seata-server否则会提示加载失败!!

 

 

 表明装载成功!

 

posted @   创客未来  阅读(1026)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?
点击右上角即可分享
微信分享提示