Seata Server 環境搭建
參考地址:
seata.io/zh-cn/docs/…
Server 端存儲模式(store.mode)支持三種:
- file:單機模式,全局事務會話信息內存中讀寫并持久化本地文件root.data,性能較高
- db:高可用模式,全局事務會話信息通過db共享,相應性能差些
- redis:Seata-Server 1.3及以上版本支持,性能較高,存在事務信息丟失風險,請提前配置適合當前場景的redis持久化配置
配置文件
配置文件目錄:
github.com/seata/seata…
client
存放client端sql腳本,參數配置
config-center
各個配置中心參數導入腳本,config.txt(包含server和client,原名nacos-config.txt)為通用參數文件
server
server端數據庫腳本及各個容器配置
db存儲模式 + Nacos(注冊&配置中心)部署
步驟一:下載安裝包 github.com/seata/seata…
步驟二:建表(僅db模式) 全局事務會話信息由3塊內容構成,全
局事務-->分支事務-->全局鎖,對應表global_table、branch_table、lock_table 創建數據庫seata,執行sql腳本,文件在
script/server/db/MySQL.sql(seata源碼)中
-- -------------------------------- The script used when storeMode is 'db' --------------------------------
-- 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(128),
`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;
步驟三:修改store.mode
啟動包: seata-->conf-->file.conf,修改store.mode="db"
源碼: 根目錄-->seata-server-->resources-->file.conf,修改store.mode="db"
步驟四:修改數據庫連接
啟動包: seata-->conf-->file.conf,修改store.db相關屬性。
源碼: 根目錄-->seata-server-->resources-->file.conf,修改store.db相關屬性。
此時可以調到步驟七:直接啟動Seata Server,注冊中心和配置中心都是file
## transaction log store, only used in seata-server
store {
## store mode: file、db、redis
mode = "db"
## rsa decryption public key
publicKey = ""
## file store property
file {
## store location dir
dir = "sessionStore"
# branch session size , if exceeded first try compress lockkey, still exceeded throws exceptions
maxBranchSessionSize = 16384
# globe session size , if exceeded throws exceptions
maxGlobalSessionSize = 512
# file buffer size , if exceeded allocate new buffer
fileWriteBufferCacheSize = 16384
# when recover batch read size
sessionReloadReadSize = 100
# async, sync
flushDiskMode = async
}
## database store property
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"
## if using mysql to store the data, recommend add rewriteBatchedStatements=true in jdbc connection param
url = "jdbc:mysql://127.0.0.1:3306/seata?rewriteBatchedStatements=true"
user = "root"
password = "root123"
minConn = 5
maxConn = 100
globalTable = "global_table"
branchTable = "branch_table"
lockTable = "lock_table"
queryLimit = 100
maxWait = 5000
}
## redis store property
redis {
## redis mode: single、sentinel
mode = "single"
## single mode property
single {
host = "127.0.0.1"
port = "6379"
}
## sentinel mode property
sentinel {
masterName = ""
## such as "10.28.235.65:26379,10.28.235.65:26380,10.28.235.65:26381"
sentinelHosts = ""
}
password = ""
database = "0"
minConn = 1
maxConn = 10
maxTotal = 100
queryLimit = 100
}
}
步驟五:配置Nacos注冊中心
將Seata Server注冊到Nacos,修改conf目錄下的registry.conf配置
然后啟動注冊中心Nacos Server
步驟六:配置Nacos配置中心
# 進入Nacos安裝目錄,linux單機啟動
bin/startup.sh ‐m standalone
# windows單機啟動
bin/startup.bat
注意:如果配置了seata server使用nacos作為配置中心,則配置信息會從nacos讀取,file.conf可以不用配置。 客戶端配置registry.conf 使用nacos時也要注意group要和seata server中的group一致,默認group是"DEFAULT_GROUP" 獲取
/seata/script/config-center/config.txt,修改配置信息配置事務分組, 要與客戶端配置的事務分組一致 (客戶端properties配置:spring.cloud.alibaba.seata.tx‐service‐group=my_test_tx_group)
配置參數同步到Nacos shell:
sh /Users/zhengsh/software/seata/script/config-center/nacos/nacos-config.sh ‐h localhost ‐p 8848 ‐g SEATA_GROUP ‐t 5a3c7d6c‐f497‐4d68‐a71a‐2e5e3340b3ca
參數說明:
-h: host,默認值 localhost
-p: port,默認值 8848
-g: 配置分組,默認值為 'SEATA_GROUP'
-t: 租戶信息,對應 Nacos 的命名空間ID字段, 默認值為空
執行結果
精簡配置
service.vgroupMapping.my_test_tx_group=default
service.default.grouplist=127.0.0.1:8091
service.enableDegrade=false
service.disableGlobalTransaction=false
store.mode=db
store.db.datasource=druid
store.db.dbType=mysql
store.db.driverClassName=com.mysql.jdbc.Driver
store.db.url=jdbc:mysql://127.0.0.1:3306/seata?useUnicode=true 10 store.db.user=root
store.db.password=root
store.db.minConn=5
store.db.maxConn=30
store.db.globalTable=global_table
store.db.branchTable=branch_table
store.db.queryLimit=100
store.db.lockTable=lock_table
store.db.maxWait=5000
配置中心結果
步驟七:啟動Seata Server
源碼啟動: 執行server模塊下
io.seata.server.Server.java的main方法
命令啟動: bin/seata-server.sh -h 127.0.0.1 -p 8091 -m db -n 1 -e test
啟動Seata Server
bin/seata‐server.sh
啟動成功,默認端口8091
在注冊中心中可以查看到seata-server注冊成功
作者:心城以北
鏈接:
https://juejin.cn/post/7106501042778406948