Seata是一个需要独立部署的中间件,除了直接部署外,还支持多种部署方式,比如Docker、Kubernetes、Helm。本文主要介绍直接安装的方式。
在Seata官网下载1.2.0版本的安装包
进入${SEATA_HOME}\bin目录,根据系统类型执行相应的启动脚本。
seata支持设置启动参数,完整的参数列表如下
参数 | 全写 | 作用 | 备注 |
-h | --host | 指定在注册中心注册的ip | 不指定时获取当前的IP地址,外部访问部署在云环境和容器中的server,建议指定 |
-p | --port | 指定server的启动端口 | 默认为8091 |
-m | --storeMode | 事务日志的存储方式 | 支持file和db,默认为file |
-n | --serverNode | seata server的节点 | 如 1,2,3… 默认为1 |
-e | --seataEnv | seata server的运行环境 | 如dev、test等,服务启动时会使用registry-dev.conf这样的配置 |
file存储模式
Server端存储模式(store.mode) 有file、db两种,file存储模式无需改动,直接启动即可。
file存储模式为单机模式,全局事务会话信息持久在本地文件${SEATA_HOME}\bin\sessionStore\root.data中,性能较高,启动命令如下:
sh seata-server.sh -p 8091 -h 127.0.0.1 -m file
db存储模式
db存储模式为高可用模式,全局事务会话信息通过db共享,性能相对差一些。操作步骤如下:
创建表结构,Seata全局事务会话信息由全局事务、分支事务、全局锁构成,对应表globaltable、branchtable、lock_table。
-- -------------------------------- The script used when storeMode is 'db' ---------------------------------- the table to store GlobalSession dataCREATE 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 dataCREATE 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 dataCREATE 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;
设置事务日志存储方式,进入${SEATA_HOME}\config\file.conf,修改store.mode="db"。
修改数据库连接信息:
db { ## the implement of javax.sql.DataSource, such as DruidDataSource(druid)/BasicDataSource(dbcp) etc. datasource = "druid" ## mysql/oracle/postgresql/h2/oceanbase etc. dbType = "mysql" driverClassName = "com.mysql.jdbc.Driver" url = "jdbc:mysql://127.0.0.1:3306/seata" user = "mysql" password = "mysql" minConn = 5 maxConn = 30 globalTable = "global_table" branchTable = "branch_table" lockTable = "lock_table" queryLimit = 100 maxWait = 5000 }
启动seata-server:
seata-server.sh -h 127.0.0.1 -p 8091 -m db -n 1
参数说明如下:
-h: 注册到注册中心的IP地址,Seata-Server可以把自己注册到注册中心,支持Nacos、Eureka、Redis、Zookeeper、Consul、Etcd3、Sofa。
-p: Server RPC监听端口。
-m: 全局事务会话信息存储模式,包括file、db,优先读取启动参数。
-n: Server node,有多个server时,需区分各自节点,用于生成不同区间的transactionId,以免冲突。
Seata服务端配置中心说明
在${SEATA_HOME}\conf目录下有两个配置文件,分别是registry.conf和file.conf。
registry.conf配置说明
registry.conf中包含两项配置:registry、config
registry { # file 、nacos 、eureka、redis、zk、consul、etcd3、sofa type = "file" nacos { application = "seata-server" serverAddr = "localhost" namespace = "" cluster = "default" username = "" password = "" } eureka { serviceUrl = "http://localhost:8761/eureka" application = "default" weight = "1" } redis { serverAddr = "localhost:6379" db = 0 password = "" cluster = "default" timeout = 0 } zk { cluster = "default" serverAddr = "127.0.0.1:2181" sessionTimeout = 6000 connectTimeout = 2000 username = "" password = "" } consul { cluster = "default" serverAddr = "127.0.0.1:8500" } etcd3 { cluster = "default" serverAddr = "http://localhost:2379" } sofa { serverAddr = "127.0.0.1:9603" application = "default" region = "DEFAULT_ZONE" datacenter = "DefaultDataCenter" cluster = "default" group = "SEATA_GROUP" addressWaitTime = "3000" } file { name = "file.conf" }}config { # file、nacos 、apollo、zk、consul、etcd3 type = "file" nacos { serverAddr = "localhost" namespace = "" group = "SEATA_GROUP" username = "" password = "" } consul { serverAddr = "127.0.0.1:8500" } apollo { appId = "seata-server" apolloMeta = "http://192.168.1.204:8801" namespace = "application" } zk { serverAddr = "127.0.0.1:2181" sessionTimeout = 6000 connectTimeout = 2000 username = "" password = "" } etcd3 { serverAddr = "http://localhost:2379" } file { name = "file.conf" }}
registry表示配置Seata服务注册的地址,支持目前市面上所有主流的注册中心组件,它的配置非常简单,通过type指定注册中心的类型,然后根据指定的类型配置对应的服务地址信息,比如当type=nacos时,则匹配到Nacos的配置如下:
type = "nacos" nacos { application = "seata-server" serverAddr = "localhost" namespace = "" cluster = "default" username = "" password = "" }
type默认为file,它表示不依赖于配置中心,在file类型下,可以不依赖第三方注册中心快速集成Seata,不过,file类型不具备注册中心的动态发现和动态配置功能。
config
config配置用于配置Seata服务端的配置文件地址,也就是可以通过config配置指定Seata服务端的配置信息的加载位置,它支持从远程配置中心读取和本地文件读取两种方式。如果配置为远程配置中心,可以使用type指定,配置形式和registry相同。
type = "nacos" nacos { serverAddr = "localhost" namespace = "" group = "SEATA_GROUP" username = "" password = "" }
在默认情况下type=file,它会加载file.conf文件中的配置信息。
file.conf配置说明
file.conf存储的是Seata服务端的配置信息,完整配置如下。它包含transport、server、metrics,分别表示协议配置、服务配置、监控。
transport { # tcp udt unix-domain-socket type = "TCP" #NIO NATIVE server = "NIO" #enable heartbeat heartbeat = true #client和server通信心跳检测开关 # the client batch send request enable enableClientBatchSendRequest = false #thread factory for netty threadFactory { bossThreadPrefix = "NettyBoss" workerThreadPrefix = "NettyServerNIOWorker" serverExecutorThreadPrefix = "NettyServerBizHandler" shareBossWorker = false clientSelectorThreadPrefix = "NettyClientSelector" clientSelectorThreadSize = 1 clientWorkerThreadPrefix = "NettyClientWorkerThread" # netty boss thread size,will not be used for UDT bossThreadSize = 1 #auto default pin or 8 workerThreadSize = "default" } shutdown { # when destroy server, wait seconds wait = 3 } serialization = "seata" #client和server通信编解码方式 compressor = "none" #client和server通信数据压缩方式(none, gzip)}## transaction log store, only used in server side## 事务日志存储配置store { ## store mode: file、db mode = "file" ## 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) etc. datasource = "druid" ## mysql/oracle/postgresql/h2/oceanbase etc. dbType = "mysql" driverClassName = "com.mysql.jdbc.Driver" url = "jdbc:mysql://127.0.0.1:3306/seata" user = "mysql" password = "mysql" minConn = 5 maxConn = 30 globalTable = "global_table" # db模式全局事务表名 branchTable = "branch_table" # db模式分支事务表名 lockTable = "lock_table" # db模式全局锁表名 queryLimit = 100 # db模式查询全局事务一次的最大条数 }}## server configuration, only used in server sideserver { recovery { #schedule committing retry period in milliseconds 两阶段提交未完成状态全局事务重试提交线程间隔时间 committingRetryPeriod = 1000 #schedule asyn committing retry period in milliseconds 两阶段异步提交状态重试提交线程间隔时间 asynCommittingRetryPeriod = 1000 #schedule rollbacking retry period in milliseconds 两阶段回滚状态重试回滚线程间隔时间 rollbackingRetryPeriod = 1000 #schedule timeout retry period in milliseconds 超时状态检测重试线程间隔时间 timeoutRetryPeriod = 1000 } undo { logSaveDays = 7 # undo保留天数 #schedule delete expired undo_log in milliseconds undo清理线程间隔时间(毫秒) logDeletePeriod = 86400000 } #unit ms,s,m,h,d represents milliseconds, seconds, minutes, hours, days, default permanent maxCommitRetryTimeout = "-1" maxRollbackRetryTimeout = "-1" rollbackRetryTimeoutUnlockEnable = false}## metrics configuration, only used in server sidemetrics { enabled = false registryType = "compact" # multi exporters use comma divided exporterList = "prometheus" exporterPrometheusPort = 9898}
Seata服务端启动时会加载file.conf中的配置参数。
从配置中心加载配置
Seata服务在启动时可以将自己注册到注册中心上,并且file.conf文件中的配置同样可以保存在配置中心,接下来可以尝试把配置信息存储到Nacos上。
将配置上传到Nacos
在GitHub官网代码中下载Seata的源码,在源码包中有一个script文件夹,目录结构如下:
client:存放客户端的SQL脚本,参数配置。
config-center:各个配置中心参数导入脚本,config.txt(包含server和client)为通用参数文件。
server:服务端数据库脚本及各个容器配置
进入config-center目录,包含config.txt和不同配置中心的目录(该目录下包含shell脚本和py脚本)。其中config.txt存放的是Seata客户端和服务端的所有配置信息。
在config-center\nacos目录下,执行如下脚本
sh nacos-config.sh -h 192.168.216.128 -p 8848 -g SEATA-GROUP
该脚本的作用是把config.txt中的配置信息上传到Nacos配置中心。由于config.txt中提供的是默认配置,在实际使用时可以先修改该文件中的内容,再执行上传操作。
脚本如果执行正确,就可以在Nacos中看到配置列表。
Seata服务端修改配置加载位置
进入${SEATA_HOME}\conf目录,修改registry.conf文件中的config段:
registry { # file 、nacos 、eureka、redis、zk、consul、etcd3、sofa type = "nacos" nacos { application = "seata-server" serverAddr = "localhost" namespace = "" cluster = "default" username = "" password = "" }}config { # file、nacos 、apollo、zk、consul、etcd3 type = "nacos" nacos { serverAddr = "localhost" namespace = "" group = "SEATA_GROUP" username = "" password = "" } }
至此,便完成了Seata服务端的注册和统一配置的管理。