仅四步教你快速接入Seata分布式事务


第一步:

首先访问:https://seata.io/zh-cn/blog/download.html

下载我们需要使用的seata服务并启动.

第二步:

在你的数据库中加入undo_log这张表

-- 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`( `id` BIGINT(20) NOT NULL AUTO_INCREMENT COMMENT 'increment id', `branch_id` BIGINT(20) NOT NULL COMMENT 'branch transaction id', `xid` VARCHAR(100) 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 NOT NULL COMMENT 'create datetime', `log_modified` DATETIME NOT NULL COMMENT 'modify datetime', PRIMARY KEY (`id`), UNIQUE KEY `ux_undo_log` (`xid`, `branch_id`)) ENGINE = InnoDB AUTO_INCREMENT = 1 DEFAULT CHARSET = utf8 COMMENT ='AT transaction mode undo table';


第三步:

在你的项目中引入seata依赖(注:1.1.0版本目前需通过oss远程仓库发布,等正式版发布即可直接使用,或者可通过拉取1.1.0的源码进行本地打包使用.)

oss远程仓库:

https://oss.sonatype.org/index.html#view-repositories;snapshots~browsestorage~seata


<dependency>

<groupId>io.seata</groupId>

<artifactId>seata-spring-boot-starter</artifactId>    

 <version>1.1.0</version>

</dependency>

注:如果使用的是springcloud 版本切换为spring-cloud-alibaba-seata 2.2.0

<dependency> <groupId>io.seata</groupId> <artifactId>spring-cloud-alibaba-seata</artifactId> <version>2.2.0</version></dependency>


并在你参与其中的项目的application.yml中都加入如下配置(视频内容不方便修改,在配置上请参考图文内容)


seata: enabled: true application-id: test #可自定义 tx-service-group: my_test_tx_group #可自定义 # enable-auto-data-source-proxy: true # use-jdk-proxy: false service: vgroup-mapping: my_test_tx_group: default grouplist: default: 127.0.0.1:8091 # disable-global-transaction: false config: type: file file: name: file.conf registry: type: file


application.properties参考如下:



seata.enabled=trueseata.application-id=testseata.tx-service-group=my_test_tx_group# seata.enable-auto-data-source-proxy=true# seata.use-jdk-proxy=falseseata.service.vgroup-mapping.my_test_tx_group=defaultseata.service.grouplist.default=127.0.0.1:8091seata.service.enable-degrade=false# seata.service.disable-global-transaction=falseseata.config.type=fileseata.config.file.name=file.confseata.registry.type=file


注:starter1.1.0的自动代理支持属性配置和注解@EnableAutoDataSourceProxy两种方式

第四步:

在您的发起者的接口上加入@GlobalTransactional,并查看server日志以及程序日志进行调试即可.


原文地址;https://mp.weixin.qq.com/s/BOntA2mcdF-e0t2e4CBjPQ

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容