Seata 搭建 db模式
版本 V1.6.1
一、 简介
Server端存储模式(store.mode)现有file、db、redis三种(后续将引入raft,mongodb),file模式无需改动,直接启动即可,下面专门讲下db和redis启动步骤。
注: file模式为单机模式,全局事务会话信息内存中读写并持久化本地文件root.data,性能较高;
db模式为高可用模式,全局事务会话信息通过db共享,相应性能差些;(推荐)
redis模式Seata-Server 1.3及以上版本支持,性能较高,存在事务信息丢失风险,请提前配置合适当前场景的redis持久化配置.
二、下载
1、官网 https://seata.io/zh-cn/
2、参考文档:https://seata.io/zh-cn/blog/seata-quick-start.html
3、部署指南:https://seata.io/zh-cn/docs/ops/deploy-guide-beginner.html
三、建表(仅db)
全局事务会话信息由3块内容构成,全局事务–>分支事务–>全局锁,对应表global_table、branch_table、lock_table
当前版本支持 3大数据库:mysql、postgresql 、oracle。
- 解压缩: seata-1.6.1.tar.gz
- mysql初始化脚本:seata/script/server/db/mysql.sql
DROP DATABASE IF EXISTS `sxxc_seata`;
CREATE DATABASE `sxxc_seata` DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci;
SET NAMES utf8mb4;
SET FOREIGN_KEY_CHECKS = 0;
USE `sxxc_seata`;
-- -------------------------------- 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_status_gmt_modified` (`status` , `gmt_modified`),
KEY `idx_transaction_id` (`transaction_id`)
) ENGINE = InnoDB
DEFAULT CHARSET = utf8mb4;
-- 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 = utf8mb4;
-- the table to store lock data
CREATE TABLE IF NOT EXISTS `lock_table`
(
`row_key` VARCHAR(128) NOT NULL,
`xid` VARCHAR(128),
`transaction_id` BIGINT,
`branch_id` BIGINT NOT NULL,
`resource_id` VARCHAR(256),
`table_name` VARCHAR(32),
`pk` VARCHAR(36),
`status` TINYINT NOT NULL DEFAULT '0' COMMENT '0:locked ,1:rollbacking',
`gmt_create` DATETIME,
`gmt_modified` DATETIME,
PRIMARY KEY (`row_key`),
KEY `idx_status` (`status`),
KEY `idx_branch_id` (`branch_id`),
KEY `idx_xid` (`xid`)
) ENGINE = InnoDB
DEFAULT CHARSET = utf8mb4;
CREATE TABLE IF NOT EXISTS `distributed_lock`
(
`lock_key` CHAR(20) NOT NULL,
`lock_value` VARCHAR(20) NOT NULL,
`expire` BIGINT,
primary key (`lock_key`)
) ENGINE = InnoDB
DEFAULT CHARSET = utf8mb4;
INSERT INTO `distributed_lock` (lock_key, lock_value, expire) VALUES ('AsyncCommitting', ' ', 0);
INSERT INTO `distributed_lock` (lock_key, lock_value, expire) VALUES ('RetryCommitting', ' ', 0);
INSERT INTO `distributed_lock` (lock_key, lock_value, expire) VALUES ('RetryRollbacking', ' ', 0);
INSERT INTO `distributed_lock` (lock_key, lock_value, expire) VALUES ('TxTimeoutCheck', ' ', 0);
四、配置 seata server 参数
4.1、V1.4.2之前方式
1. 修改config.txt
config.txt位置: seata/seata/script/config-center/config.txt
#For details about configuration items, see https://seata.io/zh-cn/docs/user/configurations.html
#Transport configuration, for client and server
transport.type=TCP
transport.server=NIO
transport.heartbeat=true
transport.enableTmClientBatchSendRequest=false
transport.enableRmClientBatchSendRequest=true
transport.enableTcServerBatchSendResponse=false
transport.rpcRmRequestTimeout=30000
transport.rpcTmRequestTimeout=30000
transport.rpcTcRequestTimeout=30000
transport.threadFactory.bossThreadPrefix=NettyBoss
transport.threadFactory.workerThreadPrefix=NettyServerNIOWorker
transport.threadFactory.serverExecutorThreadPrefix=NettyServerBizHandler
transport.threadFactory.shareBossWorker=false
transport.threadFactory.clientSelectorThreadPrefix=NettyClientSelector
transport.threadFactory.clientSelectorThreadSize=1
transport.threadFactory.clientWorkerThreadPrefix=NettyClientWorkerThread
transport.threadFactory.bossThreadSize=1
transport.threadFactory.workerThreadSize=default
transport.shutdown.wait=3
transport.serialization=seata
transport.compressor=none
#Transaction routing rules configuration, only for the client
service.vgroupMapping.default_tx_group=default
#If you use a registry, you can ignore it
service.default.grouplist=172.22.1.190:8091
service.enableDegrade=false
service.disableGlobalTransaction=false
#Transaction rule configuration, only for the client
client.rm.asyncCommitBufferLimit=10000
client.rm.lock.retryInterval=10
client.rm.lock.retryTimes=30
client.rm.lock.retryPolicyBranchRollbackOnConflict=true
client.rm.reportRetryCount=5
client.rm.tableMetaCheckEnable=true
client.rm.tableMetaCheckerInterval=60000
client.rm.sqlParserType=druid
client.rm.reportSuccessEnable=false
client.rm.sagaBranchRegisterEnable=false
client.rm.sagaJsonParser=fastjson
client.rm.tccActionInterceptorOrder=-2147482648
client.tm.commitRetryCount=5
client.tm.rollbackRetryCount=5
client.tm.defaultGlobalTransactionTimeout=60000
client.tm.degradeCheck=false
client.tm.degradeCheckAllowTimes=10
client.tm.degradeCheckPeriod=2000
client.tm.interceptorOrder=-2147482648
client.undo.dataValidation=true
client.undo.logSerialization=jackson
client.undo.onlyCareUpdateColumns=true
server.undo.logSaveDays=7
server.undo.logDeletePeriod=86400000
client.undo.logTable=undo_log
client.undo.compress.enable=true
client.undo.compress.type=zip
client.undo.compress.threshold=64k
#For TCC transaction mode
tcc.fence.logTableName=tcc_fence_log
tcc.fence.cleanPeriod=1h
#Log rule configuration, for client and server
log.exceptionRate=100
#Transaction storage configuration, only for the server. The file, db, and redis configuration values are optional.
store.mode=db
store.lock.mode=db
store.session.mode=db
#Used for password encryption
store.publicKey=""
#These configurations are required if the `store mode` is `db`. If `store.mode,store.lock.mode,store.session.mode` are not equal to `db`, you can remove the configuration block.
store.db.datasource=druid
store.db.dbType=mysql
store.db.driverClassName=com.mysql.jdbc.Driver
store.db.url=jdbc:mysql://172.22.2.187:3306/sxxc_seata?useUnicode=true&rewriteBatchedStatements=true
store.db.user=root
store.db.password=StQzZ&YW6Kt!PcXP
store.db.minConn=5
store.db.maxConn=30
store.db.globalTable=global_table
store.db.branchTable=branch_table
store.db.distributedLockTable=distributed_lock
store.db.queryLimit=100
store.db.lockTable=lock_table
store.db.maxWait=5000
#Transaction rule configuration, only for the server
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.distributedLockExpireTime=10000
server.xaerNotaRetryTimeout=60000
server.session.branchAsyncQueueSize=5000
server.session.enableBranchAsyncRemove=false
server.enableParallelRequestHandle=false
#Metrics configuration, only for the server
metrics.enabled=false
metrics.registryType=compact
metrics.exporterList=prometheus
metrics.exporterPrometheusPort=9898
- 初始化到nacos脚本
进入目录: cd ./seata/script/config-center
修改config.txt: 具体参考 ”修改config.txt“
sh nacos-config.sh -h 172.22.1.190 -p 8848 -g SEATA_GROUP -t d229c141-52de-4e60-bd4f-216391283c58 -u nacos -w SOPQzj312#!40Cc
-h – nacos ip
-p – nacos 端口
-g – seata-server配置文件 分组名
-t – seata-server配置文件 命名空间ID,当然在此之前需要现在nacos上新建命名空间并记录下命名空间ID
-u – nacos账号
-w – nacos密码
4.2、V1.4.2 之后推荐方式(seataServer.properties)
Seata从v1.4.2版本开始,支持从一个Nacos dataId配置项中获取所有配置信息。所以,在nacos配置中心中新建配置,dataId为 seataServer.properties配置项
- 在nacos中 添加dataID:seataServer.properties文件,具体内容还是config.txt中的配置
五、配置Server
5.1、 修改 appplication.yml
cd ./seata/conf/application.yml
如下参考:
# Copyright 1999-2019 Seata.io Group.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
server:
port: 7091
spring:
application:
name: seata-server
logging:
config: classpath:logback-spring.xml
file:
path: ${user.home}/logs/seata
extend:
logstash-appender:
destination: 127.0.0.1:4560
kafka-appender:
bootstrap-servers: 127.0.0.1:9092
topic: logback_to_logstash
console:
user:
username: xxx
password: xxx
seata:
config:
# support: nacos, consul, apollo, zk, etcd3
type: nacos
nacos:
server-addr: 172.22.1.190:8848
group: DEFAULT_GROUP
username: xxx
password: xxx
context-path:
##if use MSE Nacos with auth, mutex with username/password attribute
#access-key:
#secret-key:
data-id: seataServer.properties
registry:
# support: nacos, eureka, redis, zk, consul, etcd3, sofa
type: nacos
preferred-networks: 172.22.*
nacos:
application: seata-server
server-addr: xxx:8848
group: test
cluster: default
username: nacos
password: SOPQzj312#!40Cc
context-path:
##if use MSE Nacos with auth, mutex with username/password attribute
#access-key:
#secret-key:
store:
# support: file 、 db 、 redis
mode: db
db:
datasource: druid
db-type: mysql
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql://xxx:3306/sxxc_seata?rewriteBatchedStatements=true
user: xxx
password: xxx
min-conn: 10
max-conn: 100
global-table: global_table
branch-table: branch_table
lock-table: lock_table
distributed-lock-table: distributed_lock
query-limit: 1000
max-wait: 5000
security:
secretKey: SeataSecretKey0c382ef121d778043159209298fd40bf3850a017
tokenValidityInMilliseconds: 1800000
ignore:
urls: /,/**/*.css,/**/*.js,/**/*.html,/**/*.map,/**/*.svg,/**/*.png,/**/*.ico,/console-fe/public/**,/api/v1/auth/login
5.1.1、 修改 appplication.yml seata.store (db)
seata:
store:
# support: file 、 db 、 redis
mode: db
db:
datasource: druid
db-type: mysql
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql://xxxxx:3306/seata?rewriteBatchedStatements=true
user: xxxx # mysql的用户名
password: xxx #mysql的密码
min-conn: 5
max-conn: 100
global-table: global_table
branch-table: branch_table
lock-table: lock_table
distributed-lock-table: distributed_lock
query-limit: 100
max-wait: 5000
5.1.2、 修改 appplication.yml seata.config
seata:
config:
# support: nacos, consul, apollo, zk, etcd3
type: nacos
nacos:
server-addr: 127.0.0.1:8848
group : "SEATA_GROUP"
namespace: "xxxxx"
username: "xxx" # nacos的用户名
password: "xxx" # nacos的密码
5.1.3、 修改 appplication.yml seata.registry
seata:
registry:
# support: nacos, eureka, redis, zk, consul, etcd3, sofa
type: nacos
nacos:
application: "seata-server"
serverAddr: 127.0.0.1:8848
group: "SEATA_GROUP"
namespace: "xxxxxx"
username: "xxxx" # nacos的用户名
password: "xxx" # nacos的密码
5.2、启动Server
进入目录 cd ./seata/bin
sh seata-server.sh -h 172.22.1.190 -p 8091 -m db
5.3、查看nacos控台台
5.4、访问seata控制台
http://xxx:7091/
六、总结
seata安装版本是1.6.1,版本不同,安装流程也可能不同,这里的版本需要保持一致
- 执行sql创建数据表
- 使用脚本添加配置到nacos配置中心
- 修改application.yml文件,分别修改store、config、registry相关配置。
- 启动服务,成功登陆seata控制台。
- 查看nacos控制台,服务列表新增seata服务。
七、参考
Seata新手部署指南
原文地址:https://www.cnblogs.com/jeremylai7/p/16832440.html
原文地址:https://www.yht7.com/news/213351
版本 V2.0.0(docker安装)
1. 拉取 Seata Server 镜像
从 Docker Hub 上拉取 Seata Server 的镜像。你可以选择最新的镜像或者指定一个特定版本的镜像。
docker pull seataio/seata-server:2.0.0
2. 创建/usr/local/docker/seata/conf/ 目录, 创建 application.yml文件,添加配置
console:
user:
password: seata@fly
username: seata
logging:
config: classpath:logback-spring.xml
extend:
kafka-appender:
bootstrap-servers: 127.0.0.1:9092
topic: logback_to_logstash
logstash-appender:
destination: 127.0.0.1:4560
file:
path: ${log.home:${user.home}/logs/seata}
seata:
config:
nacos:
group: DEFAULT_GROUP
namespace: ed55622d-4d65-4f9b-8682-7e9de0de9e6f
password: nacos@fly
server-addr: 172.17.0.4:8848
username: nacos
type: nacos
registry:
nacos:
application: fly-seata-server
group: DEFAULT_GROUP
namespace: ed55622d-4d65-4f9b-8682-7e9de0de9e6f
password: nacos@fly
server-addr: 172.17.0.4:8848
username: nacos
type: nacos
security:
ignore:
urls: /,/**/*.css,/**/*.js,/**/*.html,/**/*.map,/**/*.svg,/**/*.png,/**/*.jpeg,/**/*.ico,/api/v1/auth/login,/metadata/v1/**
secretKey: SeataSecretKey0c382ef121d778043159209298fd40bf3850a017
tokenValidityInMilliseconds: 1800000
store:
db:
branch-table: branch_table
datasource: druid
db-type: mysql
distributed-lock-table: distributed_lock
driver-class-name: com.mysql.cj.jdbc.Driver
global-table: global_table
lock-table: lock_table
max-conn: 100
max-wait: 5000
min-conn: 10
password: mysql@fly
query-limit: 1000
url: jdbc:mysql://172.17.0.3:3306/seata?characterEncoding=utf8&useSSL=false&serverTimezone=GMT%2B8&rewriteBatchedStatements=true&allowPublicKeyRetrieval=true
user: root
mode: db
server:
port: 7091
spring:
application:
name: fly-seata-server
3. 运行 Seata Server
使用 docker run 命令启动 Seata Server。这里有一些常用的参数需要配置:
docker run --name seata-server-io -d -p 8091:8091 -e SEATA_IP=127.0.0.1 -p 7091:7091 -v /usr/local/docker/seata/conf/application.yml:/seata-server/resources/application.yml -v /usr/local/docker/seata/logs:/root/logs/seata seataio/seata-server:2.0.0
参数说明:
--name seata-server-io: 指定容器名称。
-d: 后台运行容器。
-p 8891:8091: 将容器的 8091 端口映射到主机的 8891 端口。
-e SEATA_IP=127.0.0.1: 设置 Seata Server 的 IP 地址。服务可达
-v /usr/local/docker/seata/conf:/root/seata-config: 挂载配置文件目录。
-v /usr/local/docker/seata/logs:/root/logs/seata: 挂载日志文件目录。
seataio/seata-server:2.0.0: 使用的 Seata Server 镜像版本。
4. 检查 Seata Server 是否启动成功
使用 docker ps 命令查看正在运行的容器,确认 Seata Server 已经启动。
docker ps
5. 访问 Seata Server
一旦 Seata Server 成功启动,你可以通过访问 http://127.0.0.1:7091/#/login 来查看 Seata 的控制台页面。
用户名:密码seata/seata@fly
注意事项
配置文件:确保 /usr/local/docker/seata/conf 目录下的配置文件正确配置了 Seata Server 的参数,包括注册中心、配置中心等信息。
日志文件:挂载的日志文件目录 /usr/local/docker/seata/logs 会保存 Seata Server 的日志,这对于调试和监控非常重要。
网络配置:确保主机和容器之间的网络配置正确,以便 Seata Server 可以被其他服务发现和访问。
版本兼容性:选择 Seata Server 的版本时,请确保它与你的应用版本兼容。