seata服务搭建
它支持两种存储模式,一个是文件,一个是数据库,下面我们分别介绍一下这两种
配置nacos存储配置,注意如果registry.conf中注册和配置使用的是file,就会去读取file.config的配置,如果是nacos则通过nacos动态读取配置
用nacos作为注册中心了,所以我们接下来要做的就是讲配置上传到nacos进行存储
首先我们需要去官网下载Seata的源码,找到其中的script目录:
https://gitcode.net/mirrors/seata/seata
script的目录结构是这样的:
client:存放客户端SQL脚本的,参数配置
config-center:各个配置中心参数导入脚本,config.txt为通用参数文件
server:服务端数据库脚本及各个容器配置
进入config-center/nacos目录,执行脚本,添加nacos配置信息(不要忘记开启nacos服务哦),命令如下:
ps:下载完成后,将脚本文件放入到seata项目的配置文件夹下(seata-server-1.4.2\conf):
ps: 需要导入nacos的配置信息文件config.txt需要放在config的上一层目录
ps: 存储模式改成 store.mode=db ,因为我使用的是mysql8.0.17版本的,所以 store.db.driverClassName=com.mysql.cj.jdbc.Driver,需要另外下载jdbc的jar包,下载好后放入项目中seata-server-1.4.2\lib\jdbc的目录下:
剩下的就是配置数据库的相关配置信息(url、username、password):
store.db.url=jdbc:mysql://127.0.0.1:3306/seata?useUnicode=true&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai
store.db.user=root
store.db.password=123456
//执行脚本
sh ${SEATAPATH}/bin/nacos-config.sh -h localhost -p 8848 -g SEATA_GROUP -t 5a3c7d6c-f497-4d68-a71a-2e5e3340b3ca -u nacos-w nacos
Parameter Description:
-h: host, the default value is localhost.
-p: port, the default value is 8848.
-g: Configure grouping, the default value is 'SEATA_GROUP'.
-t: Tenant information, corresponding to the namespace ID field of Nacos, the default value is ''.
-u: username, nacos 1.2.0+ on permission control, the default value is ''.
-w: password, nacos 1.2.0+ on permission control, the default value is ''.
让我们看一下nacos那边的配置列表是否有对应的配置数据了:
很显然,已经添加成功了 ,那接下来我们就需要修改一下Seata服务端配置加载位置,那就需要修改registry.conf文件中的config那段配置,配置如下:
config {
# file、nacos 、apollo、zk、consul、etcd3
type = "nacos" #修改
nacos {
serverAddr = "127.0.0.1:8848" #修改
namespace = ""
group = "SEATA_GROUP" #跟上面的-g 命令保持一致即可
}
#以下代码省略...
}
1、存储模式
1、file存储模式
默认支持的存储模式,直接启动即可,不需要改动任何文件,file模式是单机模式,全局事务会话信息会持久化到本地文件:/bin/sessionStore/root.data,性能较高,具体的命令如下:
sh seata-server.sh -p 8091 -h 127.0.0.1 -m file
2、db存储模式
这个模式是高可用的模式,全局事务会话信息通过db共享的,性能较比file模式较差一些,具体的实现步骤如下:
①创建数据库,表结构如下:
-- 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(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;
②设置事务日志存储方式
进入conf/file.conf,修改属性store.mode="db"
③修改数据库连接
db {
datasource = "druid" ##修改
dbType = "mysql"
driverClassName = "com.mysql.jdbc.Driver"
url = "jdbc:mysql://127.0.0.1:3306/seata" ##修改
user = "mysql" ##修改
password = "mysql" ##修改
minConn = 1
maxConn = 10
globalTable = "global_table"
branchTable = "branch_table"
lockTable = "lock_table"
queryLimit = 100
}
④启动seata
seata-server.bat -h 127.0.0.1 -p 8091 -m db -n 1
关于后面的启动参数这里就不做陈述了,上面表格有详解
2、配置中心说明
Seata根目录config有两个配置文件,registry.conf和file.config
registry.con配置说明
这个文件中只包含了两项配置属相:registry和config,源码如下:
registry {
# file 、nacos 、eureka、redis、zk、consul、etcd3、sofa
type = "file"
nacos {
serverAddr = "localhost"
namespace = ""
cluster = "default"
}
eureka {
serviceUrl = "http://localhost:8761/eureka"
application = "default"
weight = "1"
}
redis {
serverAddr = "localhost:6379"
db = "0"
}
zk {
cluster = "default"
serverAddr = "127.0.0.1:2181"
session.timeout = 6000
connect.timeout = 2000
}
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"
}
consul {
serverAddr = "127.0.0.1:8500"
}
apollo {
app.id = "seata-server"
apollo.meta = "http://192.168.1.204:8801"
namespace = "application"
}
zk {
serverAddr = "127.0.0.1:2181"
session.timeout = 6000
connect.timeout = 2000
}
etcd3 {
serverAddr = "http://localhost:2379"
}
file {
name = "file.conf"
}
}
registry
它可以配置Seata服务注册的地址,支持现在市面上绝大多数的注册中心组件,配置也很简单,只需要修改type即可,然后在对应的配置下,配置好对应服务的ip相关信息即可,比如想用Nacos作为注册中心,那就type=nacos,然后修改ip,源码如下:
registry {
# file 、nacos 、eureka、redis、zk、consul、etcd3、sofa
type = "nacos" #修改,按照上面注释的单词来填写
nacos {
serverAddr = "127.0.0.1:8848" #对应nacos的相关配置
namespace = ""
cluster = "default"
}
# 以下代码省略...
}
这里注意一下,这个type默认是file,表示不用注册中心,如果是file,就可以不用注册中心快速运行Seata,只不过file没有注册中心的动态发现和动态配置的功能
config
用于配置Seata服务端的配置文件地址,意思就是说可以通过config配置来指定Seata服务端的配置信息的加载位置,它支持从远程配置中心读取和本地文件读取两种方式,如果配置为远程配置中心,可以使用type指定,配置形式和上面的registry一样
config {
# file、nacos 、apollo、zk、consul、etcd3
type = "nacos" #修改
nacos {
serverAddr = "127.0.0.1:8848" #修改
namespace = ""
group = "SEATA_GROUP"
}
# 以下代码省略...
}
需要注意的是,如果这里的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
# 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"
compressor = "none"
}
# service configuration, only used in client side
service {
#transaction service group mapping
vgroupMapping.my_test_tx_group = "default"
#only support when registry.type=file, please don't set multiple addresses
default.grouplist = "127.0.0.1:8091"
#degrade, current not support
enableDegrade = false
#disable seata
disableGlobalTransaction = false
}
#client transaction configuration, only used in client side
client {
rm {
asyncCommitBufferLimit = 10000
lock {
retryInterval = 10
retryTimes = 30
retryPolicyBranchRollbackOnConflict = true
}
reportRetryCount = 5
tableMetaCheckEnable = false
reportSuccessEnable = false
sqlParserType = druid
}
tm {
commitRetryCount = 5
rollbackRetryCount = 5
}
undo {
dataValidation = true
logSerialization = "jackson"
logTable = "undo_log"
}
log {
exceptionRate = 100
}
}
## 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 = "dbcp"
## mysql/oracle/h2/oceanbase etc.
dbType = "mysql"
driverClassName = "com.mysql.jdbc.Driver"
url = "jdbc:mysql://127.0.0.1:3306/seata"
user = "mysql"
password = "mysql"
minConn = 1
maxConn = 10
globalTable = "global_table" # db模式全局事务表名
branchTable = "branch_table" # db模式分支事务表名
lockTable = "lock_table" # db模式全局锁表名
queryLimit = 100 # db模式查询全局事务一次的最大条数
}
}
## server configuration, only used in server side
# server服务端配置
server {
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 {
# undo保留天数
logSaveDays = 7
#schedule delete expired undo_log in milliseconds
#undo清理线程间隔时间(ms)
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 side
## 监控设置
metrics {
# 是否启动
enabled = false
# 指标注册器类型
registryType = "compact"
# multi exporters use comma divided
# 指标结果prometheus数据输出器列表
exporterList = "prometheus"
# prometheus输出器Client端口号
exporterPrometheusPort = 9898
}