Seata分TC、TM和RM三個角色,TC(Server端)為單獨服務端部署,TM和RM(Client端)由業務系統集成。
https://seata.io/zh-cn/docs/ops/deploy-guide-beginner.html
下載安裝包
https://github.com/seata/seata/releases


windows 安裝 Seata
解壓seata-server-1.3.0.zip

Server端存儲模式(store.mode)現有file、db、redis三種(后續將引入raft,mongodb),file模式無需改動,直接啟動即可,下面專門講下db和redis啟動步驟。
注: file模式為單機模式,全局事務會話信息內存中讀寫並持久化本地文件root.data,性能較高;
db模式為高可用模式,全局事務會話信息通過db共享,相應性能差些;(mysql 5.7+)
redis模式Seata-Server 1.3及以上版本支持,性能較高,存在事務信息丟失風險,請提前配置合適當前場景的redis持久化配置.
打開conf目錄下的file.conf

修改 mode = "db" 及數據連接配置信息
修改store.mode
啟動包: seata-->conf-->file.conf,修改store.mode="db或者redis"
源碼: 根目錄-->seata-server-->resources-->file.conf,修改store.mode="db或者redis"
## transaction log store, only used in seata-server store { ## store mode: file、db、redis ## mode = "file" mode = "db" ## 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" url = "jdbc:mysql://127.0.0.1:3406/seata_server" user = "root" password = "Root&123456" minConn = 5 maxConn = 30 globalTable = "global_table" branchTable = "branch_table" lockTable = "lock_table" queryLimit = 100 maxWait = 5000 } ## redis store property redis { host = "127.0.0.1" port = "6379" password = "" database = "0" minConn = 1 maxConn = 10 queryLimit = 100 } }
修改數據庫連接|redis屬性配置
啟動包: seata-->conf-->file.conf,修改store.db或store.redis相關屬性。
源碼: 根目錄-->seata-server-->resources-->file.conf,修改store.db或store.redis相關屬性。
找到sql角本創建上面配置的seata對應數據庫

下載解壓

建表(僅db)



全局事務會話信息由3塊內容構成,全局事務-->分支事務-->全局鎖,對應表global_table、branch_table、lock_table
修改 nacos 注冊中心





修改 nacos 配置中心


https://seata.io/zh-cn/docs/user/txgroup/transaction-group-and-ha.html
最佳實踐1:TC的異地多機房容災
- 假定TC集群部署在兩個機房:guangzhou機房(主)和shanghai機房(備)各兩個實例
- 一整套微服務架構項目:projectA
- projectA內有微服務:serviceA、serviceB、serviceC 和 serviceD
其中,projectA所有微服務的事務分組tx-service-group設置為:projectA,projectA正常情況下使用guangzhou的TC集群(主)
那么正常情況下,client端的配置如下所示:
seata.tx-service-group=projectA
seata.service.vgroup-mapping.projectA=Guangzhou

假如此時guangzhou集群分組整個down掉,或者因為網絡原因projectA暫時無法與Guangzhou機房通訊,那么我們將配置中心中的Guangzhou集群分組改為Shanghai,如下:
seata.service.vgroup-mapping.projectA=Shanghai
並推送到各個微服務,便完成了對整個projectA項目的TC集群動態切換。

配置事務分組,要與客戶端配置的事務分組一致
# my_test_tx_group 需要與客戶端保持一致 default需要跟客戶端和registry.conf中registry中的cluster保持一致
(客戶端properties配置:spring.cloud.alibaba.seata.tx-service-group=my_test_tx_group)
將 seata-1.3.0\script 文件夾復制到 seata-server-1.3.0\seata 下

資源目錄介紹
點擊查看
- client
存放client端sql腳本 (包含 undo_log表) ,參數配置
- config-center
各個配置中心參數導入腳本,config.txt(包含server和client,原名nacos-config.txt)為通用參數文件
- server
server端數據庫腳本 (包含 lock_table、branch_table 與 global_table) 及各個容器配置


Server端存儲模式(store.mode)現有file、db、redis三種(后續將引入raft,mongodb),file模式無需改動,直接啟動即可,下面專門講下db和redis啟動步驟。
注: file模式為單機模式,全局事務會話信息內存中讀寫並持久化本地文件root.data,性能較高;
db模式為高可用模式,全局事務會話信息通過db共享,相應性能差些;
redis模式Seata-Server 1.3及以上版本支持,性能較高,存在事務信息丟失風險,請提前配置合適當前場景的redis持久化配置.

事務分組:異地機房停電容錯機制
my_test_tx_group 可以自定義 比如:(guangzhou、shanghai ...) ,對應的client也要去設置
seata.service.vgroup-mapping.my_test_tx_group=Guangzhou
default 必須要等於 registry.conf cluster = "default"

sh E:\SpringCloud\seata-server-1.3.0\seata\script\config-center\nacos\nacos-config.sh -h 192.168.133.129 -p 8847 -g SEATA_GROUP -t c8296d30-25bc-47f2-95a0-5698e69aa6d7

sh nacos-config.sh -h localhost -p 8848 -g SEATA_GROUP -u nacos -w nacos


sh nacos-config.sh -h 192.168.133.129 -p 8847 -g SEATA_GROUP





在nacos添加配置
特別注意,為了讓tc服務的集群可以共享配置,我們選擇了nacos作為統一配置中心。因此服務端配置文件seataServer.properties文件需要在nacos中配好。
# 數據存儲方式,db代表數據庫 store.mode=db store.db.datasource=druid store.db.dbType=mysql store.db.driverClassName=com.mysql.cj.jdbc.Driver store.db.url=jdbc:mysql://127.0.0.1:3406/seata_server?useUnicode=true&rewriteBatchedStatements=true store.db.user=root store.db.password=Root&123456 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 # 事務、日志等配置 server.recovery.committingRetryPeriod=1000 server.recovery.asynCommittingRetryPeriod=1000 server.recovery.rollbackingRetryPeriod=1000 server.recovery.timeoutRetryPeriod=1000 server.maxCommitRetryTimeout=-1 server.maxRollbackRetryTimeout=-1 server.rollbackRetryTimeoutUnlockEnable=false server.undo.logSaveDays=7 server.undo.logDeletePeriod=86400000 # 客戶端與服務端傳輸方式 transport.serialization=seata transport.compressor=none # 關閉metrics功能,提高性能 metrics.enabled=false metrics.registryType=compact metrics.exporterList=prometheus metrics.exporterPrometheusPort=9898
注意:其中的數據庫地址、用戶名、密碼都需要修改成你自己的數據庫信息。

mysql數據庫版本和代碼使用的版本不一致導致的,mysql5.x版本和8.x版本驅動不同
所以需要修改數據庫連接驅動:
老版本驅動,一般指5.x版本:
driverClassName: com.mysql.jdbc.Driver
新版本驅動,一般指8.x版本:
driverClassName: com.mysql.cj.jdbc.Driver
啟動
- 源碼啟動: 執行Server.java的main方法
- 命令啟動: seata-server.sh -h 127.0.0.1 -p 8091 -m db -n 1 -e test
-h: 注冊到注冊中心的ip
-p: Server rpc 監聽端口
-m: 全局事務會話信息存儲模式,file、db、redis,優先讀取啟動參數 (Seata-Server 1.3及以上版本支持redis)
-n: Server node,多個Server時,需區分各自節點,用於生成不同區間的transactionId,以免沖突
-e: 多環境配置參考 http://seata.io/en-us/docs/ops/multi-configuration-isolation.html

