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

到此这篇关于seata springcloud整合教程与遇到的坑的文章就介绍到这了,更多相关seata springcloud整合内容请搜索www.887551.com以前的文章或继续浏览下面的相关文章希望大家以后多多支持www.887551.com!