通過查看seata官方文檔浪默,我用docker-compose搭建了一個(gè)seata-server實(shí)例環(huán)境,并且把官方默認(rèn)的mysql5的driver替換成了mysql8的driver缀匕,使用了nacos作為seata-server的注冊(cè)中心和配置中心纳决。下面記錄一下我的操作步驟:
1.首先咱們先把seata的項(xiàng)目通過git給下載到本地;seata github地址
2.修改config.txt配置文件
找到上圖中的兩個(gè)文件乡小,并修改config.txt阔加,使其符合自己當(dāng)前的需求
除以下幾個(gè)參數(shù)需要現(xiàn)在修改,其他參數(shù)可暫時(shí)不改满钟,或后面再修改都行:
# 修改存儲(chǔ)模式為數(shù)據(jù)庫存儲(chǔ)
store.mode=db
# 指定數(shù)據(jù)庫為mysql胜榔,其他數(shù)據(jù)庫類型根據(jù)自身情況修改
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}
注意:大多數(shù)小伙伴只需要修改以上幾個(gè)參數(shù)夭织,其他參數(shù)可暫時(shí)不改
整個(gè)config.txt文件配置有這么多參數(shù):
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命令執(zhí)行nacos-config.sh文件
sh nacos-config.sh -h {nacos服務(wù)的host} -u {nacos用戶名} -w {nacos密碼}
解析一下上述命令中參數(shù)的意思:
[-h host] [-p port] [-g group] [-t tenant] [-u username] [-w password]
按照nacos服務(wù)請(qǐng)看這里:docker搭建nacos server集群
配置上傳成功的截圖:
4.下載好mysql8的引擎
5.編寫registry.conf。這個(gè)文件的目的就是配置seata-server服務(wù)從nacos上讀取第3步上傳上去的配置吠撮,并且把自己注冊(cè)到nacos的服務(wù)列表中去
registry {
# file 摔癣、nacos 、eureka、redis择浊、zk戴卜、consul、etcd3琢岩、sofa
# 指定注冊(cè)中心為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.準(zhǔn)備初始化數(shù)據(jù)庫:
-- 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:
# 分布式事務(wù)服務(wù)
seata-server:
image: seataio/seata-server
ports:
- "8091:8091"
environment:
#宿主機(jī)ip
- SEATA_IP={宿主機(jī)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.啟動(dòng)seata-server服務(wù)
docker-compose up -d seata-server
seata-server注冊(cè)成功截圖: