1. 下載:https://github.com/seata/seata/releases
解壓筑累,注意看下里面有個README-zh.md隔崎,相關的一些腳本可以在這個文件的鏈接找到挫剑,因為不同的seata版本,腳本可能有差異车酣,需要注意下。我這里把1.4.0的這個文件內容補充在下面:
腳本說明
client
存放用于客戶端的配置和SQL
- at: AT模式下的
undo_log
建表語句 - conf: 客戶端的配置文件
- saga: SAGA 模式下所需表的建表語句
- spring: SpringBoot 應用支持的配置文件
server
存放server側所需SQL和部署腳本
- db: server 側的保存模式為
db
時所需表的建表語句 - docker-compose: server 側通過 docker-compose 部署的腳本
- helm: server 側通過 Helm 部署的腳本
- kubernetes: server 側通過 Kubernetes 部署的腳本
config-center
用于存放各種配置中心的初始化腳本着逐,執(zhí)行時都會讀取
config.txt
配置文件倘潜,并寫入配置中心
- nacos: 用于向 Nacos 中添加配置
- zk: 用于向 Zookeeper 中添加配置,腳本依賴 Zookeeper 的相關腳本械拍,需要手動下載突勇;ZooKeeper相關的配置可以寫在
zk-params.txt
中,也可以在執(zhí)行的時候輸入 - apollo: 向 Apollo 中添加配置坷虑,Apollo 的地址端口等可以寫在
apollo-params.txt
甲馋,也可以在執(zhí)行的時候輸入 - etcd3: 用于向 Etcd3 中添加配置
- consul: 用于向 consul 中添加配置
我按這個文檔,找了下相關的腳本地址:https://github.com/seata/seata/tree/develop/script/client
這里我把undo_log的腳本貼在下面:
-- for AT mode you must to init this sql for you business database. the seata server not need it.
CREATE TABLE IF NOT EXISTS `undo_log`
(
`branch_id` BIGINT(20) NOT NULL COMMENT 'branch transaction id',
`xid` VARCHAR(100) NOT NULL COMMENT 'global transaction id',
`context` VARCHAR(128) NOT NULL COMMENT 'undo_log context,such as serialization',
`rollback_info` LONGBLOB NOT NULL COMMENT 'rollback info',
`log_status` INT(11) NOT NULL COMMENT '0:normal status,1:defense status',
`log_created` DATETIME(6) NOT NULL COMMENT 'create datetime',
`log_modified` DATETIME(6) NOT NULL COMMENT 'modify datetime',
UNIQUE KEY `ux_undo_log` (`xid`, `branch_id`)
) ENGINE = InnoDB
AUTO_INCREMENT = 1
DEFAULT CHARSET = utf8 COMMENT ='AT transaction mode undo table';
2. 修改registry.conf的注冊中心和配置中心為nacos
registry {
# file 迄损、nacos 定躏、eureka、redis海蔽、zk共屈、consul、etcd3党窜、sofa
type = "nacos"
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 = 6000
connectTimeout = 2000
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 = "nacos"
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 = 6000
connectTimeout = 2000
username = ""
password = ""
}
etcd3 {
serverAddr = "http://localhost:2379"
}
file {
name = "file.conf"
}
}
3. 配置好之后豁护,啟動seata
# nohup sh seata-server.sh -p 8091 -h 127.0.0.1 -m file > seata.log &
-p 指定啟動seata server的端口號哼凯。
-h 指定seata server所綁定的主機。
-m 事務日志楚里、事務執(zhí)行信息存儲的方式断部,目前支持file(文件方式)、db(數據庫方式班缎,建表語句請查看config/db_store.sql蝴光、config/db_undo_log.sql)
這里如果是公網連接seata的話她渴,需要將127.0.0.1換成公網IP。
啟動之后可以在nacos的控制臺上看到注冊的服務:
在這里插入圖片描述
補充幾點:
1. 如果你的seata使用的模式是db模式蔑祟,
在這里插入圖片描述
請注意趁耗,像使用云主機的話,數據庫的連接名類似如下:
jdbc:mysql://rm-8vbqa4stkv73491rkso.mysql.zhangbei.rds.aliyuncs.com:3306/ry-cloud?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=true&serverTimezone=GMT%2B8
否則的話疆虚,啟動會報錯苛败。
啟動方式改為-m db,具體如下:
# nohup sh seata-server.sh -p 8091 -h 106.13.210.114 -m db > seata.log &
當然這樣會報錯径簿,在這之前咱們需要將seata的配置導入到nacos中:
進入seata/conf目錄下罢屈,創(chuàng)建一個nacos-config.sh文件:
# touch nacos-config.sh
將https://github.com/seata/seata/blob/develop/script/config-center/nacos/nacos-config.sh的內容編輯進如nacos-config.sh
然后到seata目錄創(chuàng)建config.txt,注意跟nacos-config.sh不在同一個目錄
# touch config.txt
將如下內容寫入:
service.vgroupMapping.my_test_tx_group=default
service.default.grouplist=127.0.0.1:8091
service.enableDegrade=false
service.disableGlobalTransaction=false
store.mode=file
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
最后再執(zhí)行導入:
[root@instance-0terk04p conf]# ls
file.conf file.conf.example logback.xml META-INF nacos-config.sh README.md README-zh.md registry.conf
[root@instance-0terk04p conf]# sh nacos-config.sh 127.0.0.1
set nacosAddr=localhost:8848
set group=SEATA_GROUP
Set service.vgroupMapping.my_test_tx_group=default successfully
Set service.default.grouplist=127.0.0.1:8091 successfully
Set service.enableDegrade=false successfully
Set service.disableGlobalTransaction=false successfully
Set store.mode=file successfully
Set store.db.datasource=druid successfully
Set store.db.dbType=mysql successfully
Set store.db.driverClassName=com.mysql.cj.jdbc.Driver successfully
Set store.db.url=jdbc:mysql://rm-8vbqa4stkv73491rkso.mysql.zhangbei.rds.aliyuncs.com:3306/ry-cloud?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=true&serverTimezone=GMT%2B8 successfully
Set store.db.user=dongda successfully
Set store.db.password=$Fuxiaoxue123 successfully
Set store.db.minConn=5 successfully
Set store.db.maxConn=30 successfully
Set store.db.globalTable=global_table successfully
Set store.db.branchTable=branch_table successfully
Set store.db.queryLimit=100 successfully
Set store.db.lockTable=lock_table successfully
Set store.db.maxWait=5000 successfully
=========================================================================
Complete initialization parameters, total-count:18 , failure-count:0
=========================================================================
Init nacos config finished, please start seata-server.
[root@instance-0terk04p conf]#
導入之后篇亭,可以在nacos看到配置信息:
圖片.png
然后再把seata重啟下
下一節(jié)我們來介紹分布式事務如何使用seata