一、下载Seata Server
下载seata最新发布的server编译启动程序,下载地址:Seata Server最新版本下载
Linux选择下载seata-server-1.2.0.tar.gz,Windows选择下载seata-server-1.2.0.zip。
二、解压Seata Server
tar -zxvf seata-server-1.2.0.tar.gz
三、修改配置文件
cd /seata/conf
1、registry.conf配置
默认情况下,seata-server的配置模式是file模式,删除其他无用的配置方式后,registry.conf的结构精简如下:
registry {
type = "nacos"
nacos {
application = "seata-server" #Nacos服务名称
serverAddr = "10.101.128.148:8848" #Nacos服务地址
namespace = "dev" #Nacos命名空间
cluster = "default" #集群模式
}
}
config {
type = "file"
file {
name = "file.conf" #Seata配置文件(同级file.conf文件)
}
}
2、file.conf配置
seata-server的存储模式有file和db两种,可以通过store.mode属性配置,默认的存储方式是file。
file模式下,seata的事务相关信息会走内存,并持久化到root.data文件中,这种模式性能较高。
db模式是一种高可用的模式,seata的全局事务,分支事务和锁都在数据库中存储,相关表在mkz_seata.sql文件中。
我选择的是db模式,只需要修改数据库连接,用户名和密码,具体如下:
transport {
type = "TCP"
server = "NIO"
heartbeat = true
enableClientBatchSendRequest = false
threadFactory {
bossThreadPrefix = "NettyBoss"
workerThreadPrefix = "NettyServerNIOWorker"
serverExecutorThreadPrefix = "NettyServerBizHandler"
shareBossWorker = false
clientSelectorThreadPrefix = "NettyClientSelector"
clientSelectorThreadSize = 1
clientWorkerThreadPrefix = "NettyClientWorkerThread"
bossThreadSize = 1
workerThreadSize = "default"
}
shutdown {
wait = 3
}
serialization = "seata"
compressor = "none"
}
store {
mode = "db" #采用DB模式
db {
datasource = "druid"
dbType = "mysql"
driverClassName = "com.mysql.cj.jdbc.Driver" #根据数据库版本选择驱动程序
url = "jdbc:mysql://10.101.128.148:3306/mkz_seata" #修改数据库连接
user = "root" #数据库连接用户名
password = "Mkz_1234" #数据库连接密码
minConn = 5
maxConn = 30
globalTable = "global_table"
branchTable = "branch_table"
lockTable = "lock_table"
queryLimit = 100
}
}
server {
recovery {
committingRetryPeriod = 1000
asynCommittingRetryPeriod = 1000
rollbackingRetryPeriod = 1000
timeoutRetryPeriod = 1000
}
undo {
logSaveDays = 7
logDeletePeriod = 86400000
}
maxCommitRetryTimeout = "-1"
maxRollbackRetryTimeout = "-1"
rollbackRetryTimeoutUnlockEnable = false
}
metrics {
enabled = false
registryType = "compact"
exporterList = "prometheus"
exporterPrometheusPort = 9898
}
四、数据脚本
/*
Navicat MySQL Data Transfer
Source Server : MkzCloud
Source Server Version : 80020
Source Host : 10.101.128.148:3306
Source Database : mkz_seata
Target Server Type : MYSQL
Target Server Version : 80020
File Encoding : 65001
Date: 2020-05-06 17:20:42
*/
SET FOREIGN_KEY_CHECKS=0;
-- ----------------------------
-- Table structure for branch_table
-- ----------------------------
DROP TABLE IF EXISTS `branch_table`;
CREATE TABLE `branch_table` (
`branch_id` bigint NOT NULL,
`xid` varchar(128) NOT NULL,
`transaction_id` bigint DEFAULT NULL,
`resource_group_id` varchar(32) DEFAULT NULL,
`resource_id` varchar(256) DEFAULT NULL,
`branch_type` varchar(8) DEFAULT NULL,
`status` tinyint DEFAULT NULL,
`client_id` varchar(64) DEFAULT NULL,
`application_data` varchar(2000) DEFAULT NULL,
`gmt_create` datetime(6) DEFAULT NULL,
`gmt_modified` datetime(6) DEFAULT NULL,
PRIMARY KEY (`branch_id`),
KEY `idx_xid` (`xid`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
-- ----------------------------
-- Table structure for global_table
-- ----------------------------
DROP TABLE IF EXISTS `global_table`;
CREATE TABLE `global_table` (
`xid` varchar(128) NOT NULL,
`transaction_id` bigint DEFAULT NULL,
`status` tinyint NOT NULL,
`application_id` varchar(32) DEFAULT NULL,
`transaction_service_group` varchar(32) DEFAULT NULL,
`transaction_name` varchar(128) DEFAULT NULL,
`timeout` int DEFAULT NULL,
`begin_time` bigint DEFAULT NULL,
`application_data` varchar(2000) DEFAULT NULL,
`gmt_create` datetime DEFAULT NULL,
`gmt_modified` datetime DEFAULT NULL,
PRIMARY KEY (`xid`),
KEY `idx_gmt_modified_status` (`gmt_modified`,`status`),
KEY `idx_transaction_id` (`transaction_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
-- ----------------------------
-- Table structure for lock_table
-- ----------------------------
DROP TABLE IF EXISTS `lock_table`;
CREATE TABLE `lock_table` (
`row_key` varchar(128) NOT NULL,
`xid` varchar(96) DEFAULT NULL,
`transaction_id` bigint DEFAULT NULL,
`branch_id` bigint NOT NULL,
`resource_id` varchar(256) DEFAULT NULL,
`table_name` varchar(32) DEFAULT NULL,
`pk` varchar(36) DEFAULT NULL,
`gmt_create` datetime DEFAULT NULL,
`gmt_modified` datetime DEFAULT NULL,
PRIMARY KEY (`row_key`),
KEY `idx_branch_id` (`branch_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
五、启动Seata服务
cd /seata/bin
nohup sh seata-server.sh -p 8091 -h 127.0.0.1 -m db &
脚本参数:
-p 指定启动seata server的端口号。
-h 指定seata server所绑定的主机,这里配置要注意指定的主机IP要与业务服务内的配置文件保持一致,如:-h 192.168.1.10,业务服务配置文件内应该配置192.168.1.10,即使在同一台主机上也要保持一致。
-m 事务日志、事务执行信息存储的方式,目前支持file、db。