首先进行数据库的创建与用户授权操作。需要新建一个名为seata的数据库实例,并为该库分配相应的权限。具体而言,授予seata_user用户在所有主机('%')上对seata数据库的全部访问权限,并设置登录密码为'123455'。这一步是确保后续SEATA服务端能够正确读写事务日志的基础前提。
> create database seata;
> grant all on seata.* to seata_user@'%' identified by '123455'
接下来准备数据库建表语句。SEATA在采用数据库作为事务日志存储模式时,需要特定的表结构来保存全局事务、分支事务等状态信息。建表脚本通常包含global_table用于存储全局会话,branch_table用于存储分支会话,lock_table用于处理全局锁等关键数据表。这些表定义了xid、transaction_id、status等核心字段,以支撑分布式事务的状态追踪与管理。
cat create_seata_table.sql
-- -------------------------------- The script used when storeMode is 'db' --------------------------------
-- 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;
随后将准备好的建表SQL文件导入到数据库中。使用mysql命令行工具,指定用户名seata_user和密码,连接到之前创建的seata数据库,并将create_seata_table.sql脚本中的内容执行导入。此过程会在数据库中生成所需的元数据结构,为SEATA服务器后续的事务协调提供持久化存储支持。
mysql -useata_user -p seata < create_seata_table.sql
进入配置文件修改阶段,首要任务是调整file.conf以适配数据库存储模式。打开/home/seata-server/resources/file.conf文件,找到store模块下的配置项。将mode参数从默认的file或其他模式更改为"db",表明事务日志将存储在数据库中。同时,需在此处或关联的配置中填入正确的数据库连接信息,包括IP地址、端口、数据库名称以及刚才创建的seata_user账号密码,确保服务端能连通数据库。
cat /home/seata-server/resources/file.conf
## transaction log store, only used in seata-server
store {
## store mode: file、db、redis
mode = "db"
## rsa decryption public key
publicKey = ""
## 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"
## if using mysql to store the data, recommend add rewriteBatchedStatements=true in jdbc connection param
url = "jdbc:mysql://192.168.56.30:3306/seata?rewriteBatchedStatements=true" user = "seata_user"
password = "123455"
minConn = 5
maxConn = 100
globalTable = "global_table"
branchTable = "branch_table"
lockTable = "lock_table"
queryLimit = 100
maxWait = 5000
}
## redis store property
redis {
## redis mode: single、sentinel
mode = "single"
## single mode property
single {
host = "127.0.0.1"
port = "6379"
}
## sentinel mode property
sentinel {
masterName = ""
## such as "10.28.235.65:26379,10.28.235.65:26380,10.28.235.65:26381"
sentinelHosts = ""
}
password = ""
database = "0"
minConn = 1
maxConn = 10
maxTotal = 100
queryLimit = 100
}
}
其次需要修改registry.conf以配置服务注册发现机制。编辑/home/seata-server/resources/registry.conf文件,在registry模块中将type设置为"nacos",指定使用Nacos作为注册中心。接着在nacos子配置块中,填写application名称(如es-provider-seata)、serverAddr(如192.168.56.30:8848)、group(DEFAULT_GROUP)、namespace、cluster等信息,并配置username和password(均为nacos)。这使SEATA Server能够向Nacos注册自身,供客户端发现。
cat /home/seata-server/resources/registry.conf
registry {
# file 、nacos 、eureka、redis、zk、consul、etcd3、sofa
type = "nacos"
loadBalance = "RandomLoadBalance"
loadBalanceVirtualNodes = 10
nacos {
application = "es-provider-seata"
serverAddr = "192.168.56.30:8848"
group = "DEFAULT_GROUP"
namespace = ""
cluster = "default"
username = "nacos"
password = "nacos"
}
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"
aclToken = ""
}
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 = "127.0.0.1:8848"
namespace = ""
group = "SEATA_GROUP"
username = ""
password = ""
dataId = "seataServer.properties"
}
consul {
serverAddr = "127.0.0.1:8500"
aclToken = ""
}
apollo {
appId = "seata-server"
## apolloConfigService will cover apolloMeta
apolloMeta = "http://192.168.1.204:8801"
apolloConfigService = "http://192.168.1.204:8080"
namespace = "application"
apolloAccesskeySecret = ""
cluster = "seata"
}
zk {
serverAddr = "127.0.0.1:2181"
sessionTimeout = 6000
connectTimeout = 2000
username = ""
password = ""
}
etcd3 {
serverAddr = "http://localhost:2379"
}
file {
name = "file.conf"
}
}
完成配置后,执行Docker部署命令启动SEATA服务。使用docker run指令,定义容器名称为seata-server-latest,映射宿主机8091端口到容器内8091端口。通过-v参数将本地修改好的file.conf和registry.conf分别挂载至容器内的对应路径,同时将日志目录挂载以便排查问题。镜像选用seataio/seata-server:1.3.0版本。执行该命令后,SEATA Server将在容器中启动并加载上述配置。
docker run --name seata-server-latest -p 8091:8091 \
-v /home/seata-server/resources/file.conf:/seata-server/resources/file.conf \
-v /home/seata-server/resources/registry.conf:/seata-server/resources/registry.conf \
-v /home/seata-server/logs:/root/logs \
seataio/seata-server:1.3.0
至此,关于SEATA事务服务在Docker环境下的完整部署流程已介绍完毕。涵盖了从底层数据库准备、配置调整到容器化运行的关键环节。如需了解更多关于SEATA与Docker部署的相关细节,建议查阅本站历史文章或继续浏览后续相关内容。感谢阅读与支持。