通过查看seata官方文档,我用docker-compose搭建了一个seata-server实例环境,并且把官方默认的mysql5的driver替换成了mysql8的driver,使用了nacos作为seata-server的注册中心和配置中心。下面记录一下我的操作步骤:
1.首先咱们先把seata的项目通过git给下载到本地;seata github地址
2.修改config.txt配置文件
找到上图中的两个文件,并修改config.txt,使其符合自己当前的需求
除以下几个参数需要现在修改,其他参数可暂时不改,或后面再修改都行:
# 修改存储模式为数据库存储
store.mode=db
# 指定数据库为mysql,其他数据库类型根据自身情况修改
store.db.dbType=mysql
# 指定引擎,mysql8以下为com.mysql.jdbc.Driver,mysql8为com.mysql.cj.jdbc.Driver
store.db.driverClassName=com.mysql.cj.jdbc.Driver
store.db.url=jdbc:mysql://{ip}:3306/{db_name}?useUnicode=true
store.db.user={db_user}
store.db.password={db_password}
注意:大多数小伙伴只需要修改以上几个参数,其他参数可暂时不改
整个config.txt文件配置有这么多参数:
transport.type=TCP
transport.server=NIO
transport.heartbeat=true
transport.enableClientBatchSendRequest=false
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
service.vgroupMapping.my_test_tx_group=default
service.default.grouplist=127.0.0.1:8091
service.enableDegrade=false
service.disableGlobalTransaction=false
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=false
client.rm.sqlParserType=druid
client.rm.reportSuccessEnable=false
client.rm.sagaBranchRegisterEnable=false
client.tm.commitRetryCount=5
client.tm.rollbackRetryCount=5
client.tm.degradeCheck=false
client.tm.degradeCheckAllowTimes=10
client.tm.degradeCheckPeriod=2000
store.mode=file
store.file.dir=file_store/data
store.file.maxBranchSessionSize=16384
store.file.maxGlobalSessionSize=512
store.file.fileWriteBufferCacheSize=16384
store.file.flushDiskMode=async
store.file.sessionReloadReadSize=100
store.db.datasource=druid
store.db.dbType=mysql
store.db.driverClassName=com.mysql.jdbc.Driver
store.db.url=jdbc:mysql://127.0.0.1:3306/seata?useUnicode=true
store.db.user=username
store.db.password=password
store.db.minConn=5
store.db.maxConn=30
store.db.globalTable=global_table
store.db.branchTable=branch_table
store.db.queryLimit=100
store.db.lockTable=lock_table
store.db.maxWait=5000
store.redis.host=127.0.0.1
store.redis.port=6379
store.redis.maxConn=10
store.redis.minConn=1
store.redis.database=0
store.redis.password=null
store.redis.queryLimit=100
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
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.log.exceptionRate=100
transport.serialization=seata
transport.compressor=none
metrics.enabled=false
metrics.registryType=compact
metrics.exporterList=prometheus
metrics.exporterPrometheusPort=9898
3.将修改好的config.txt配置添加到nacos配置中心
通过ssh命令执行nacos-config.sh文件
sh nacos-config.sh -h {nacos服务的host} -u {nacos用户名} -w {nacos密码}
解析一下上述命令中参数的意思:
[-h host] [-p port] [-g group] [-t tenant] [-u username] [-w password]
按照nacos服务请看这里:docker搭建nacos server集群
配置上传成功的截图:
4.下载好mysql8的引擎
5.编写registry.conf。这个文件的目的就是配置seata-server服务从nacos上读取第3步上传上去的配置,并且把自己注册到nacos的服务列表中去
registry {
# file 、nacos 、eureka、redis、zk、consul、etcd3、sofa
# 指定注册中心为nacos
type = "nacos"
nacos {
serverAddr = "{host}:{port}"
namespace = ""
cluster = "default"
username="{username}"
password="{password}"
}
}
config {
# file、nacos 、apollo、zk、consul、etcd3
type = "nacos"
nacos {
serverAddr = "{host}:{port}"
namespace = ""
username="{username}"
password="{password}"
}
}
6.准备初始化数据库:
-- the table to store GlobalSession data
drop table if exists `global_table`;
create table `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`)
);
-- the table to store BranchSession data
drop table if exists `branch_table`;
create table `branch_table` (
`branch_id` bigint not null,
`xid` varchar(128) not null,
`transaction_id` bigint ,
`resource_group_id` varchar(32),
`resource_id` varchar(256) ,
`lock_key` varchar(128) ,
`branch_type` varchar(8) ,
`status` tinyint,
`client_id` varchar(64),
`application_data` varchar(2000),
`gmt_create` datetime,
`gmt_modified` datetime,
primary key (`branch_id`),
key `idx_xid` (`xid`)
);
-- the table to store lock data
drop table if exists `lock_table`;
create table `lock_table` (
`row_key` varchar(128) not null,
`xid` varchar(96),
`transaction_id` long ,
`branch_id` long,
`resource_id` varchar(256) ,
`table_name` varchar(32) ,
`pk` varchar(36) ,
`gmt_create` datetime ,
`gmt_modified` datetime,
primary key(`row_key`)
);
7.编写docker-compose.yaml文件
version: "3.2"
services:
# 分布式事务服务
seata-server:
image: seataio/seata-server
ports:
- "8091:8091"
environment:
#宿主机ip
- SEATA_IP={宿主机host}
- SEATA_PORT=8091
- STORE_MODE=db
volumes:
# 第5步编写的registry.conf
- "./seata_server/resources/registry.conf:/seata-server/resources/registry.conf"
# 第4步下载的mysql8引擎jar包
- "./seata_server/libs/mysql-connector-java-8.0.21.jar:/seata-server/libs/mysql-connector-java-8.0.21.jar"
# 日志文件夹
- "./seata_server/logs:/root/logs/seata"
8.启动seata-server服务
docker-compose up -d seata-server
seata-server注册成功截图: