• 【跟乐乐学seata分布式事务组件】springCloudAlibaba分布式组件Seata 1.3.0集成教程


    项目环境

    项目基于若依微服务框架,具体组件版本如下
    spring-boot.version:
    2.3.4.RELEASE

    spring-cloud.version:
    Hoxton.SR8

    spring-cloud-alibaba.version:
    2.2.3.RELEASE

    spring-cloud-starter-alibaba-nacos-discovery.version:
    2.2.3.RELEASE

    spring-cloud-starter-alibaba-nacos-config.version:
    2.2.3.RELEASE

    seata组件本身调度所使用的mysql数据库版本为5.7.37
    此处并非指项目业务所使用的数据库。

    必要资源

    seata官网组件下载页面
    https://seata.io/zh-cn/blog/download.html

    需下载文件:
    seata-server1.3.0服务端

    seata-server1.3.0服务端源码

    开始

    一.配置seata业务调度数据库

    创建一个数据库,用于seata组件的业务调度。
    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;
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57

    另外,需要再额外创建一张‘undo_log’表。

    -- 注意此处0.3.0+ 增加唯一索引 ux_undo_log
    CREATE TABLE `undo_log` (
      `id` bigint(20) NOT NULL AUTO_INCREMENT,
      `branch_id` bigint(20) NOT NULL,
      `xid` varchar(100) NOT NULL,
      `context` varchar(128) NOT NULL,
      `rollback_info` longblob NOT NULL,
      `log_status` int(11) 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=1 DEFAULT CHARSET=utf8;
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14

    最后是这样子
    在这里插入图片描述

    二.配置file.conf

    打开服务端目录下的\conf\file.conf
    参照以下内容进行编辑

    
    ## transaction log store, only used in seata-server
    service {
      #transaction service group mapping
      #此处为配置事务组信息,“my_test_tx_group”和“default”为自定义,用于定义事务组名称;可以随便取。也可以默认不改。
      vgroup_mapping.my_test_tx_group = "default"
      #only support when registry.type=file, please don't set multiple addresses
      # 此处为你的seate服务端地址
      default.grouplist = "127.0.0.1:8091"
      #disable seata
      disableGlobalTransaction = false
    }
    
    store {
      ## store mode: file、db、redis
      # 此处对于seata的业务调度信息存储模式,我们指定修改为'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
      # 此处配置你的seata业务调度数据库信息。
      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"
        # 如果数据库版本为8.0以上,则修改为com.mysql.cj.jdbc.Driver
        driverClassName = "com.mysql.jdbc.Driver"
        url = "jdbc:mysql://127.0.0.1:3306/alibaba_seata"
        user = "root"
        password = "root"
        minConn = 5
        maxConn = 30
        globalTable = "global_table"
        branchTable = "branch_table"
        lockTable = "lock_table"
        queryLimit = 100
        maxWait = 5000
      }
    
      ## redis store property
      redis {
        host = "127.0.0.1"
        port = "6379"
        password = ""
        database = "0"
        minConn = 1
        maxConn = 10
        queryLimit = 100
      }
    
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68

    三.创建nacos命名空间

    需要创建一个nacos命名空间,用于读取和开放seata的业务配置信息。

    f1b0fa23.png)

    在这里插入图片描述
    在这里插入图片描述

    创建后可看到一传MD5,此为命名空间名(namespace)
    在这里插入图片描述

    四.配置registry.conf

    接下来配置同目录下的registry.conf文件
    参照以下内容进行编辑。

    registry {
      # file 、nacos 、eureka、redis、zk、consul、etcd3、sofa
      # 此处修改为nacos,意思是使用nacos作为seata的业务调度注册中心。
      type = "nacos"
    
      # 此处配置nacos信息
      nacos {
      	# 指定seata服务端注册在nacos的服务名称
        application = "seata-server"
    	# nacos注册中心地址
        serverAddr = "192.168.0.5:8848"
        # 分组名称
        group = "SEATA_GROUP"
        # 命名空间
        namespace = "fc0539dd-e76f-40ac-8f00-3b189d3e5075"
        # 此处默认为"default"即可
        cluster = "default"
        # nacos控制台登录用户名
        username = "nacos"
        # nacos控制台登录密码
        password = "nacos"
      }
      eureka {
        serviceUrl = "http://localhost:8761/eureka"
        application = "default"
        weight = "1"
      }
      redis {
        serverAddr = "localhost:6379"
        db = 0
        password = ""
        cluster = "default"
        timeout = 0
      }
      zk {
        cluster = "default"
        serverAddr = "127.0.0.1:2181"
        sessionTimeout = 6000
        connectTimeout = 2000
        username = ""
        password = ""
      }
      consul {
        cluster = "default"
        serverAddr = "127.0.0.1:8500"
      }
      etcd3 {
        cluster = "default"
        serverAddr = "http://localhost:2379"
      }
      sofa {
        serverAddr = "127.0.0.1:9603"
        application = "default"
        region = "DEFAULT_ZONE"
        datacenter = "DefaultDataCenter"
        cluster = "default"
        group = "SEATA_GROUP"
        addressWaitTime = "3000"
      }
      file {
        name = "file.conf"
      }
    }
    
    config {
      # file、nacos 、apollo、zk、consul、etcd3
      # 此处修改为nacos,意思是使用nacos作为seata业务调度数据配置中心。
      type = "nacos"
    
      nacos {
      	# nacos配置中心地址
        serverAddr = "192.168.0.5:8848"
        # 命名空间
        namespace = "fc0539dd-e76f-40ac-8f00-3b189d3e5075"
        # 分组名称
        group = "SEATA_GROUP"
        # nacos控制台登录用户名
        username = "nacos"
        # nacos控制台登录密码
        password = "nacos"
      }
      consul {
        serverAddr = "127.0.0.1:8500"
      }
      apollo {
        appId = "seata-server"
        apolloMeta = "http://192.168.1.204:8801"
        namespace = "application"
      }
      zk {
        serverAddr = "127.0.0.1:2181"
        sessionTimeout = 6000
        connectTimeout = 2000
        username = ""
        password = ""
      }
      etcd3 {
        serverAddr = "http://localhost:2379"
      }
      file {
        name = "file.conf"
      }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104

    五.配置nacos推送脚本“config.txt”

    打开服务端源码目录下的\script\config-center\config.txt
    该配置主要用于将数量繁多的nacos配置文件批量推送到nacos配置中心。
    参照以下内容进行修改。

    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
    # 这里的名字与file.conf中vgroup_mapping.my_test_tx_group = "default"相同
    service.vgroupMapping.my_test_tx_group=default
    # 这里的名字与file.conf中default.grouplist = "127.0.0.1:8091"相同
    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.degradeCheck=false
    client.tm.degradeCheckAllowTimes=10
    client.tm.degradeCheckPeriod=2000
    # 此处指定为db
    store.mode=db
    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
    # 如果数据库版本为8.0以上,则修改为com.mysql.cj.jdbc.Driver
    store.db.driverClassName=com.mysql.jdbc.Driver
    store.db.url=jdbc:mysql://127.0.0.1:3306/alibaba_seata?useUnicode=true
    # 数据库账户名
    store.db.user=root
    # 数据库密码
    store.db.password=root
    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
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86

    需要注意的是,在实际中要将以#开头的注释信息全部删除,不然脚本会执行失败。

    六.推送seata配置信息到nacos

    到服务端源码目录\script\config-center\nacos下
    通过git bash here窗口执行shell运行命令,将seata配置推送至nacos上。
    在这里插入图片描述
    执行下列命令:
    sh nacos-config.sh -h nacos服务ip地址 -p nacos服务端口 -g 分组名 -t 命名空间 -u nacos控制台登录账户 -w nacos控制台登录密码

    例如:

    sh nacos-config.sh -h 192.168.0.5 -p 8848 -g SEATA_GROUP -t fc0539dd-e76f-40ac-8f00-3b189d3e5075 -u nacos -w nacos
    
    
    • 1
    • 2

    当输出“ Init nacos config finished, please start seata-server.”表示推送完成。
    在这里插入图片描述
    打开命名空间下的配置信息,可以看到已经推上来了。
    在这里插入图片描述

    七.启动seata服务端

    进入seata服务端的\bin目录下,执行seata-server.bat文件(windows系统下),即可启动服务。
    在这里插入图片描述
    出现红框内的一行,说明启动成功了。

    八.pom中引入依赖

                
                <dependency>
                    <groupId>com.alibaba.cloudgroupId>
                    <artifactId>spring-cloud-starter-alibaba-seataartifactId>
                    <version>2.2.3.RELEASEversion>
                dependency>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    九.配置模块application.yaml信息

    # seata配置信息
    seata:
      # 是否启用,true为启用
      enabled: true
      # 分布式事务服务id,默认同当前application.name相同
      application-id: taobao-user-application
      # 这里的名字与file.conf中vgroup_mapping.my_test_tx_group = "default"相同
      tx-service-group: my_test_tx_group
      enable-auto-data-source-proxy: true
      # 此处配置该模块要连接的seata服务端等详细信息
      service:
        # 此处配置事务组信息,这里的名字与file.conf中的相同,也就是vgroup_mapping.my_test_tx_group = "default"
        vgroup-mapping:
          my_test_tx_group: default
          # seata服务端地址,可以单个也可以多个。此处为单个。
        grouplist:
          default: 127.0.0.1:8091
    
          # 指定seata的配置中心信息
      config:
        # 指定seata配置中心类型,为nacos
        type: nacos
        # nacos配置中心的详细信息
        nacos:
          # 所在的命名空间
          namespace: fc0539dd-e76f-40ac-8f00-3b189d3e5075
          # naocs配置中心地址
          server-addr: 192.168.0.5:8848
          # 所在的组
          group: SEATA_GROUP
          # nacos控制台登录账户
          username: "nacos"
          # nacos控制台登录密码
          password: "nacos"
    
          # 指定seata的注册中心信息
      registry:
        # 指定seata注册中心类型,为nacos
        type: nacos
        # nacos注册中心的详细信息
        nacos:
          # 指定本seata客户端在nacos注册中心的服务名
          application: seata-server
          # nacos注册中心地址
          server-addr: 192.168.0.5:8848
          # 所在的组
          group: SEATA_GROUP
          # 所在的命名空间
          namespace: fc0539dd-e76f-40ac-8f00-3b189d3e5075
          # nacos控制台登录账户
          username: "nacos"
          # nacos控制台登录密码
          password: "nacos"
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53

    参考:
    https://blog.csdn.net/shenyulong1214/article/details/109603231
    https://blog.csdn.net/elias607/article/details/126272279
    https://blog.csdn.net/Saintmm/article/details/121725763

  • 相关阅读:
    第三章 内存管理 九、基本分段存储管理方式
    Mybatis Plus入门进阶:特殊符号、动态条件、公共语句、关联查询、多租户插件
    微信小程序中 在xwml 中使用外部引入的 js进行判断计算
    高并发之缓存
    wireshark数据结构
    在python里如何实现switch函数的功能
    Github 2024-07-11 Go开源项目日报 Top10
    我做不到受每个人喜欢
    【ARK UI】HarmonyOS 从相册选择图片并显示到Image组件上
    Android音视频开发-AudioTrack
  • 原文地址:https://blog.csdn.net/chinatopno1/article/details/126721875