【Seata分布式事务使用过程】
创始人
2024-06-01 11:16:04
0

Seata分布式事务实现过程

  • springcloud-eureka-feign-mybatis-seata
  • 分布式事务实现过程
    • 1.引入依赖
    • 2.添加注册配置
    • 3.创建file.conf
    • 4.修改各大需要参与事务微服务的application.yml配置,配置通信指定的组名(fsp_tx_group 在file.conf中有)
    • 5.DataSourceConfiguration.java
    • 6.添加undo_log表
    • 7.启动 Seata-Server
    • 8.在需要执行分布式事务的入口方法上添加注解@GlobalTransactional开启事务

springcloud-eureka-feign-mybatis-seata

分布式事务实现过程

1.引入依赖

com.alibaba.cloudspring-cloud-alibaba-seata2.1.0.RELEASEseata-allio.seata

2.添加注册配置

创建同名文件:registry.conf
–TC存储RM注册信息的文件配置,默认使用文件存储

registry {# file 、nacos 、eureka、redis、zk、consul、etcd3、sofatype = "eureka"  修改这里,指明注册中心使用什么nacos {serverAddr = "localhost"namespace = ""cluster = "default"}eureka {serviceUrl = "http://localhost:8761/eureka"  修改这里application = "default"  weight = "1"}redis {serverAddr = "localhost:6379"db = "0"}zk {cluster = "default"serverAddr = "127.0.0.1:2181"session.timeout = 6000connect.timeout = 2000}consul {cluster = "default"serverAddr = "127.0.0.1:8500"}etcd3 {cluster = "default"serverAddr = "http://localhost:2379"}sofa {serverAddr = "127.0.0.1:9603"application = "default"region = "DEFAULT_ZONE"datacenter = "DefaultDataCenter"cluster = "default"group = "SEATA_GROUP"addressWaitTime = "3000"}file {name = "file.conf"}
}

3.创建file.conf

–各大微服务的RM和TC之间的通信配置

## transaction log store
store {## store mode: file、dbmode = "db"   修改这里,表明事务信息用db存储## file store 当mode=db时,此部分配置就不生效了,这是mode=file的配置file {dir = "sessionStore"# branch session size , if exceeded first try compress lockkey, still exceeded throws exceptionsmax-branch-session-size = 16384# globe session size , if exceeded throws exceptionsmax-global-session-size = 512# file buffer size , if exceeded allocate new bufferfile-write-buffer-cache-size = 16384# when recover batch read sizesession.reload.read_size = 100# async, syncflush-disk-mode = async}## database store  mode=db时,事务日志存储会存储在这个配置的数据库里db {## the implement of javax.sql.DataSource, such as DruidDataSource(druid)/BasicDataSource(dbcp) etc.datasource = "dbcp"## mysql/oracle/h2/oceanbase etc.db-type = "mysql"driver-class-name = "com.mysql.jdbc.Driver"url = "jdbc:mysql://116.62.62.26/seat-server"  修改这里user = "root"  修改这里password = "root"  修改这里min-conn = 1max-conn = 3global.table = "global_table"branch.table = "branch_table"lock-table = "lock_table"query-limit = 100}
}
service {#vgroup->rgroupvgroup_mapping.fsp_tx_group = "default"  修改这里,fsp_tx_group这个事务组名称是我自定义的,一定要与client端的这个配置一致!否则会报错!#only support single nodedefault.grouplist = "127.0.0.1:8091"   此配置作用参考:https://blog.csdn.net/weixin_39800144/article/details/100726116#degrade current not supportenableDegrade = false#disabledisable = false#unit ms,s,m,h,d represents milliseconds, seconds, minutes, hours, days, default permanentmax.commit.retry.timeout = "-1"max.rollback.retry.timeout = "-1"
}

4.修改各大需要参与事务微服务的application.yml配置,配置通信指定的组名(fsp_tx_group 在file.conf中有)

spring:application:name: order-servercloud:alibaba:seata:tx-service-group: fsp_tx_group  这个fsp_tx_group自定义命名很重要,server,client都要保持一致

5.DataSourceConfiguration.java

/*** 数据源代理* @author wangzhongxiang*/
@Configuration
public class DataSourceConfiguration {/*** 普通数据源*/@Bean@ConfigurationProperties(prefix = "spring.datasource")public DataSource druidDataSource(){DruidDataSource druidDataSource = new DruidDataSource();return druidDataSource;}/*** 代理数据源,DataSourceProxy 绑定undo_log操作*/@Primary@Bean("dataSource")public DataSourceProxy dataSource(DataSource druidDataSource){return new DataSourceProxy(druidDataSource);}/*** MyBatis-->手动指定sqlSessionFactory所使用的的数据源*/@Beanpublic SqlSessionFactory sqlSessionFactory(DataSourceProxy dataSourceProxy)throws Exception{SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();sqlSessionFactoryBean.setDataSource(dataSourceProxy);sqlSessionFactoryBean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath*:/mapper/*.xml"));sqlSessionFactoryBean.setTransactionFactory(new SpringManagedTransactionFactory());return sqlSessionFactoryBean.getObject();}}

6.添加undo_log表

--在业务相关的数据库中添加undo_log表,用于保存需要回滚的数据
-- 注意此处0.3.0+ 增加唯一索引 ux_undo_log
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,`ext` varchar(100) DEFAULT NULL,PRIMARY KEY (`id`),UNIQUE KEY `ux_undo_log` (`xid`,`branch_id`)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;

7.启动 Seata-Server

将客户端下载到本地,解压后,启动即可

8.在需要执行分布式事务的入口方法上添加注解@GlobalTransactional开启事务

官网demo地址

相关内容

热门资讯

a股有几家上市公司 中国a股上... 齐鲁晚报讯祁鲁镇记者张通讯员6月15日,中国证监会山东监管局连续发布3份辅导工作总结报告,其中2份的...
开办小型日化厂违法吗 小型洗洁... 食品安全一直受到关注,尤其是在这样的大热天,人们对进口的东西更加谨慎。抚顺的朋友们,你们可能已经注意...
马云预言未来十大行业 暴利行业... 说到马云,很多人都很熟悉他。马云通过自己的努力和奋斗,逐渐开创了网上支付,他的眼光和勇气是非常独特的...
总是很自卑怎么办?做什么都做不... 总是很自卑怎么办?做什么都做不好,心态也很不好,感觉真的好累自卑跟性格有很大关系!你可能有些内向!其...
求一部国外电影,关于博物馆的故... 求一部国外电影,关于博物馆的故事讲的是在一个博物馆里每到晚上里面的东西就会复活,好像恐龙骨架什么的,...
我愿做江州司马为你泪湿青衫 我愿做江州司马为你泪湿青衫《琵琶行》白居易 座中泣下谁最多,江州司马青衫湿。同是天涯沦落人,相逢...
肩周炎怎么治? 肩周炎怎么治?肩周炎又称冻结肩、五十肩,是肩关节周围软组织慢性炎症性病变,主要以保守治疗为主。具体治...
谁有古代言而无信的例子? 谁有古代言而无信的例子?谁有古代言而无信的例子? 秦惠文王、张仪:秦惠文王更元十二年(前313年...
学生成长记录手册上家长寄语怎么... 学生成长记录手册上家长寄语怎么写可以写的抒情点,如“在生活中的每一天,都是一个阳光明媚的日子,因为,...
和伴侣在一起时会做哪些事情? 和伴侣在一起时会做哪些事情?情侣刚在一起的时候总是想把自己好的一面展现给对方,但是随着时间长了,感情...