0
点赞
收藏
分享

微信扫一扫

Seata(二): Seata-Client集成 & 配置

黎轩的闲暇时光 2022-04-05 阅读 31

一、官方文档

  • 业务系统集成Client

Seata部署指南icon-default.png?t=M276https://seata.io/zh-cn/docs/ops/deploy-guide-beginner.html

  • Seata参数配置文档

https://seata.io/zh-cn/docs/user/configurations.htmlicon-default.png?t=M276https://seata.io/zh-cn/docs/user/configurations.html

  • Seata示例项目工程

GitHub - seata/seata-samples: seata-samplesicon-default.png?t=M276https://github.com/seata/seata-samples

  • Client客户端配置文件

seata/script/client at 1.3.0 · seata/seata · GitHub:fire: Seata is an easy-to-use, high-performance, open source distributed transaction solution. - seata/script/client at 1.3.0 · seata/seatahttps://github.com/seata/seata/tree/1.3.0/script/client

二、业务客户端集成步骤

  • 查看工程依赖seata版本

  • 选择Github响应的版本参考

 

1、在业务DB库新建undo_log表

如果在业务中有多个库,需要在每个库新建undo_log表

  • Github DB 脚本文档地址

seata/script/client/at/db at develop · seata/seata · GitHubicon-default.png?t=M276https://github.com/seata/seata/tree/develop/script/client/at/db

-- 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       NOT NULL COMMENT 'branch transaction id',
    `xid`           VARCHAR(128) 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';

 

2、工程增加依赖包

  • 父工程 pom.xml依赖了spring-cloud-alibaba
<properties>
    <spring-cloud-alibaba-dependencies.version>2.2.7.RELEASE</spring-cloud-alibaba-dependencies.version>
</properties>

<dependencyManagement>
    <dependencies>
    
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-alibaba-dependencies</artifactId>
            <version>${spring-cloud-alibaba-dependencies.version}</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
        
    </dependencies>
</dependencyManagement>

3、在需要用到的业务工程增加Seata依赖

spring-cloud-alibaba-dependencies已经管理管理好相应的 spring-cloud-starter-alibaba-seata依赖的版本由spring-cloud-alibaba统一管理,不用再手动指定使用的seata版本

<dependencies>

    <!-- Seata分布式事务 -->
    <dependency>
        <groupId>com.alibaba.cloud</groupId>
        <artifactId>spring-cloud-starter-alibaba-seata</artifactId>
    </dependency>
    
<dependencies>

 4、在application.yml或者bootstrap.yml增加seata配置文件

tx-service-group: xxxx-global-biz-tx-group seata 全局事务分组 与 file.conf service 配置 vgroupMapping.xxxx-global-biz-tx-group一一对应

### Seata事务配置
---
spring:
  cloud:
    alibaba:
      seata:
        ### seata 全局事务分组 与 file.conf service 配置 vgroupMapping.westyle-global-biz-tx-group一一对应
        tx-service-group: westyle-global-biz-tx-group

5、添加Seata配置文件

  • Seata官方参数文档

https://seata.io/zh-cn/docs/user/configurations.htmlicon-default.png?t=M276https://seata.io/zh-cn/docs/user/configurations.html

  • Github 配置文件

https://github.com/seata/seata/tree/develop/script/client/conficon-default.png?t=M276https://github.com/seata/seata/tree/develop/script/client/conf

 

 

5.1、添加yaml配置文件(推荐)

pom.xml定义的全局变量,Maven打包时,自动把定义的变量填充到对应的配置文件

@spring.cloud.seata.registry.nacos.server-addr@

@spring.cloud.seata.registry.nacos.group@

@spring.cloud.seata.registry.nacos.namespace@

### seata 客户端相关配置
seata:
  enabled: true
  # 使用默认的应用名称即可
  # application-id: applicationName
  # seata 全局事务分组 与 file.conf_bak service 配置 vgroupMapping.xxxx-global-biz-tx-group一一对应
  # 自定义命名特别注意: Springboot2 需要以 "-"命名区分,如果以下划线"_",启动会报错
  tx-service-group: xxxx-global-biz-tx-group
  # 默认已开启数据源自动代理
  enable-auto-data-source-proxy: true
  # 使用JDK代理
  use-jdk-proxy: false
  excludes-for-auto-proxying: firstClassNameForExclude,secondClassNameForExclude
  # 对应file.conf -> client节点的配置
  client:
    rm:
      # 异步提交缓存队列长度 默认10000。 二阶段提交成功,RM异步清理undo队列
      async-commit-buffer-limit: 10000
      # 一阶段结果上报TC重试次数 默认5次
      report-retry-count: 5
      # 自动刷新缓存中的表结构 默认false
      table-meta-check-enable: false
      # 是否上报一阶段成功 true、false,从1.1.0版本开始,默认false.true用于保持分支事务生命周期记录完整,false可提高不少性能
      report-success-enable: false
      saga-branch-register-enable: false
      lock:
        # 校验或占用全局锁重试间隔 默认10,单位毫秒
        retry-interval: 10
        # 校验或占用全局锁重试次数 默认30
        retry-times: 30
        # 分支事务与其它全局回滚事务冲突时锁策略 默认true,优先释放本地锁让回滚成功
        retry-policy-branch-rollback-on-conflict: true
    tm:
      degrade-check: false
      degrade-check-period: 2000
      degrade-check-allow-times: 10
      # 一阶段全局提交结果上报TC重试次数 默认1次,建议大于1
      commit-retry-count: 5
      # 一阶段全局回滚结果上报TC重试次数 默认1次,建议大于1
      rollback-retry-count: 5
    undo:
      # 二阶段回滚镜像校验 默认true开启,false关闭
      data-validation: true
      # undo序列化方式  默认jackson
      log-serialization: jackson
      # 自定义undo表名  默认undo_log
      log-table: undo_log
      only-care-update-columns: true
    log:
      # 日志异常输出概率   默认100,目前用于undo回滚失败时异常堆栈输出,百分之一的概率输出,回滚失败基本是脏数据,无需输出堆栈占用硬盘空间
      exceptionRate: 100
  # 对应file.conf -> service节点的配置
  service:
    vgroup-mapping:
      # 事务群组 xxxx-global-biz-tx-group为分组,配置项值为TC集群名
      # 特别注意配置该项值为:default 时配置 default.grouplist值不生效,无论怎么改变都是默认值,读取的是127.0.0.1:8091
      # 自定义命名特别注意: Springboot2 需要以 "-"命名区分,如果以下划线"_",启动会报错
      xxxx-global-biz-tx-group: default
    grouplist:
      # only support when registry.type=file, please don't set multiple addresses
      # TC服务列表 仅注册中心为file时使用 注意这里的 xxxx-global-biz 域名上面一个配置项的值一一对应
      default: @spring.cloud.seata.server.grouplist@
    # degrade, current not support
    enable-degrade: false
    # disable seata
    # 全局事务开关 默认false。false为开启,true为关闭
    disable-global-transaction: false
  # 对应file.conf -> transport节点的配置
  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
      worker-thread-size: default
      boss-thread-size: 1
    type: TCP
    server: NIO
    heartbeat: true
    serialization: seata
    compressor: none
    enable-client-batch-send-request: true
  # 对应registry.conf -> config节点的配置
  config:
    # file 、nacos 、eureka、redis、zk、consul、etcd3、sofa 默认为 file
    type: file
    # config.type=nacos时,该配置生效
    nacos:
      serverAddr: 127.0.0.1:8848
      namespace:
      group: SEATA_GROUP
      username: ""
      password: ""
  # 对应registry.conf -> registry节点的配置
  registry:
    # file 、nacos 、eureka、redis、zk、consul、etcd3、sofa 默认为file
    type: nacos
    nacos:
      # seata-server注册到Nacos上的应用名称 需要与seata-server一一对应
      # 这里作为seata的客户端,连接到nacos注册中心,拉取seata-server在nacos上的在线节点
      application: seata-server
      # seata-server的Nacos注册地址 需要与seata-server的nacos一一对应
      server-addr: @spring.cloud.seata.registry.nacos.server-addr@
      # Nacos注册分组名 特别注意,需要与seata-server一一对应
      group: @spring.cloud.seata.registry.nacos.group@
      # Nacos注册命名空间 特别注意,需要与seata-server一一对应
      namespace: @spring.cloud.seata.registry.nacos.namespace@
      username: ""
      password: ""

 

举报

相关推荐

0 条评论