1. 前言

    1. 关于seata版本的选择和更详细的安装,可以参考 SpringCloud Alibaba之Seata入门及踩坑

    2. 本篇博客是整合nacos,nacos直接下载安装解压运行就可以了。

  2. seata的下载:

    1. 可以去seata官网上面点击下载,然后找到对应版本去github上面下载,但是github上面懂得,速度老慢了。

    2. 也可以去sourceforge-seata上面下载,虽然也慢,起码不会跟github一样偶尔找不到的好。

      vxxx.zip和vxxx.tar.gz是对应系统和版本的源码,也可以下载下来,因为一些文件要从里面拿

      seata-server-x.x.x.zip和tar.gz是对应系统和版本的服务器,拿过来直接用的,必须下,源码随意

    3. 我自己下载下来几个版本(源码只有1.4.0的),可以瞅一眼,这个云也许会快一点吧...

      https://wwa.lanzoui.com/b010hbq8j

      密码:seata

  3. seata-server安装

    1. 解压下载下来seata-server。打开/seata/conf文件夹,其中主要修改的是registry.conffiel.conf,由于我们基于nacos,这里就不用管file.conf了。下面先将registry.conf备份,然后修改为

      registry {
      type = "nacos"
      nacos {
      application = "seata-server"
      serverAddr = "127.0.0.1:8848"
      group = "SEATA_GROUP"
      namespace = ""
      cluster = "default"
      username = "nacos"
      password = "nacos"
      }
      } config {
      type = "nacos"
      nacos {
      serverAddr = "127.0.0.1:8848"
      namespace = ""
      group = "SEATA_GROUP"
      username = "nacos"
      password = "nacos"
      }
      }

      其中的含义:

      • registry:服务器要注册到nacos的位置(命名空间、集群什么的)。
      • config:seata服务器配置,这里需要去nacos上面拿配置,那么nacos上面没有,所以我们需要推上去。
    2. 解压下载下来的源码,打开文件夹/script/config-center下的config.txt,也可以直接复制

      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.defaultGlobalTransactionTimeout=60000
      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

      主要修改其中的:

      service.vgroupMapping.my_test_tx_group=default
      # 其中my_test_tx_group是可以自定义的且之后要与seata客户端中相匹配 store.mode=file
      # 修改为db,下面修改到对应数据库的信息 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
    3. 打开源码中的script/config-center/nacos下的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")/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

      可以直接修改下面这些,也可以执行时候使用参数形式给进去。

      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

      其中tenant为命名空间,也即nacos上面的namespace。

    4. 运行nacos,然后windows下可以安装Git For Windows来运行sh文件将配置推上nacos。

    5. 打开nacos,点击配置管理,和自己填写对应的命名空间(""默认为public),检查第二步中自定数据库和service.vgroupMapping.my_test_tx_group。那么里面的数据库呢?MySQL中没有seata数据库啊。下面来手动新建。

    6. 新建数据库seata(可以自己命名,nacos上面可以手动编辑的),新建三个表,sql代码官方也给出来了,在源码的script/server/db下的mysql.sql,如下

      -- -------------------------------- The script used when storeMode is 'db' --------------------------------
      -- the table to store GlobalSession data
      CREATE TABLE IF NOT EXISTS `global_table`
      (
      `xid` VARCHAR(128) NOT NULL,
      `transaction_id` BIGINT,
      `status` TINYINT NOT NULL,
      `application_id` VARCHAR(32),
      `transaction_service_group` VARCHAR(32),
      `transaction_name` VARCHAR(128),
      `timeout` INT,
      `begin_time` BIGINT,
      `application_data` VARCHAR(2000),
      `gmt_create` DATETIME,
      `gmt_modified` DATETIME,
      PRIMARY KEY (`xid`),
      KEY `idx_gmt_modified_status` (`gmt_modified`, `status`),
      KEY `idx_transaction_id` (`transaction_id`)
      ) ENGINE = InnoDB
      DEFAULT CHARSET = utf8; -- the table to store BranchSession data
      CREATE TABLE IF NOT EXISTS `branch_table`
      (
      `branch_id` BIGINT NOT NULL,
      `xid` VARCHAR(128) NOT NULL,
      `transaction_id` BIGINT,
      `resource_group_id` VARCHAR(32),
      `resource_id` VARCHAR(256),
      `branch_type` VARCHAR(8),
      `status` TINYINT,
      `client_id` VARCHAR(64),
      `application_data` VARCHAR(2000),
      `gmt_create` DATETIME(6),
      `gmt_modified` DATETIME(6),
      PRIMARY KEY (`branch_id`),
      KEY `idx_xid` (`xid`)
      ) ENGINE = InnoDB
      DEFAULT CHARSET = utf8; -- the table to store lock data
      CREATE TABLE IF NOT EXISTS `lock_table`
      (
      `row_key` VARCHAR(128) NOT NULL,
      `xid` VARCHAR(96),
      `transaction_id` BIGINT,
      `branch_id` BIGINT NOT NULL,
      `resource_id` VARCHAR(256),
      `table_name` VARCHAR(32),
      `pk` VARCHAR(36),
      `gmt_create` DATETIME,
      `gmt_modified` DATETIME,
      PRIMARY KEY (`row_key`),
      KEY `idx_branch_id` (`branch_id`)
      ) ENGINE = InnoDB
      DEFAULT CHARSET = utf8;
    7. 运行seata-server/bin/seata-server.bat,查看nacos上面服务管理中是否有对应seata-server服务启动。

    8. 至此,seata-server服务端配置完成。

  4. seata客户端准备与概况

    在seata的提供者有了之后,我们需要来创建seata客户端。这里使用微服务模块的方式来配置。

    注:在AT模式下,各自客户端中操作每个数据库中必须要有一个undo_log,创建脚本在源码script/client/at/db/mysql.sql,如下:

    -- 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';

    由于篇幅过长,简单来描述测试过程就是:

    • 建立两个独立的数据库
    • 在两个提供者中对各自的数据库做插入或者更新操作。
    • 在消费者调用的方法或者类上面注解@GlobalTransactional
    • 在调用完提供者处理数据库的方法后,手动抛出异常。
    • 数据库中完成回滚。

参考:


seata-server 1.3.0整合nacos,使用nacos做注册和配置中心的更多相关文章

  1. Spring Cloud入门-Nacos实现注册和配置中心(Hoxton版本)

    文章目录 摘要 Nacos简介 使用Nacos作为注册中心 安装并运行Nacos 创建应用注册到Nacos 负载均衡功能 使用Nacos作为配置中心 创建nacos-config-client模块 在 ...

  2. 使用Alibaba的Nacos做为SpringCloud的注册和配置中心,并结合Sentinel+Nocos动态进行限流熔断

    最近在学习阿里的Nacos组件以及Sentinel组件,折腾出了一个小demo. Git地址:https://github.com/yangzhilong/nacos-client 有兴趣的小伙伴可以 ...

  3. Spring Cloud实战 | 第十篇 :Spring Cloud + Seata 1.4.1 + Nacos1.4.0 整合实现微服务架构中逃不掉的话题分布式事务

    Seata分布式事务在线体验地址:https://www.youlai.store 本篇完整源码地址:https://github.com/hxrui/youlai-mall 有想加入开源项目开发的童 ...

  4. SpringCloud实战 | 第三篇:SpringCloud整合Nacos实现配置中心

    前言 随着eureka的停止更新,如果同时实现注册中心和配置中心需要SpringCloud Eureka和SpringCloud Config两个组件;配置修改刷新时需要SpringCloud Bus ...

  5. 配置中心Nacos(服务发现)

    服务演变之路 单体应用架构 在刚开始的时候,企业的用户量.数据量规模都⽐较⼩,项⽬所有的功能模块都放在⼀个⼯程中编码.编译.打包并且部署在⼀个Tomcat容器中的架构模式就是单体应用架构,这样的架构既 ...

  6. .net5+nacos+ocelot 配置中心和服务发现实现

    最近一段时间 因公司业务需要,需要使用.net5做一套微服务的接口,使用nacos 做注册中心和配置中心,ocelot做网关. 因为ocelot 支持的是consol和eureka,如果使用nacos ...

  7. spring cloud alibaba - Nacos 作为配置中心基础使用

    1.简要说明 Nacos提供了作为配置中心的功能,只需要在Nacos的控制台页面添加配置,然后在项目中配置相应的"路径"就好. 主要分为几个步骤: 在Nacos控制台添加配置 在项 ...

  8. 5-18 Nacos配置中心 | RestTemplate

    配置中心 什么是配置中心 所谓配置中心:将项目需要的配置信息保存在配置中心,需要读取时直接从配置中心读取,方便配置管理的微服务工具 我们可以将部分yml文件的内容保存在配置中心 一个微服务项目有很多子 ...

  9. 【Nacos】Springboot整合Nacos配置中心(二) 多环境配置

    本篇随笔接上一篇文章:Springboot整合Nacos配置中心(一),主要记录Nacos多环境的配置的方法 Nacos多环境的配置 方法一: 1.在项目中的bootstrap.yaml文件中配置激活 ...

  10. Spring Cloud Alibaba 整合 Nacos 实现服务配置中心

    在之前的文章 <Nacos 本地单机版部署步骤和使用> 中,大家应该了解了 Nacos 是什么?其中 Nacos 提供了动态配置服务功能 一.Nacos 动态配置服务是什么? 官方是这么说 ...

随机推荐

  1. HTTP方法-GET对比POST

    什么是 HTTP ? 超文本传输协议(HTTP)的设计目的是保证客户端与服务器之间的通信. HTTP 的工作方式是客户端与服务器之间的请求-应答协议. web 浏览器可能是客户端,而计算机上的网络应用 ...

  2. 微信小程序slot(二)

    在组件的 wxml 中可以包含 slot 节点,用于承载组件使用者提供的 wxml 结构. 默认情况下,一个组件的 wxml 中只能有一个 slot .需要使用多 slot 时,可以在组件 js 中声 ...

  3. 并发编程 - 线程同步(三)之原子操作Interlocked简介

    上一章我们了解了3种处理多线程中共享资源安全的方法,今天我们将更近一步,学习一种针对简单线程同步场景的解决方案--Interlocked. 在此之前我们先学习一个概念--原子操作. 01.原子操作 原 ...

  4. CPU算力如何计算

    本文分享自天翼云开发者社区<CPU算力如何计算>,作者:l****n 什么是算力 随着国家大力发展数字基础设施,算力的提升和普惠变得越来越重要,它注定会在人们的视线中占据很重要的一席.那么 ...

  5. Linux 文件压缩和解压缩命令

    Linux 文件压缩和解压缩命令 在Linux操作系统中,文件压缩和解压缩是日常管理和维护任务中的重要一环.通过压缩文件,可以显著减少存储空间的使用,并加快网络传输速度.Linux提供了多种压缩和解压 ...

  6. kvm virtio window server2003

    https://www.linux-kvm.org/page/Downloads 这是kvm官网对virtio讲解 http://www.linux-kvm.org/images/d/dd/KvmFo ...

  7. WPF中实现PropertyGrid的三种方式

    原文地址: https://www.cnblogs.com/zhuqil/archive/2010/09/02/Wpf-PropertyGrid-Demo.html 第一种方式:使用WindowsFo ...

  8. C# 钩子函数使用

    1. 什么是钩子 hook(钩子)是windows提供的一种消息处理机制平台,是指在程序正常运行中接受信息之前预先启动的函数,用来检查和修改传给该程序的信息,(钩子)实际上是一个处理消息的程序段,通过 ...

  9. JS实现隐藏手机号码中间4位数

    代码COPY 3. 使用正则 function geTel(tel){ var reg = /^(\d{3})\d{4}(\d{4})$/; return tel.replace(reg, " ...

  10. ES6语法糖,超甜!

    ES6 语法糖 1. ... ... 表示取出可遍历数组中的内容. const arr = new Array() const numbers = [1,2,3,4,5] arr.push(...nu ...