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否則會提示加載失敗!!

 

 

 表明裝載成功!

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM