手机网站整站下载,网站建设公司有哪,大学什么专业是学网站开发的,邯郸市房价目录 1、添加依赖2.、配置Seata3、创建AT模式表4、使用Seata分布式事务 1、添加依赖 dependencygroupIdio.seata/groupIdartifactIdseata-spring-boot-starter/artifactId/dependency上述依赖适用于springboot项目
如果你的项… 目录 1、添加依赖2.、配置Seata3、创建AT模式表4、使用Seata分布式事务 1、添加依赖 dependencygroupIdio.seata/groupIdartifactIdseata-spring-boot-starter/artifactId/dependency上述依赖适用于springboot项目
如果你的项目是springcloud项目那么可以使用下面这个依赖 dependencygroupIdcom.alibaba.cloud/groupIdartifactIdspring-cloud-starter-alibaba-seata/artifactId/dependencyspring-cloud-starter-alibaba-seata是为Spring Cloud应用程序开发的可以在Spring Cloud环境中使用并提供了与Spring Cloud Config、Eureka、Nacos等应用程序所需的集成。它依赖于Spring Cloud Alibaba项目因此需要引入spring-cloud-starter-alibaba-dependencies的BOMBill of Materials来管理版本依赖关系。
spring-cloud-starter-alibaba-seata推荐依赖配置方式自定义seata-spring-boot-starter版本 dependencygroupIdio.seata/groupIdartifactIdseata-spring-boot-starter/artifactIdversion最新版/version/dependencydependencygroupIdcom.alibaba.cloud/groupIdartifactIdspring-cloud-starter-alibaba-seata/artifactIdversion最新版本/versionexclusionsexclusiongroupIdio.seata/groupIdartifactIdseata-spring-boot-starter/artifactId/exclusion/exclusions/dependency2.、配置Seata
在 Spring Boot 应用的配置文件application.yml 或 application.properties中配置 Seata 相关参数例如
application.yml
seata:enabled: trueapplication-id: your_application_idtx-service-group: default_tx_groupenable-auto-data-source-proxy: truedata-source-proxy-mode: ATservice:vgroup-mapping:default_tx_group: defaultregistry:type: nacosnacos:server-addr: 127.0.0.1:8848group: SEATA_GROUPnamespace:username: nacospassword: nacosconfig:type: nacosnacos:server-addr: 127.0.0.1:8848group: SEATA_GROUPdata-id: seataServer.propertiesnamespace:username: nacospassword: nacos上面我seata的注册和配置都放到了nacos上nacos上的配置可以看我上一篇文章https://blog.csdn.net/qq_36551991/article/details/135968940
注data-id要与我们nacos创建的配置文件的data-id一致这里默认的data-id为seata.properties
对于多个服务来说都需要配置seata而且注册中心需要一致
3、创建AT模式表
在AT模式下每个业务数据库都必须创建 undo_log 表undo_log 表是 Seata AT模式必须创建的表主要用于分支事务的回滚
表机构地址https://github.com/apache/incubator-seata/tree/master/script/client/at/db
大家在上面地址选择自己使用的数据库的sql脚本进行执行我的数据库是mysql所以使用的是mysql.sql
Mysql表结构如下
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,PRIMARY KEY (id),UNIQUE KEY ux_undo_log (xid,branch_id)
) ENGINEInnoDB AUTO_INCREMENT1 DEFAULT CHARSETutf8;4、使用Seata分布式事务
在我们的事务方法上添加GlobalTransactional注解即可开启全局事务主服务加上GlobalTransactional注解即可被调用服务不用加GlobalTransactional和Transactional
下面是一个示例样板 //seata全局事务注解GlobalTransactional (rollbackForException.class)public void createOrder(Integer userId, Integer productId) {log.info(当前 XID: {}, RootContext.getXID());//1、减库存productFeigne.reduceStock(productId, 1);//2、减余额accountFeign.reduceBalance(userId, product.getPrice());//3、下订单Orders order new Orders();ordersMapper.insertSelective(order);}