【Seata分布式事务使用过程】
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.引入依赖
<dependency><groupId>com.alibaba.cloud</groupId><artifactId>spring-cloud-alibaba-seata</artifactId><version>2.1.0.RELEASE</version><exclusions><exclusion><artifactId>seata-all</artifactId><groupId>io.seata</groupId></exclusion></exclusions></dependency>
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地址