1.下載
首先我們要下載seata-server包,地址在http://seata.io/zh-cn/blog/download.html
seata-server-1.4.2.zip
2.解壓
在非中文目錄解壓seata-server-1.4.2.zip,其目錄結構如下:
3.修改配置
修改conf目錄下的registry.conf文件:
內容如下:
registry {
# tc服務的注冊中心類,這里選擇nacos,也可以是eureka、zookeeper等
# file 、nacos 、eureka、redis、zk、consul、etcd3、sofa
type = "nacos"
nacos {
application = "seata-tc-server"
serverAddr = "localhost:8848" #不要配成127.0.0.1:8848
group = "SEATA_GROUP"
namespace = ""
cluster = "SH"
username = "nacos"
password = "nacos"
}
}
config {
# 讀取tc服務端的配置文件的方式,這里是從nacos配置中心讀取,這樣如果tc是集群,可以共享配置
# file、nacos 、apollo、zk、consul、etcd3
type = "nacos"
# 配置nacos地址等信息
nacos {
serverAddr = "localhost:8848" #不要配成127.0.0.1:8848
namespace = ""
group = "SEATA_GROUP"
username = "nacos"
password = "nacos"
dataId = "seataServer.properties"
}
}
4.在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://47.*.*.*:33064/seata?useUnicode=true&rewriteBatchedStatements=true store.db.user=xk-dev store.db.password=xk-dev 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
注意:其中的數據庫地址、用戶名、密碼都需要修改成你自己的數據庫信息。
5.創建數據庫表
特別注意:tc服務在管理分布式事務時,需要記錄事務相關數據到數據庫中,你需要提前創建好這些表。
新建一個名為seata的數據庫,運行如下sql語句,這些表主要記錄全局事務、分支事務、全局鎖信息:

SET NAMES utf8mb4; SET FOREIGN_KEY_CHECKS = 0; -- ---------------------------- -- 分支事務表 -- ---------------------------- DROP TABLE IF EXISTS `branch_table`; CREATE TABLE `branch_table` ( `branch_id` bigint(20) NOT NULL, `xid` varchar(128) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL, `transaction_id` bigint(20) NULL DEFAULT NULL, `resource_group_id` varchar(32) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL, `resource_id` varchar(256) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL, `branch_type` varchar(8) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL, `status` tinyint(4) NULL DEFAULT NULL, `client_id` varchar(64) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL, `application_data` varchar(2000) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL, `gmt_create` datetime(6) NULL DEFAULT NULL, `gmt_modified` datetime(6) NULL DEFAULT NULL, PRIMARY KEY (`branch_id`) USING BTREE, INDEX `idx_xid`(`xid`) USING BTREE ) ENGINE = InnoDB CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Compact; -- ---------------------------- -- 全局事務表 -- ---------------------------- DROP TABLE IF EXISTS `global_table`; CREATE TABLE `global_table` ( `xid` varchar(128) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL, `transaction_id` bigint(20) NULL DEFAULT NULL, `status` tinyint(4) NOT NULL, `application_id` varchar(32) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL, `transaction_service_group` varchar(32) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL, `transaction_name` varchar(128) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL, `timeout` int(11) NULL DEFAULT NULL, `begin_time` bigint(20) NULL DEFAULT NULL, `application_data` varchar(2000) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL, `gmt_create` datetime NULL DEFAULT NULL, `gmt_modified` datetime NULL DEFAULT NULL, PRIMARY KEY (`xid`) USING BTREE, INDEX `idx_gmt_modified_status`(`gmt_modified`, `status`) USING BTREE, INDEX `idx_transaction_id`(`transaction_id`) USING BTREE ) ENGINE = InnoDB CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Compact; SET FOREIGN_KEY_CHECKS = 1;
6.啟動TC服務
進入bin目錄,運行其中的seata-server.bat即可:
啟動成功后,seata-server應該已經注冊到nacos注冊中心了。
打開瀏覽器,訪問nacos地址:http://localhost:8848,然后進入服務列表頁面,可以看到seata-tc-server的信息: