深度理解 Seata 我就不說了,我也是一知半解铣缠,但我會把我理解的部分在以下闡述。Seata官網(wǎng)
搭建 seata-server
在搭建之前鹦付,大家一定要理解 seata 的核心角色以及之間的通信關(guān)系尚粘,Seata分TC、TM和RM三個角色敲长,TC(Server端)為單獨(dú)服務(wù)端部署郎嫁,TM和RM(Client端)由業(yè)務(wù)系統(tǒng)集成。
TC (Transaction Coordinator) - 事務(wù)協(xié)調(diào)者
維護(hù)全局和分支事務(wù)的狀態(tài)祈噪,驅(qū)動全局事務(wù)提交或回滾泽铛。TM (Transaction Manager) - 事務(wù)管理器
定義全局事務(wù)的范圍:開始全局事務(wù)、提交或回滾全局事務(wù)辑鲤。RM (Resource Manager) - 資源管理器
管理分支事務(wù)處理的資源盔腔,與TC交談以注冊分支事務(wù)和報告分支事務(wù)的狀態(tài),并驅(qū)動分支事務(wù)提交或回滾月褥。
正式開始搭建(TC)
- seata-server 下載地址弛随,下載完成后解壓縮。
- 修改 seata/conf/file.conf 文件吓坚,這個文件里是存儲事務(wù)日志的撵幽。我使用的是DB存儲。
## transaction log store, only used in seata-server
store {
## store mode: file礁击、db盐杂、redis
mode = "db"
## file store property
file {
## store location dir
dir = "sessionStore"
# branch session size , if exceeded first try compress lockkey, still exceeded throws exceptions
maxBranchSessionSize = 16384
# globe session size , if exceeded throws exceptions
maxGlobalSessionSize = 512
# file buffer size , if exceeded allocate new buffer
fileWriteBufferCacheSize = 16384
# when recover batch read size
sessionReloadReadSize = 100
# async, sync
flushDiskMode = async
}
## database store property
db {
## the implement of javax.sql.DataSource, such as DruidDataSource(druid)/BasicDataSource(dbcp)/HikariDataSource(hikari) etc.
datasource = "druid"
## mysql/oracle/postgresql/h2/oceanbase etc.
dbType = "mysql"
driverClassName = "com.mysql.cj.jdbc.Driver"
url = "jdbc:mysql://0.0.0.0:3306/seata?useUnicode=true&characterEncoding=UTF-8&autoReconnect=true&useSSL=false&zeroDateTimeBehavior=convertToNull&&serverTimezone=Asia/Shanghai"
user = "dev"
password = "mysql@dev.2020"
minConn = 5
maxConn = 30
globalTable = "global_table"
branchTable = "branch_table"
lockTable = "lock_table"
queryLimit = 100
maxWait = 5000
}
## redis store property
redis {
host = "127.0.0.1"
port = "6379"
password = ""
database = "0"
minConn = 1
maxConn = 10
queryLimit = 100
}
}
- 修改 seata/conf/registry.conf 文件,這個文件是用來修改 seata-server 的注冊方式哆窿,你可以把 seata-server 單獨(dú)當(dāng)成一個服務(wù)链烈,但既然是服務(wù)那么是不是要考慮一個 seata-server 蹦了怎么辦?所以這里提供了集群的注冊方式挚躯。
# 這里是服務(wù)注冊强衡,默認(rèn)是file,也就是單機(jī)码荔,我這里使用了nacos服務(wù)管理
registry {
# file 漩勤、nacos 、eureka缩搅、redis越败、zk、consul硼瓣、etcd3究飞、sofa
type = "nacos"
nacos {
application = "seata-server"
serverAddr = "192.168.200.89:8848"
group = "SEATA_GROUP"
namespace = ""
cluster = "default"
username = "nacos"
password = "nacos@root@2020"
}
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"
}
}
# 配置文件讀取的方式,默認(rèn)也是file(file.conf),讀取的本地文件亿傅,我們使用nacos媒峡,讓他讀取nacos上的配置,所以上面配置的 file.conf里的內(nèi)容基本沒用。
config {
# file葵擎、nacos 谅阿、apollo、zk坪蚁、consul奔穿、etcd3
type = "nacos"
nacos {
serverAddr = "192.168.200.89:8848"
namespace = ""
group = "SEATA_GROUP"
username = "nacos"
password = "nacos@root@2020"
}
consul {
serverAddr = "127.0.0.1:8500"
}
apollo {
appId = "seata-server"
apolloMeta = "http://192.168.1.204:8801"
namespace = "application"
}
zk {
serverAddr = "127.0.0.1:2181"
sessionTimeout = 6000
connectTimeout = 2000
username = ""
password = ""
}
etcd3 {
serverAddr = "http://localhost:2379"
}
file {
name = "file.conf"
}
}
到這里基本的 seata-server 就算是配置好了。 注意我代碼里也有文字說明敏晤。
創(chuàng)建 seata 數(shù)據(jù)庫
TC 全局事務(wù)會話信息由3塊內(nèi)容構(gòu)成贱田,全局事務(wù)-->分支事務(wù)-->全局鎖,對應(yīng)表global_table嘴脾、branch_table男摧、lock_table。
表結(jié)構(gòu)
nacos 配置
對于 nacos 還不會的小伙伴可以看我之前寫的 nacos文章:
- spring cloud alibaba - Nacos 學(xué)習(xí)(discovery)
- spring cloud alibaba - Nacos 學(xué)習(xí)(config)
- spring cloud alibaba - Nacos 學(xué)習(xí)(集群)
nacos 搭建好了之后译打,我們需要把在 seata/conf/registry.conf 里寫的 config內(nèi)容配置到 nacos里耗拓,步驟如下:
- 隨便在一個文件下建 setat-config文件夾,然后在 setat-config/nacos/nacos-config.sh奏司,具體內(nèi)容如下:
#!/usr/bin/env bash
# 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.
while getopts ":h:p:g:t:" opt
do
case $opt in
h)
host=$OPTARG
;;
p)
port=$OPTARG
;;
g)
group=$OPTARG
;;
t)
tenant=$OPTARG
;;
?)
echo " USAGE OPTION: $0 [-h host] [-p port] [-g group] [-t tenant] "
exit 1
;;
esac
done
if [[ -z ${host} ]]; then
host=localhost
fi
if [[ -z ${port} ]]; then
port=8848
fi
if [[ -z ${group} ]]; then
group="SEATA_GROUP"
fi
if [[ -z ${tenant} ]]; then
tenant=""
fi
nacosAddr=$host:$port
contentType="content-type:application/json;charset=UTF-8"
echo "set nacosAddr=$nacosAddr"
echo "set group=$group"
failCount=0
tempLog=$(mktemp -u)
function addConfig() {
curl -X POST -H "${1}" "http://$2/nacos/v1/cs/configs?dataId=$3&group=$group&content=$4&tenant=$tenant" >"${tempLog}" 2>/dev/null
if [[ -z $(cat "${tempLog}") ]]; then
echo " Please check the cluster status. "
exit 1
fi
if [[ $(cat "${tempLog}") =~ "true" ]]; then
echo "Set $3=$4 successfully "
else
echo "Set $3=$4 failure "
(( failCount++ ))
fi
}
count=0
for line in $(cat $(dirname "$PWD")/config.txt | sed s/[[:space:]]//g); do
(( count++ ))
key=${line%%=*}
value=${line#*=}
addConfig "${contentType}" "${nacosAddr}" "${key}" "${value}"
done
echo "========================================================================="
echo " Complete initialization parameters, total-count:$count , failure-count:$failCount "
echo "========================================================================="
if [[ ${failCount} -eq 0 ]]; then
echo " Init nacos config finished, please start seata-server. "
else
echo " init nacos config fail. "
fi
配置內(nèi)容來自:阿里seata config-center
- 創(chuàng)建 setat-config/config.txt,內(nèi)容如下:
service.vgroupMapping.my_test_tx_group=default
store.mode=db
store.db.datasource=druid
store.db.dbType=mysql
store.db.driverClassName=com.mysql.cj.jdbc.Driver
store.db.url=jdbc:mysql://0.0.0.0:3306/seata?useUnicode=true&characterEncoding=UTF-8&autoReconnect=true&useSSL=false&zeroDateTimeBehavior=convertToNull&&serverTimezone=Asia/Shanghai
store.db.user=dev
store.db.password=mysql@dev.2020
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
- 運(yùn)行 nacos-config.sh
sh nacos-config.sh -h 192.168.200.89
-h naocs的地址
會得到如下效果:
接下來只需要 啟動 seata-server 服務(wù)韵洋,然后能在 nacos 服務(wù)列表中看到 seata-server就算搭建完成竿刁。后面我會先搭建一個 多數(shù)據(jù)源的事務(wù)處理。
官方提供:
Seata整合教程:https://www.bilibili.com/video/BV1tz411z7BX/
文字版教程https://mp.weixin.qq.com/s/2KSidJ72YsovpJ94P1aK1g
springcloud整合demo:
https://gitee.com/itCjb/spring-cloud-alibaba-seata-demo
https://github.com/lightClouds917/springcloud-eureka-feign-mybatis-seata-v100