- 前言
Seata 是一款开源的分布式事务解决方案,
- 下载对应版本的文件
-- zookeeper:3.6.1 ( https://mirrors.tuna.tsinghua.edu.cn/apache/zookeeper/ )
-- seata 1.4.0 ( https://github.com/seata/seata )
seata 可以直接下载已打包好的tar包,也可以直接下载源码本地打包启动。
- 修改配置文件
编辑 cong/registry.conf ,将注册和配置中心都改为 zk, 并配置上本地zk的地址
registry {
# file 、nacos 、eureka、redis、zk、consul、etcd3、sofa
type = "zk"
loadBalance = "RandomLoadBalance"
loadBalanceVirtualNodes = 10
nacos {
application = "seata-server"
serverAddr = "127.0.0.1:8848"
group = "SEATA_GROUP"
namespace = ""
cluster = "default"
username = ""
password = ""
}
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 = 60000
connectTimeout = 20000
username = ""
password = ""
}
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 = "zk"
nacos {
serverAddr = "127.0.0.1:8848"
namespace = ""
group = "SEATA_GROUP"
username = ""
password = ""
}
consul {
serverAddr = "127.0.0.1:8500"
}
apollo {
appId = "seata-server"
apolloMeta = "http://192.168.1.204:8801"
namespace = "application"
apolloAccesskeySecret = ""
}
zk {
serverAddr = "127.0.0.1:2181"
sessionTimeout = 60000
connectTimeout = 20000
username = ""
password = ""
}
etcd3 {
serverAddr = "http://localhost:2379"
}
file {
name = "file.conf"
}
}
因为我们使用的是zk做配置和注册中心,所以不许配置 file.conf。
然后下载config.txt配置文件,如果是下载的源码,可以在script\config-center\config.txt 找到,编辑此配置文件,主要是修改db相关的url、username、password参数
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.defaultGlobalTransactionTimeout=60000
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=root
store.db.password=123456
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
- 导入seata配置到zk
这是到了关键的一步,将以上配置导入到zk的配置中心,我这里使用的最笨的方法,手动导入到ZK......
先启动zk,在zk的bin下双击zkServer.cmd脚本启动zk
在bin目录,按住 Shift 然后 右击, 在弹出的窗口选择 在此处打开 PowerShell ,
在PowerShell窗口中 输入 .\zkCli.cmd -server
出现如上页面,表示zk客户端访问成功。
输入 create /seata 创建seata节点
通过 ls / 查看 已存在的节点
接下,依次创建配置文件中的每一个子节点,80多个比较累~
比如,第一个 transport.type=TCP ,转换为zk命令则是 create /seata/transport.type TCP
附上完整的命令,如果有会写cmd脚本的大佬,可以忽略这一步~
create /seata/transport.type TCP
create /seata/transport.server NIO
create /seata/transport.heartbeat true
create /seata/transport.enableClientBatchSendRequest false
create /seata/transport.threadFactory.bossThreadPrefix NettyBoss
create /seata/transport.threadFactory.workerThreadPrefix NettyServerNIOWorker
create /seata/transport.threadFactory.serverExecutorThreadPrefix NettyServerBizHandler
create /seata/transport.threadFactory.shareBossWorker false
create /seata/transport.threadFactory.clientSelectorThreadPrefix NettyClientSelector
create /seata/transport.threadFactory.clientSelectorThreadSize 1
create /seata/transport.threadFactory.clientWorkerThreadPrefix NettyClientWorkerThread
create /seata/transport.threadFactory.bossThreadSize 1
create /seata/transport.threadFactory.workerThreadSize default
create /seata/transport.shutdown.wait 3
create /seata/service.vgroupMapping.my_test_tx_group default
create /seata/service.default.grouplist 127.0.0.1:8091
create /seata/service.enableDegrade false
create /seata/service.disableGlobalTransaction false
create /seata/client.rm.asyncCommitBufferLimit 10000
create /seata/client.rm.lock.retryInterval 10
create /seata/client.rm.lock.retryTimes 30
create /seata/client.rm.lock.retryPolicyBranchRollbackOnConflict true
create /seata/client.rm.reportRetryCount 5
create /seata/client.rm.tableMetaCheckEnable false
create /seata/client.rm.sqlParserType druid
create /seata/client.rm.reportSuccessEnable false
create /seata/client.rm.sagaBranchRegisterEnable false
create /seata/client.tm.commitRetryCount 5
create /seata/client.tm.rollbackRetryCount 5
create /seata/client.tm.defaultGlobalTransactionTimeout 60000
create /seata/client.tm.degradeCheck false
create /seata/client.tm.degradeCheckAllowTimes 10
create /seata/client.tm.degradeCheckPeriod 2000
create /seata/store.mode file
create /seata/store.file.dir file_store/data
create /seata/store.file.maxBranchSessionSize 16384
create /seata/store.file.maxGlobalSessionSize 512
create /seata/store.file.fileWriteBufferCacheSize 16384
create /seata/store.file.flushDiskMode async
create /seata/store.file.sessionReloadReadSize 100
create /seata/store.db.datasource druid
create /seata/store.db.dbType mysql
create /seata/store.db.driverClassName com.mysql.jdbc.Driver
create /seata/store.db.url jdbc:mysql://127.0.0.1:3306/seata?useUnicode=true
create /seata/store.db.user root
create /seata/store.db.password 123456
create /seata/store.db.minConn 5
create /seata/store.db.maxConn 30
create /seata/store.db.globalTable global_table
create /seata/store.db.branchTable branch_table
create /seata/store.db.queryLimit 100
create /seata/store.db.lockTable lock_table
create /seata/store.db.maxWait 5000
create /seata/store.redis.host 127.0.0.1
create /seata/store.redis.port 6379
create /seata/store.redis.maxConn 10
create /seata/store.redis.minConn 1
create /seata/store.redis.database 0
create /seata/store.redis.password null
create /seata/store.redis.queryLimit 100
create /seata/server.recovery.committingRetryPeriod 1000
create /seata/server.recovery.asynCommittingRetryPeriod 1000
create /seata/server.recovery.rollbackingRetryPeriod 1000
create /seata/server.recovery.timeoutRetryPeriod 1000
create /seata/server.maxCommitRetryTimeout -1
create /seata/server.maxRollbackRetryTimeout -1
create /seata/server.rollbackRetryTimeoutUnlockEnable false
create /seata/client.undo.dataValidation true
create /seata/client.undo.logSerialization jackson
create /seata/client.undo.onlyCareUpdateColumns true
create /seata/server.undo.logSaveDays 7
create /seata/server.undo.logDeletePeriod 86400000
create /seata/client.undo.logTable undo_log
create /seata/client.log.exceptionRate 100
create /seata/transport.serialization seata
create /seata/transport.compressor none
create /seata/metrics.enabled false
create /seata/metrics.registryType compact
create /seata/metrics.exporterList prometheus
create /seata/metrics.exporterPrometheusPort 9898
执行完以上命令后,输入 ls /seata 查看zk中seata所有配置节点。
操作到这里,所有需要配置的地方都配置好了,接下来就是启动了。
- 启动seata
在seata/bin目录下,按住 shift + 右键 ,选择在此处打开 powerShell。
输入命令 .\seata-server.bat -h 127.0.0.1 -p 8091 -m db 启动本地的seata
出现如下信息表示seata 启动成功
- 启动应用
在zk和seata启动成功的情况下,直接启动引入seata的项目应用
可以查看seata控制台打印的 RM 和 TM 已经注册到 seata
项目中的启动日志中也会打印响应的日志信息
项目应用如何配置seata,请参考我的其他博文。