seata file模式引入springboot项目步骤(不使用注册中心)

seata的官方文档如下

http://seata.io/zh-cn/docs/overview/what-is-seata.html

seata-server的启动和配置

  1. 从官方下载seata-server安装包https://github.com/seata/seata/releases,下载完成后解压该安装包

    tar -xvf seata-server-1.4.2.tar.gz
  2. 在配置文件中修改,设置部署模式为file模式

    cd seata-server-1.4.2/conf
    vim file.conf

    配置文件如下

    ## transaction log store, only used in seata-server
    store {
    ## store mode: file、db、redis
    mode = "file"
    ## rsa decryption public key
    ## transaction log store, only used in seata-server
    store {
    ## store mode: file、db、redis
    mode = "file"
    ## rsa decryption public key
    publicKey = ""
    ## file store property
    file {
      ## store location dir
      dir = "sessionStore"
      # branch session size , if exceeded first try compress lockkey, still exceeded throws exceptions
      maxBranchSessionSize = 16384
      # globe session size , if exceeded throws exceptions
      maxGlobalSessionSize = 512
      # file buffer size , if exceeded allocate new buffer
      fileWriteBufferCacheSize = 16384
      # when recover batch read size
      sessionReloadReadSize = 100
      # async, sync
      flushDiskMode = async
    }

    ## database store property
    db {
      ## the implement of javax.sql.DataSource, such as DruidDataSource(druid)/BasicDataSource(dbcp)/HikariDataSource(hikari) etc.
      datasource = "druid"
      ## mysql/oracle/postgresql/h2/oceanbase etc.
      dbType = "mysql"
      driverClassName = "com.mysql.jdbc.Driver"
      ## if using mysql to store the data, recommend add rewriteBatchedStatements=true in jdbc connection param
      url = "jdbc:mysql://127.0.0.1:3306/seata?rewriteBatchedStatements=true"
      user = "mysql"
      password = "mysql"
      minConn = 5
      maxConn = 100
      globalTable = "global_table"
      branchTable = "branch_table"
      lockTable = "lock_table"
      queryLimit = 100
      maxWait = 5000
    }

    ## redis store property
    redis {
      ## redis mode: single、sentinel
      mode = "single"
      ## single mode property
      single {
        host = "127.0.0.1"
        port = "6379"
      }
      ## sentinel mode property
      sentinel {
        masterName = ""
        ## such as "10.28.235.65:26379,10.28.235.65:26380,10.28.235.65:26381"
        sentinelHosts = ""
      }
      password = ""
      database = "0"
      minConn = 1
      maxConn = 10
      maxTotal = 100
      queryLimit = 100
    }
    }

    单机版将store.mode改为file即可,这里的db为高可用配置,需要使用mysql数据库,这里不做说明

  3. 启动服务

    cd bin
    nohup sh seata-server.sh &

    这里由于我服务器内存不够,于是删除了官方启动脚本中对java虚拟机的参数设置

    启动参数如下:

    Usage: sh seata-server.sh(for linux and mac) or cmd seata-server.bat(for windows) [options]
    Options:
      --host, -h
        The host to bind.
        Default: 0.0.0.0
      --port, -p
        The port to listen.
        Default: 8091
      --storeMode, -m
        log store mode : file、db
        Default: file
      --help

    e.g.

    sh seata-server.sh -p 8091 -h 127.0.0.1 -m file

     

在springboot项目中引入seata

  1. 在所有需要使用seata的项目的数据库中引入undolog日志表,建表语句如下:

    CREATE TABLE `undo_log` (
    `id` bigint NOT NULL AUTO_INCREMENT,
    `branch_id` bigint NOT NULL,
    `xid` varchar(100) NOT NULL,
    `context` varchar(128) NOT NULL,
    `rollback_info` longblob NOT NULL,
    `log_status` int NOT NULL,
    `log_created` datetime NOT NULL,
    `log_modified` datetime NOT NULL,
    `ext` varchar(100) DEFAULT NULL,
    PRIMARY KEY (`id`),
    UNIQUE KEY `ux_undo_log` (`xid`,`branch_id`)
    ) ENGINE=InnoDB AUTO_INCREMENT=6 DEFAULT CHARSET=utf8mb3
  2. 为项目引入seata的starter

    不知道为什么直接引入seata-spring-boot-starter会发生包冲突,因此这里另外引入了seata-all来配合starter

        <dependency>
          <groupId>io.seata</groupId>
          <artifactId>seata-spring-boot-starter</artifactId>
          <version>1.4.0</version>
          <exclusions>
              <exclusion>
                  <artifactId>seata-all</artifactId>
                  <groupId>io.seata</groupId>
              </exclusion>
          </exclusions>
      </dependency>
      <dependency>
          <groupId>io.seata</groupId>
          <artifactId>seata-all</artifactId>
          <version>1.4.0</version>
      </dependency>
  1. yml配置文件

#====================================Seata Config===============================================
seata:
  enabled: true
  client:
      rm-report-success-enable: true
      rm-table-meta-check-enable: false # 自动刷新缓存中的表结构(默认false)
      rm-report-retry-count: 5 # 一阶段结果上报TC重试次数(默认5)
      rm-async-commit-buffer-limit: 10000 # 异步提交缓存队列长度(默认10000)
      rm:
          lock:
              lock-retry-internal: 10 # 校验或占用全局锁重试间隔(默认10ms)
              lock-retry-times:   30 # 校验或占用全局锁重试次数(默认30)
              lock-retry-policy-branch-rollback-on-conflict: true # 分支事务与其它全局回滚事务冲突时锁策略(优先释放本地锁让回滚成功)
      tm-commit-retry-count:   3 # 一阶段全局提交结果上报TC重试次数(默认1次,建议大于1)
      tm-rollback-retry-count: 3 # 一阶段全局回滚结果上报TC重试次数(默认1次,建议大于1)
      undo:
          undo-data-validation: true # 二阶段回滚镜像校验(默认true开启)
          undo-log-serialization: jackson # undo序列化方式(默认jackson)
          undo-log-table: undo_log # 自定义undo表名(默认undo_log)
      support:
          spring:
              datasource-autoproxy: true
  service:
      vgroup-mapping:
          my_test_tx_group: default # TC 集群(必须与seata-server保持一致)
      enable-degrade: false # 降级开关
      disable-global-transaction: false # 禁用全局事务(默认false)
      grouplist:
          default: 1.15.113.171:8091
  transport:
      shutdown:
          wait: 3
      thread-factory:
          boss-thread-prefix: NettyBoss
          worker-thread-prefix: NettyServerNIOWorker
          server-executor-thread-prefix: NettyServerBizHandler
          share-boss-worker: false
          client-selector-thread-prefix: NettyClientSelector
          client-selector-thread-size: 1
          client-worker-thread-prefix: NettyClientWorkerThread
      type: TCP
      server: NIO
      heartbeat: true
      serialization: seata
      compressor: none
      enable-client-batch-send-request: true # 客户端事务消息请求是否批量合并发送(默认true)
  registry:
      file:
          name: file.conf
      type: file
  config:
      file:
          name: file.conf
      type: file
  log:
      exception-rate: 100

这里需要重点关注的配置如下

  • seata.service.vgourpmapping

    随便取个名字,同一个业务中该名称保持相同

  • seata.service.grouplist

    这里用来配置连接的seata-server ip与地址,spring.cloud.alibaba.seata.tx_service_group设定该group

posted @ 2021-08-05 11:26  秋雨清笛  阅读(1892)  评论(0编辑  收藏  举报