当前位置:   article > 正文

springboot + dubbo + nacos + seata 快速集成_spring boot 整合seata saga模式

spring boot 整合seata saga模式

Seata

Seata 是一款开源的分布式事务解决方案,致力于在微服务架构下提供高性能和简单易用的分布式事务服务,下面我们以最快的速度搭建springboot + dubbo + seata 的demo

1.首先下载seatea最新的安装包,路径如下

https://github.com/seata/seata/releases/

2.解压并配置Seata

首先进入conf/文件夹

打开registry.conf文件,

可以看到默认配置的是读取file和注册到file,我们将其都改为nacos,下面展示主要配置位置,大家按照自己的nacos进行具体配置

  1. registry {
  2. # file 、nacos 、eureka、redis、zk、consul、etcd3、sofa
  3. type = "file"
  4. nacos {
  5. application = "seata-server"
  6. serverAddr = "127.0.0.1:8848"
  7. group = "SEATA_GROUP"
  8. namespace = "74fe7825-bbb9-4723-b73e-e9182c903cec"
  9. cluster = "default"
  10. username = "nacos"
  11. password = "nacos"
  12. }
  13. .
  14. .//省略其他配置
  15. .
  16. }
  17. config {
  18. # file、nacos 、apollo、zk、consul、etcd3
  19. type = "nacos"
  20. nacos {
  21. serverAddr = "127.0.0.1:8848"
  22. namespace = "74fe7825-bbb9-4723-b73e-e9182c903cec"
  23. group = "SEATA_GROUP"
  24. username = "nacos"
  25. password = "nacos"
  26. dataId = "seataServer.properties"
  27. }
  28. .
  29. .//省略其他配置
  30. .
  31. }

3.加载默认配置参数到nacos

1)下载配置文件

 地址:https://github.com/seata/seata/blob/develop/script/config-center/config.txt

下载后,放于seata-server-x.x.x\下,

打开文件将没有配置的项删除或赋值,否则会执行失败,下面是我自己执行成功的一份文件

  1. transport.type=TCP
  2. transport.server=NIO
  3. transport.heartbeat=true
  4. transport.enableClientBatchSendRequest=true
  5. transport.threadFactory.bossThreadPrefix=NettyBoss
  6. transport.threadFactory.workerThreadPrefix=NettyServerNIOWorker
  7. transport.threadFactory.serverExecutorThreadPrefix=NettyServerBizHandler
  8. transport.threadFactory.shareBossWorker=false
  9. transport.threadFactory.clientSelectorThreadPrefix=NettyClientSelector
  10. transport.threadFactory.clientSelectorThreadSize=1
  11. transport.threadFactory.clientWorkerThreadPrefix=NettyClientWorkerThread
  12. transport.threadFactory.bossThreadSize=1
  13. transport.threadFactory.workerThreadSize=default
  14. transport.shutdown.wait=3
  15. service.vgroupMapping.my_test_tx_group=default
  16. service.default.grouplist=127.0.0.1:8091
  17. service.enableDegrade=false
  18. service.disableGlobalTransaction=false
  19. client.rm.asyncCommitBufferLimit=10000
  20. client.rm.lock.retryInterval=10
  21. client.rm.lock.retryTimes=30
  22. client.rm.lock.retryPolicyBranchRollbackOnConflict=true
  23. client.rm.reportRetryCount=5
  24. client.rm.tableMetaCheckEnable=false
  25. client.rm.tableMetaCheckerInterval=60000
  26. client.rm.sqlParserType=druid
  27. client.rm.reportSuccessEnable=false
  28. client.rm.sagaBranchRegisterEnable=false
  29. client.rm.tccActionInterceptorOrder=-2147482648
  30. client.tm.commitRetryCount=5
  31. client.tm.rollbackRetryCount=5
  32. client.tm.defaultGlobalTransactionTimeout=60000
  33. client.tm.degradeCheck=false
  34. client.tm.degradeCheckAllowTimes=10
  35. client.tm.degradeCheckPeriod=2000
  36. client.tm.interceptorOrder=-2147482648
  37. store.mode=file
  38. store.lock.mode=file
  39. store.session.mode=file
  40. store.publicKey=seata12345
  41. store.file.dir=file_store/data
  42. store.file.maxBranchSessionSize=16384
  43. store.file.maxGlobalSessionSize=512
  44. store.file.fileWriteBufferCacheSize=16384
  45. store.file.flushDiskMode=async
  46. store.file.sessionReloadReadSize=100
  47. store.db.datasource=druid
  48. store.db.dbType=mysql
  49. store.db.driverClassName=com.mysql.jdbc.Driver
  50. store.db.url=jdbc:mysql://127.0.0.1:3306/test?useUnicode=true&rewriteBatchedStatements=true
  51. store.db.user=root
  52. store.db.password=123456
  53. store.db.minConn=5
  54. store.db.maxConn=30
  55. store.db.globalTable=global_table
  56. store.db.branchTable=branch_table
  57. store.db.queryLimit=100
  58. store.db.lockTable=lock_table
  59. store.db.maxWait=5000
  60. store.redis.mode=single
  61. store.redis.single.host=127.0.0.1
  62. store.redis.single.port=6379
  63. store.redis.maxConn=10
  64. store.redis.minConn=1
  65. store.redis.maxTotal=100
  66. store.redis.database=0
  67. store.redis.password=123456
  68. store.redis.queryLimit=100
  69. server.recovery.committingRetryPeriod=1000
  70. server.recovery.asynCommittingRetryPeriod=1000
  71. server.recovery.rollbackingRetryPeriod=1000
  72. server.recovery.timeoutRetryPeriod=1000
  73. server.maxCommitRetryTimeout=-1
  74. server.maxRollbackRetryTimeout=-1
  75. server.rollbackRetryTimeoutUnlockEnable=false
  76. server.distributedLockExpireTime=10000
  77. client.undo.dataValidation=true
  78. client.undo.logSerialization=jackson
  79. client.undo.onlyCareUpdateColumns=true
  80. server.undo.logSaveDays=7
  81. server.undo.logDeletePeriod=86400000
  82. client.undo.logTable=undo_log
  83. client.undo.compress.enable=true
  84. client.undo.compress.type=zip
  85. client.undo.compress.threshold=64k
  86. log.exceptionRate=100
  87. transport.serialization=seata
  88. transport.compressor=none
  89. metrics.enabled=false
  90. metrics.registryType=compact
  91. metrics.exporterList=prometheus
  92. metrics.exporterPrometheusPort=9898

正确配置其中service.default.grouplist=127.0.0.1:8091配置的是seata-server的地址

2下载执行脚本

地址:https://github.com/seata/seata/blob/develop/script/config-center/nacos/nacos-config.sh

放于放于seata-server-x.x.x\bin下

修改其中的nacos配置为实际值

  1. #!/usr/bin/env bash
  2. # Copyright 1999-2019 Seata.io Group.
  3. #
  4. # Licensed under the Apache License, Version 2.0 (the "License");
  5. # you may not use this file except in compliance with the License.
  6. # You may obtain a copy of the License at、
  7. #
  8. # http://www.apache.org/licenses/LICENSE-2.0
  9. #
  10. # Unless required by applicable law or agreed to in writing, software
  11. # distributed under the License is distributed on an "AS IS" BASIS,
  12. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. # See the License for the specific language governing permissions and
  14. # limitations under the License.
  15. while getopts ":h:p:g:t:u:w:" opt
  16. do
  17. case $opt in
  18. h)
  19. host=$OPTARG
  20. ;;
  21. p)
  22. port=$OPTARG
  23. ;;
  24. g)
  25. group=$OPTARG
  26. ;;
  27. t)
  28. tenant=$OPTARG
  29. ;;
  30. u)
  31. username=$OPTARG
  32. ;;
  33. w)
  34. password=$OPTARG
  35. ;;
  36. ?)
  37. echo " USAGE OPTION: $0 [-h host] [-p port] [-g group] [-t tenant] [-u username] [-w password] "
  38. exit 1
  39. ;;
  40. esac
  41. done
  42. urlencode() {
  43. for ((i=0; i < ${#1}; i++))
  44. do
  45. char="${1:$i:1}"
  46. case $char in
  47. [a-zA-Z0-9.~_-]) printf $char ;;
  48. *) printf '%%%02X' "'$char" ;;
  49. esac
  50. done
  51. }
  52. if [[ -z ${host} ]]; then
  53. host="127.0.0.1"
  54. fi
  55. if [[ -z ${port} ]]; then
  56. port=8848
  57. fi
  58. if [[ -z ${group} ]]; then
  59. group="SEATA_GROUP"
  60. fi
  61. if [[ -z ${tenant} ]]; then
  62. tenant="74fe7825-bbb9-4723-b73e-e9182c903cec"
  63. fi
  64. if [[ -z ${username} ]]; then
  65. username="nacos"
  66. fi
  67. if [[ -z ${password} ]]; then
  68. password="nacos"
  69. fi
  70. nacosAddr=$host:$port
  71. contentType="content-type:application/json;charset=UTF-8"
  72. echo "set nacosAddr=$nacosAddr"
  73. echo "set group=$group"
  74. failCount=0
  75. tempLog=$(mktemp -u)
  76. function addConfig() {
  77. curl -X POST -H "${contentType}" "http://$nacosAddr/nacos/v1/cs/configs?dataId=$(urlencode $1)&group=$group&content=$(urlencode $2)&tenant=$tenant&username=$username&password=$password" >"${tempLog}" 2>/dev/null
  78. if [[ -z $(cat "${tempLog}") ]]; then
  79. echo " Please check the cluster status. "
  80. exit 1
  81. fi
  82. if [[ $(cat "${tempLog}") =~ "true" ]]; then
  83. echo "Set $1=$2 successfully "
  84. else
  85. echo "Set $1=$2 failure "
  86. (( failCount++ ))
  87. fi
  88. }
  89. count=0
  90. for line in $(cat $(dirname "$PWD")/config.txt | sed s/[[:space:]]//g); do
  91. (( count++ ))
  92. key=${line%%=*}
  93. value=${line#*=}
  94. addConfig "${key}" "${value}"
  95. done
  96. echo "========================================================================="
  97. echo " Complete initialization parameters, total-count:$count , failure-count:$failCount "
  98. echo "========================================================================="
  99. if [[ ${failCount} -eq 0 ]]; then
  100. echo " Init nacos config finished, please start seata-server. "
  101. else
  102. echo " init nacos config fail. "
  103. fi

修改后,直接执行,如果是在windows环境下,可以通过Git Bash运行,运行成功如下

4.启动seata-server

直接运行 seata-server-x.x.x\bin\seata-server.bat或seata-server-x.x.x\bin\seata-server.sh即可

5.下载seated的demo案例

下载地址:https://github.com/seata/seata-samples

6.配置springboot-dubbo-seata

分别配置samples-account samples-business samples-order samples-storage下的

application.properties和registry.conf

application.properties主要修改nacos配置

registry.conf可以从seata-server的conf下复制过来

7.下面按照官方的说明文档去运行demo就可以了

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Monodyee/article/detail/165654
推荐阅读
相关标签
  

闽ICP备14008679号