blog

Springクラウド・ナコス・シータ分散型物事の実際

トランザクションメッセージは最終的に分散トランザクションの最終的な一貫性を保つものであり、seataの方式と全く同じではありません。例えば、aがbにお金を支払う場合、aのお金の差し引きが成功したら、b...

May 18, 2020 · 7 min. read
シェア

準備

seataをダウンロード

nacosをダウンロード

nacos-server-1.1.:github.com/nac...

デモコードのダウンロード

デモ・プロジェクト・コード:github.com/leo20131231...

データベースの構築

seataデータベースgithub.com/leo20131231

サンプルプロジェクト受注データベーススクリプト:github.com/leo20131231...

サンプルプロジェクト保管用データベーススクリプト:github.com/leo20131231...

seataとnacosの開始

nacosを解凍,

nacosを起動します。"http://localhost:8848"/nacos ユーザー名 パスワード nacos/nacos

以下はseataの使い方の例です。

confディレクトリのregistry.confを変更します。

registry {
 # file nacos、eureka、redis、zk、consul、etcdsofaなどがある。
 type = "nacos"
 nacos {
 application = "seata-server"
 serverAddr = ".1:8848"
 group = "DEFAULT_GROUP"
 namespace = ""
 cluster = "default"
 username = "nacos"
 password = "nacos"
 }
 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 = ".1:2181"
 sessionTimeout = 6000
 connectTimeout = 2000
 username = ""
 password = ""
 }
 consul {
 cluster = "default"
 serverAddr = ".1:8500"
 }
 etcd3 {
 cluster = "default"
 serverAddr = "http://localhost:2379"
 }
 sofa {
 serverAddr = ".1:9603"
 application = "default"
 region = "DEFAULT_ZONE"
 datacenter = "DefaultDataCenter"
 cluster = "default"
 group = "SEATA_GROUP"
 addressWaitTime = "3000"
 }
 file {
 name = "file.conf"
 }
}
config {
 # fileナコス、アポロ、ZK、コンサル、etcd3
 type = "nacos"
 nacos {
 serverAddr = ".1:8848"
 namespace = ""
 group = "SEATA_GROUP"
 username = "nacos"
 password = "nacos"
 }
 consul {
 serverAddr = ".1:8500"
 }
 apollo {
 appId = "seata-server"
 apolloMeta = "http://...204:8801"
 namespace = "application"
 }
 zk {
 serverAddr = ".1:2181"
 sessionTimeout = 6000
 connectTimeout = 2000
 username = ""
 password = ""
 }
 etcd3 {
 serverAddr = "http://localhost:2379"
 }
 file {
 name = "file.conf"
 }
}
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=.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://.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=.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/confディレクトリに以下の内容でnacos-config.shを新規作成します。

#!/usr/bin/env bash
# 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://..org/licenses/LICENSE-.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:u:w:" opt
do
 case $opt in
 h)
 host=$OPTARG
 ;;
 p)
 port=$OPTARG
 ;;
 g)
 group=$OPTARG
 ;;
 t)
 tenant=$OPTARG
 ;;
 u)
 username=$OPTARG
 ;;
 w)
 password=$OPTARG
 ;;
 ?)
 echo " USAGE OPTION: $0 [-h host] [-p port] [-g group] [-t tenant] [-u username] [-w password] "
 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
if [[ -z ${username} ]]; then
 username=""
fi
if [[ -z ${password} ]]; then
 password=""
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 "${contentType}" "http://$nacosAddr/nacos/v1/cs/configs?dataId=$1&group=$group&content=$2&tenant=$tenant&username=$username&password=$password" >"${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 $1=$2 successfully "
 else
 echo "Set $1=$2 failure "
 (( failCount++ ))
 fi
}
count=0
for line in $(cat $(dirname "$PWD")/conf/config.txt | sed s/[[:space:]]//g); do
 (( count++ ))
	key=${line%%=*}
 value=${line#*=}
	addConfig "${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

このディレクトリでsh nacos-config.sh 127.0.0.1を実行すると、設定が正常に初期化されていることが確認できます。

seataの起動 NY seata-server.sh

プロジェクト開始

適切なデータベースリンクを修正

リクエスト 両ステージとも正常に送信されました。

リクエスト "http://localhost:9190"/order/placeOrder/rollbac ロールバック成功

いくつか遭遇

问题  no available service 'null' found, please make sure registry config correct

ソースコードを見て、seataのこの設定を読み込んでnacosに戻ることを学ぼう。

解決策:順序と保存項目 seataのサービスグループ分けは、seata初期化設定のサービスグループ分けと同じでなければなりません。

利用可能なサービス「default」が見つからない問題, please make sure registry config correct

ソースコードを読んで、プロジェクトがnacosからseataサービスホストが取得されない、取得されない理由は、グループがDEFAULT_GROUPであるサービスを取得することです知っているseataサービスグループの登録はSETA_GROUPです。

解決策:seataのregistry.confのgroupをDEFAULT_GROUPにしてseataを再起動してください。

nacosとseataのバージョンは最新のものを使用してください。

現在のところ、seataのモードは2PC (two-stage commit)方式に基づくATモードで、基本的な流れは以下の通りです。

また、トランザクションメッセージに基づいて分散されたものがあります トランザクションメッセージは、最終的にseataのスキームと分散トランザクションの究極の整合性は全く同じではありませんが、例えば、aからbのお金は、bの成功後にお金の控除は、お金の成功に追加する必要があり、それが失敗した場合、補償の成功まで。

Read next

CSSでのセンタリング

1.コンテナは、幅と高さ、テキストによって幅と高さを設定されていない次のメソッドのようにHTMLインフラストラクチャを開くには、1つのテキストが中央に配置されている:テキストを開くには、テキストによってコンテナので、テキストが中央に配置されているだろう コンテナは中央に配置されている:絶対位置決めの使用、および使用(-50)

May 18, 2020 · 4 min read