springboot dubbo nacos seata 分布式事物实战

一、准备工作

1、下载seata

seata-server-1.3.0 https://github.com/seata/seata/releases

2、下载nacos

nacos-server-1.1.:https://github.com/alibaba/nacos/releases/tag/1.1.3

3、下载demo代码

demo 工程代码:https://github.com/leo20131231/seata_demo/tree/master/springboot-dubbo-seata

4、建数据库

seata的数据库:(https://github.com/leo20131231/seata_demo/tree/master/springcloud-nacos-seata/sql/seata.sql)

示例工程的数据库脚本:(https://github.com/leo20131231/seata_demo/blob/master/springboot-dubbo-seata/sql/db_seata.sql)

二、启动seata和nacos

1、解压nacos,

启动nacos访问:http://localhost:8848/nacos     用户名密码 nacos/nacos

2、解压seata

修改conf目录的registry.conf (注意 nacos 注册服务group要是DEFAULT_GROUP)

registry {
  # file 、nacos 、eureka、redis、zk、consul、etcd3、sofa
  type = "nacos"

  nacos {
    application = "seata-server"
    serverAddr = "127.0.0.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 = "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 = "nacos"
    password = "nacos"
  }
  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/conf 目录新建 config.txt  文件内容如下 注意 mysql数据库链接地址 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.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

在seata/conf 目录新建 nacos-config.sh 内容如下

#!/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: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    可以看到配置初始化成功 如下图

springboot dubbo nacos seata 分布式事物实战_第1张图片

 

 

启动seata sh seata-server.sh

三、启动项目

项目基本流程图

 

springboot dubbo nacos seata 分布式事物实战_第2张图片

 

 

启动account business order stoarge ,修改相应的数据库链接

测试提交 使用postman请求 http://localhost:8104/business/dubbo/buy

{
    "userId":"1",
    "commodityCode":"C201901140001",
    "name":"fan",
    "count":50,
    "amount":"100"
}

 

springboot dubbo nacos seata 分布式事物实战_第3张图片

成功后如下图

 

 

 

 

测试回滚 请求 http://localhost:8104/business/dubbo/buy2

执行前数据库情况如下

 

springboot dubbo nacos seata 分布式事物实战_第4张图片

 

 

 

springboot dubbo nacos seata 分布式事物实战_第5张图片

 

 

 

springboot dubbo nacos seata 分布式事物实战_第6张图片

当代码执行如下行数时,看下此时数据库

 

 

springboot dubbo nacos seata 分布式事物实战_第7张图片

库存已经扣减

springboot dubbo nacos seata 分布式事物实战_第8张图片

账户余额已减

springboot dubbo nacos seata 分布式事物实战_第9张图片

订单已创建

springboot dubbo nacos seata 分布式事物实战_第10张图片

当把代码执行完之后 查看order工程日志 回滚

springboot dubbo nacos seata 分布式事物实战_第11张图片

订单不见了

springboot dubbo nacos seata 分布式事物实战_第12张图片

账户余额恢复了

springboot dubbo nacos seata 分布式事物实战_第13张图片

库存恢复了

springboot dubbo nacos seata 分布式事物实战_第14张图片

 

 

遇到的几个

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

看源码得知回去nacos里面读取 seata的这个配置

 

springboot dubbo nacos seata 分布式事物实战_第15张图片

 

 

解决办法: order和storage项目 这个seata服务分组要和seata初始化配置的服务分组要一致,如下

 

 

 

问题2、no available service 'default' found, please make sure registry config correct

看源码得知是项目 从nacos获取seata服务的时候 hosts 没有获取到, 没有获取到的原因是 获取服务时候用的分组是DEFAULT_GROUP, 而注册seata服务的分组 是SETA_GROUP

 

springboot dubbo nacos seata 分布式事物实战_第16张图片

 

 

 

springboot dubbo nacos seata 分布式事物实战_第17张图片

解决办法: seata 的registry.conf 中group 使用DEFAULT_GROUP 重启seata

 

 

springboot dubbo nacos seata 分布式事物实战_第18张图片

 

 

nacos和seata版本用的尽量最新,不然会有各种问题 我用的版本如下

 

springboot dubbo nacos seata 分布式事物实战_第19张图片

 

 

目前文章使用的seata的模式是AT模式 基于2PC(俩阶段提交) 方式做分布式事物,基本流程如下图

 

springboot dubbo nacos seata 分布式事物实战_第20张图片

 

 

还有的分布式事物方案是基于事务消息 事务消息最终是分布式事务的最终一致性跟seata的方案不太一样,比如a给b打钱,当a的钱扣除成功之后(扣除失败则本次失败),b则必须要加钱成功,如果失败,则要直到补偿成功。

你可能感兴趣的:(springcloud,seata)