springcloudalibaba之seata分布式事务

seata分布式事务的搭建网上都是星星散散的,总是有问题。今天出一章搭建案例 本篇基于 springcloud+nacos+seate1.2+mysql 搭建

1.首先下载seate1.2版本的代码和seate-serverhttp://seata.io/zh-cn/blog/download.html尽量和本文指导的版本一致,因为每个版本都会有各自的差异

2.将下载完的seate-1.2.zip解压通过idea打开,.主要看script中的配置在这里插入图片描述
在这里插入图片描述

3.解压seate-server,并进入conf目录,修改file.conf和registry.conf这两个文件

  • registry.conf:指定注册中心。我这里使用的是nacos。在registry和config中都需要配置:如图:(我这里是删除了多无用的配置的)
registry {
  # file 、nacos 、eureka、redis、zk、consul、etcd3、sofa
  type = "nacos"

  nacos {
    application = "seata-server"
    serverAddr = "http://127.0.0.1:8848"
    namespace = ""
    cluster = "default"
    username = "nacos"
    password = "nacos"
  }
 
  file {
    name = "file.conf"
  }
}
config {
  # file、nacos 、apollo、zk、consul、etcd3
  type = "nacos"

  nacos {
    serverAddr = "http://127.0.0.1:8848"
    namespace = "c3a1098e-196f-462e-99e1-6697708a0ff3"
    group = "SEATA_GROUP"
    username = "nacos"
    password = "nacos"
  }
 
  file {
    name = "file.conf"
  }
}

注意namespace = “c3a1098e-196f-462e-99e1-6697708a0ff3“这个配置,这个配置指的是nacos中配置seate客服端的参数,没有配置可以不写,如果写了跟下面的配置就需要对应,对应的我用斜体表示

4.修改file.conf文件

service {
  vgroupMapping.my_test_tx_group = "default"
  default.grouplist = "127.0.0.1:8091"
  #degrade, current not support
  enableDegrade = false
  #disable seata
  disableGlobalTransaction = false
}
store {
  mode = "db"
  db {
    datasource = "druid"
    dbType = "mysql"
    driverClassName = "com.mysql.jdbc.Driver"
    url = "jdbc:mysql://192.168.1.1:3306/seata"
    user = "root"
    password = "123123"
    minConn = 5
    maxConn = 30
    globalTable = "global_table"
    branchTable = "branch_table"
    lockTable = "lock_table"
    queryLimit = 100
    maxWait = 5000
  }
}


client {
  rm {
    asyncCommitBufferLimit = 10000
    lock {
      retryInterval = 10
      retryTimes = 30
      retryPolicyBranchRollbackOnConflict = true
    }
    reportRetryCount = 5
    tableMetaCheckEnable = false
    reportSuccessEnable = false
    sagaBranchRegisterEnable = false
  }
  tm {
    commitRetryCount = 5
    rollbackRetryCount = 5
  }
  undo {
    dataValidation = true
    logSerialization = "jackson"
    logTable = "undo_log"
  }
  log {
    exceptionRate = 100
  }
}

这里只要注意一下几点:

  1. vgroupMapping.my_test_tx_group = “default”中 my_test_tx_group 会和后面的参数对应我会加粗显示,vgroupMapping不要写成了vgroup_mapping
  2. 数据库连接,数据源就先使用seata
  3. global_table,branch_table,lock_table这三个为表名,待会会创建三张表存储于seata数据源中,用于seata记录

5.执行sql

  • 先在mysql中添加数据源seata,添加完以后进入script/server/db/mysql.sql,这里有三张表,在数据库中执行sql,这里的三张表和前面server配置中是对应的
  • 执行完以后在进入script/client/at/db/mysql.sql,这里面有一张表undo_log,那个数据源用到了分布式事务就在那个数据源中添加该表。

完成到这里服务端就设置完了。可以启动bin/seata-server.bat试试

6:增加配置参数到nacos中
进入idea,将刚刚下载的seata-1.2.0打开,进入script\config-center\config.txt文件中,这里的配置很多,我把重点内容贴出来

service.vgroupMapping.my_test_tx_group=default
service.default.grouplist=127.0.0.1:8091
store.mode=db
store.db.datasource=druid
store.db.dbType=mysql
store.db.driverClassName=com.mysql.jdbc.Driver
store.db.url=jdbc:mysql://192.168.1.1:3306/seata?useUnicode=true
store.db.user=root
store.db.password=123123
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

主要是修改mysql连接地址和密码。还有前面说的my_test_tx_group,这个必须和server配置的一样
修改完成以后打开script\config-center\nacos\nacos-config.sh,主要修改以下几个内容
在这里插入图片描述
注意看,前面如果设置了namespase就需要在这里加上,前面没有填则可以忽略

7.修改完成。先启动nacos,然后运行script\config-center\nacos\nacos-config.sh(打开文件夹双击就行啦),我这里是下载了git,所以可以直接执行,执行完以后进入nacos中的配置列表就能看到啦
在这里插入图片描述

8.添加spring配置(那些服务用到了分布式事务就在哪些服务添加配置):
可以添加到application.yml也可以添加到nacos中。我是直接添加在nacos中

seata:
  enabled: true
  application-id: ${spring.application.name}
  tx-service-group: my_test_tx_group
  enable-auto-data-source-proxy: true
  use-jdk-proxy: false
  excludes-for-auto-proxying: firstClassNameForExclude,secondClassNameForExclude
  client:
    rm:
      async-commit-buffer-limit: 1000
      report-retry-count: 5
      table-meta-check-enable: false
      report-success-enable: false
      saga-branch-register-enable: false
      lock:
        retry-interval: 10
        retry-times: 30
        retry-policy-branch-rollback-on-conflict: true
    tm:
      commit-retry-count: 5
      rollback-retry-count: 5
    undo:
      data-validation: true
      log-serialization: jackson
      log-table: undo_log
    log:
      exceptionRate: 100
  service:
    vgroup-mapping:
      my_test_tx_group: default
    grouplist:
      default: 127.0.0.1:8091
    enable-degrade: false
    disable-global-transaction: false
  config:
    type: nacos
    nacos:
      serverAddr: http://localhost:8848
      group: SEATA_GROUP
      namespace: c3a1098e-196f-462e-99e1-6697708a0ff3
      userName: nacos
      password: nacos
  registry:
    type: nacos
    nacos:
      application: seata-server
      serverAddr: http://localhost:8848
      namespace:
      cluster: default
      userName: nacos
      password: nacos
  • 还是需要注意前面说的namespase:c3a1098e-196f-462e-99e1-6697708a0ff3,没有可以不加
  • my_test_tx_group和前面的要对应

9.添加maven依赖

  <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-seata</artifactId>
            <version>2.2.2.RELEASE</version>
            <exclusions>
                <exclusion>
                    <groupId>io.seata</groupId>
                    <artifactId>seata-spring-boot-starter</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>io.seata</groupId>
            <artifactId>seata-spring-boot-starter</artifactId>
            <version>1.2.0</version>
        </dependency>

这里要注意版本,我用的是1.2的版本引入就用1.2.0的jar包,版本不对上就有问题的!

10:最后一步:添加spring配置,还是刚刚说的,那个用到了分布式事务就在那个里面加。在bootstrap.yml中添加

spring:
  cloud:
    alibaba:
      seata:
        tx-service-group: my_test_tx_group

注意:这里的my_test_tx_group和前面的对应

在需要使用分布式事务的地方加上@GlobalTransactional即可

我说要注意的地方都是我踩过的坑。。。。。
好,到此结束!


版权声明:本文为qq_38496991原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
原文链接:https://blog.csdn.net/qq_38496991/article/details/118518692