分布式事务 Seata 部署指南

  • Post author:
  • Post category:其他




分布式事务 Seata 部署指南


  • 部署指南

    • 直接部署:Seata 服务端(TC)- 使用DB存储模式

      • 直接下载打包好的

        seata-server-1.1.0.tar.gz

        , TC 是需要单独部署的Server

      • 建表:global_table、branch_table、lock_table

        • 全局事务会话信息由3块内容构成,全局事务–>分支事务–>全局锁,对应表global_table、branch_table、lock_table。
        • SQL语句在源码的script –> server –> db目录下,这里使用

          v1.1.0

          版本
      • 修改配置文件 : 位置在 seata-server-1.1.0–> conf –> file.conf

        ## transaction log store, only used in seata-server
        store {
          ## store mode: file、db, 修改为 db
          mode = "db"
        
          ## 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) etc.
            datasource = "dbcp"
            ## mysql/oracle/h2/oceanbase etc.
            dbType = "mysql"
            driverClassName = "com.mysql.jdbc.Driver"
            url = "jdbc:mysql://127.0.0.1:3306/seata"
            user = "root"
            password = "123456"
            minConn = 1
            maxConn = 10
            globalTable = "global_table"
            branchTable = "branch_table"
            lockTable = "lock_table"
            queryLimit = 100
          }
        }
        
      • 启动服务,多环境配置

        参考

        ,注: 堆内存建议分配2G,堆外内存1G

        # 实际使用  -Xmx512m -Xms512m -Xmn256m  也没发现什么问题
        sh seata-server512.sh -h 127.0.0.1 -p 8091 -m db
        
        -h: 注册到注册中心的ip , 虽然是可选,但建议带上
        -p: Server rpc 监听端口
        -m: 全局事务会话信息存储模式,file、db,优先读取启动参数
        -n: Server node,多个Server时,需区分各自节点,用于生成不同区间的transactionId,以免冲突
        -e: 多环境配置
        
    • 部署 Seata 服务端 – 使用

      Nacos 注册中心

      和配置中心

      • 上面的直接部署使用的是默认的文件注册中心,但是file方式,并不能提供一个注册中心的完整功能,比如健康检查机制,服务的注册与发现等,注册中心还是要使用专业的比较好

      • 安装包下载、建表、启动服务和上面直接部署部分是一样的,这里只说明不同的地方

      • 复制 nacos-config.sh 和 config.txt 文件到 nacos-server 的config 目录,文件位置在源码的 script –> config-center 目录下

      • 修改 config.txt 配置文件,它是 seata-server 的配置文件,主要修改 service.vgroupMapping 和 DB 的连接字符串

        #...
        

      transport.shutdown.wait=3

      #主要修改 service.vgroupMapping 和 连接字符串

      service.vgroupMapping.missyou_tx_group=default

      #在非file 此配置无效,参见:https://seata.io/zh-cn/docs/user/configurations.html

      service.default.grouplist=127.0.0.1:8091

      #…

      store.db.datasource=dbcp

      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=root

      store.db.password=123456

      store.db.minConn=1

      #…

      
      - 执行 nacos-config.sh 脚本,完成配置上传,执行前先登录nacos , 创建 seata 的 namespace
      
      ```bash
      sh nacos-config.sh -h localhost -p 8848 -g SEATA_GROUP -t df8254e6-071b-4299-9e86-1f698896d31b
      
      -h: host, the default value is localhost.
      -p: port, the default value is 8848.
      -g: Configure grouping, the default value is 'SEATA_GROUP'.
      -t: Tenant information, corresponding to the namespace ID field of Nacos, the default value is ''.
      
      • 修改register.conf,准备启动seata-server ,将file 改成 nacos,使用nacos的注册中心和配置中心

        registry {
          # file 、nacos 、eureka、redis、zk、consul、etcd3、sofa
          type = "nacos"
        
          nacos {
            serverAddr = "127.0.0.1"
            namespace = "c9c53f96-bee9-4337-a76e-7400dacf14cb"
            cluster = "default"
          }
          #...
          file {
            name = "file.conf"
          }
        }
        
        config {
          # file、nacos 、apollo、zk、consul、etcd3
          type = "nacos"
        
          nacos {
            serverAddr = "127.0.0.1"
            namespace = "c9c53f96-bee9-4337-a76e-7400dacf14cb"
            group = "DEFAULT_GROUP"
          }
          # ...
          file {
            name = "file.conf"
          }
        

      }

      
      - 启动服务和上面的 直接部署部分 是一样的
      
      
    • 高可用部署 Seata 服务端 – HA Cluster

      • 前提条件:

        • seata-server 使用 nacos 作为注册中心和配置中心
        • 使用DB存储模式, 参见上面
      • 分别启动 seata-server

        sh seata-server.sh -p 8091 -h 192.168.1.11 -m db -n 1
        sh seata-server.sh -p 8091 -h 192.168.1.12 -m db -n 2
        
        # 注意要设置 -n: Server node ,多个Server时,需区分各自节点,用于生成不同区间的transactionId,以免冲突
        
      • 登录nacos , 查看 seata 命名空间下的 服务列表,会发现有两个名称为 serverAddr 的服务

      • 至此 seata-server 集群已经搭建完成,但是 setata rm 和 tm 客户端如何使用这个集群呢?

      • Client端配置:在Spring Boot项目,使用的是 seata-spring-boot-starter 依赖,可以从源码的 script –> client –>spring 目录下 复制 seata 配置,但是这个配置默认是使用 file 类型的注册中心

        • file 类型下, 项目会通过

          seata.service.grouplist.default=127.0.0.1:8091

          配置的内容连接 seata-server , 即使不配置 seata.service.grouplist.default ,仍然会用 127.0.0.1:8091 尝试连接 seata-server

        • 官网说 grouplist

          只有在 file 类型才会被程序读取,其他类型的注册中心不会读取
        • 由于spring-boot本身配置文件语法的要求,这个地方需要将file.conf中的default.grouplist写成grouplist.default, 效果是一样的.
      • 为了让 setata rm 和 tm 客户端连接seata-server集群,需要利用注册中心的注册发现机制,让注册中心找到seata-server集群的IP;

      • 因此:需要添加如下配置,将注册类型file 改成 nacos , 就像 seata-server 端的 register.conf文件中的配置一样

        # 使用 nacos 注册类型,通过nacos 发现 seata-server , registry.type 默认是 file
        seata.registry.type=nacos  
        seata.registry.nacos.server-addr=127.0.0.1:8848
        seata.registry.nacos.cluster=default
        seata.registry.nacos.namespace=9a6af877-1cd4-4450-9eff-7ba6252fca8e
        



版权声明:本文为justfamily原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。