Seata 是一款開源的分佈式事務解決方案,致力於提供高性能和簡單易用的分佈式事務服務。Seata 將爲用戶提供了 AT、TCC、SAGA 和 XA 事務模式,爲用戶打造一站式的分佈式解決方案。html
一個分佈式事務鏈路須要多個系統參與, 不一樣的系統負責不一樣的角色. 通常來講, 分佈式事務的參與者須要包含如下 3 個角色.java
Seata
就是一個 TC.Spring 事務管理器
.Mysql 的管理器
.三者的關係爲:mysql
在 Seata 中,一個分佈式事務的生命週期以下:redis
本文主要講解 Seata 服務端的安裝.sql
Seata 的最簡版安裝很是直接. 下載最新版到本地, 解壓, 而後直接運行啓動腳本seata-server.sh
便可.數據庫
Seata 的核心配置主要有 2 個: 註冊中心/配置中心registry.conf
和數據存儲方式file.conf
. 咱們看下 Seata 的目錄結構:架構
bin |- seata-server.bat |- seata-server.sh conf |- META-INF/ |- registry.conf <- 註冊中心 registry 和配置中心 config 的相關配置. 默認都是本地 `file`. |- file.conf <- 存儲方式 store 相關配置, 默認爲 `file`. |- logback.xml lib |- ...
保持 registry.conf 和 file.conf 不動, 直接啓動服務:oracle
cd <seata_dir> nohup sh bin/seata-server.sh -h <your_pub_id> -p 8091 -m file &
修改配置文件 conf/file.conf
:app
## transaction log store, only used in seata-server store { ## store mode: file、db、redis mode = "db" // <- 這裏修改成 db ## file store property file { // 此處省略... } ## database store property db { // <- 配置seata db ## the implement of javax.sql.DataSource, such as DruidDataSource(druid)/BasicDataSource(dbcp)/HikariDataSource(hikari) etc. datasource = "druid" ## mysql/oracle/postgresql/h2/oceanbase etc. dbType = "mysql" driverClassName = "com.mysql.jdbc.Driver" url = "jdbc:mysql://<your_mysql_host>:3306/seata?characterEncoding=UTF-8&useTimezone=true&serverTimezone=Asia/Shanghai&useSSL=false&autoReconnect=true" user = "<mysql_user>" password = "<mysql_pass>" minConn = 1 maxConn = 5 globalTable = "global_table" branchTable = "branch_table" lockTable = "lock_table" queryLimit = 100 maxWait = 5000 } ## redis store property redis { // 此處省略... } }
在 Mysql 中建立 schema seata
, 並新建如下 3 個表:分佈式
-- the table to store GlobalSession data CREATE TABLE IF NOT EXISTS `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`) ) ENGINE = InnoDB DEFAULT CHARSET = utf8; -- the table to store BranchSession data CREATE TABLE IF NOT EXISTS `branch_table` ( `branch_id` BIGINT NOT NULL, `xid` VARCHAR(128) NOT NULL, `transaction_id` BIGINT, `resource_group_id` VARCHAR(32), `resource_id` VARCHAR(256), `branch_type` VARCHAR(8), `status` TINYINT, `client_id` VARCHAR(64), `application_data` VARCHAR(2000), `gmt_create` DATETIME(6), `gmt_modified` DATETIME(6), PRIMARY KEY (`branch_id`), KEY `idx_xid` (`xid`) ) ENGINE = InnoDB DEFAULT CHARSET = utf8; -- the table to store lock data CREATE TABLE IF NOT EXISTS `lock_table` ( `row_key` VARCHAR(128) NOT NULL, `xid` VARCHAR(96), `transaction_id` BIGINT, `branch_id` BIGINT NOT NULL, `resource_id` VARCHAR(256), `table_name` VARCHAR(32), `pk` VARCHAR(36), `gmt_create` DATETIME, `gmt_modified` DATETIME, PRIMARY KEY (`row_key`), KEY `idx_branch_id` (`branch_id`) ) ENGINE = InnoDB DEFAULT CHARSET = utf8;
而後啓動服務:
cd <seata_dir> nohup sh bin/seata-server.sh -h <your_pub_id> -p 8091 -m db -n 1 &
高可用模式通常使用 nacos 等註冊中心進行配置. 以 nacos 爲例, 修改registry.conf
的註冊中心配置:
registry { type = "nacos" nacos { application = "seata-server" serverAddr = "<your_nacos_host>" namespace = "" cluster = "default" username = "" password = "" } } config { type = "nacos" nacos { serverAddr = "<your_nacos_host>" namespace = "" group = "SEATA_GROUP" username = "" password = "" } }
而且啓動 3 個及以上服務:
cd <seata_dir> # 啓動 3 個服務 nohup sh bin/seata-server.sh -h <your_pub_id> -p 8091 -m db -n 1 & nohup sh bin/seata-server.sh -h <your_pub_id> -p 8092 -m db -n 2 & nohup sh bin/seata-server.sh -h <your_pub_id> -p 8093 -m db -n 3 &
更多參考 官方文檔.