seata springcloud整合教程与遇到的坑

来自:互联网
时间:2021-07-06
阅读:

SEATA概要

seata 是alibaba 出的一款分布式事务管理器,他有侵入性小,实现简单等特点。我们能够使用seata 实现分布式事务管理,

是微服务必备的组件。他可以实现在微服务之间的事务管理,也可以实现多个数据源的事务管理。

seata 在阿里内部,和众多的公司都有应用,因此我们可以放心的使用它。

依赖

<dependency>
    <groupId>com.alibaba.cloud</groupId>
    <artifactId>spring-cloud-starter-alibaba-seata</artifactId>
</dependency>

建表

AT模式客户端服务的数据库都需要建表undo_log
否则报错

java.sql.SQLSyntaxErrorException: Table ‘psr_enterprise_control_test.undo_log' doesn't exist

官方GIT脚本文件

-- 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';

配置

application.yml

seata:
  enabled: true
  enable-auto-data-source-proxy: true
  tx-service-group: my_test_tx_group # 与seata.service.vgroup-mapping一致
  registry:
    type: nacos # 与seata注册中心相同
    nacos:
      application: seata-server
      server-addr: ${PSR_NACOS:localhost:8848}
      namespace: test
      group: application
      cluster: default
  config:
    type: nacos # 与seata配置中心相同
    nacos:
      server-addr: ${PSR_NACOS:localhost:8848}
      group: seata
      namespace: test
  service:
    vgroup-mapping:
      my_test_tx_group: default # 事务分组名
    disable-global-transaction: false
  client:
    rm:
      report-success-enable: false

启用全局事务

@GlobalTransactional

返回顶部
顶部