写点什么

五分钟带你了解 Seata 分布式事务

  • 2021 年 11 月 11 日
  • 本文字数:2355 字

    阅读完需:约 8 分钟

这个时候全局事务的提交就变得十分轻量级,就是把 undo_log 对应的记录删掉即可,即使是当时删除失败了,也已经不会影响全局事 务的最终结果,这次删不了,那就待会再删,程序删不了,没事,顶多人工删。


1.4.全局事务回滚流程

如果全局事务的任何一个事务分支失败了,那么全局事务就进入“回滚“流程,回滚时依据先前保存好数据镜像,将原来的数据回放回去。


如果全局回放成功,那么数据的一致性也就得到了保证,如果回放不成功,那么事务就进入异常。应对异常,可能需要重试,可能需要人工介入。


2.Seata 在 dubbo 中的使用

2.1.新增 mvn 依赖

在需要增加分布式事务的模块加上相关依赖


  1. <dependency>

  2. <groupId>io.seata</groupId>

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


【一线大厂Java面试题解析+后端开发学习笔记+最新架构讲解视频+实战项目源码讲义】
浏览器打开:qq.cn.hn/FTf 免费领取
复制代码


<version>1.1.0</version>


  1. </dependency>

2.2.修改 properties 文件

  1. #服务名称

  2. dubbo.application.name=service

  3. #注册中心地址

  4. dubbo.registry.address=127.0.0.1:2181

  5. #注册中心类型

  6. dubbo.registry.protocol=zookeeper

  7. #版本号

  8. dubbo.application.version=3

  9. # Dubbo Protocol

  10. #协议名称

  11. dubbo.protocol.name=dubbo

  12. #服务暴露端口

  13. dubbo.protocol.port=20880

  14. seata.enabled=true

  15. seata.application-id=biz-service

  16. seata.tx-service-group=my_test_tx_group

  17. seata.client.rm.async-commit-buffer-limit=1000

  18. seata.client.rm.report-retry-count=5

  19. seata.client.rm.table-meta-check-enable=false

  20. seata.client.rm.report-success-enable=false

  21. seata.client.rm.lock.retry-interval=10

  22. seata.client.rm.lock.retry-times=30

  23. seata.client.rm.lock.retry-policy-branch-rollback-on-conflict=true

  24. seata.client.tm.commit-retry-count=5

  25. seata.client.tm.rollback-retry-count=5

  26. seata.client.undo.data-validation=true

  27. seata.client.undo.log-serialization=jackson

  28. seata.client.undo.log-table=undo_log

  29. seata.client.log.exceptionRate=100

  30. seata.service.vgroup-mapping.my_test_tx_group=default

  31. seata.service.grouplist.default=127.0.0.1:8091

  32. seata.transport.shutdown.wait=3

  33. seata.transport.thread-factory.boss-thread-prefix=NettyBoss

  34. seata.transport.thread-factory.worker-thread-prefix=NettyServerNIOWorker

  35. seata.transport.thread-factory.server-executor-thread-prefix=NettyServerBizHandler

  36. seata.transport.thread-factory.share-boss-worker=false

  37. seata.transport.thread-factory.client-selector-thread-prefix=NettyClientSelector

  38. seata.transport.thread-factory.client-selector-thread-size=1

  39. seata.transport.thread-factory.client-worker-thread-prefix=NettyClientWorkerThread

  40. seata.transport.thread-factory.worker-thread-size=default

  41. seata.transport.thread-factory.boss-thread-size=1

  42. seata.transport.type=TCP

  43. seata.transport.server=NIO

  44. seata.transport.heartbeat=true

  45. seata.transport.serialization=seata

  46. seata.transport.compressor=none

  47. seata.transport.enable-client-batch-send-request=true

  48. seata.config.type=file

  49. seata.registry.type=file

2.3.新增配置类

  1. package cn.enjoy.mt.order.config;

  2. import com.alibaba.druid.pool.DruidDataSource;

  3. import org.apache.ibatis.session.SqlSessionFactory;

  4. import org.mybatis.spring.SqlSessionFactoryBean;

  5. import org.springframework.boot.context.properties.ConfigurationProperties;

  6. import org.springframework.context.annotation.Bean;

  7. import org.springframework.context.annotation.Configuration;

  8. import org.springframework.core.io.support.PathMatchingResourcePatternResolver;

  9. import javax.sql.DataSource;

  10. @Configuration

  11. public class SeataConfiguration {

  12. @Bean

  13. @ConfigurationProperties(prefix = "spring.datasource")

  14. public DataSource druidDataSource() {

  15. DruidDataSource druidDataSource = new DruidDataSource();

  16. return druidDataSource;

  17. }

  18. @Bean

  19. public SqlSessionFactory sqlSessionFactory(DataSource dataSource) throws Exception {

  20. SqlSessionFactoryBean factoryBean = new SqlSessionFactoryBean();

  21. factoryBean.setDataSource(dataSource);

  22. factoryBean.setMapperLocations(new PathMatchingResourcePatternResolver()

  23. .getResources("classpath:/mapping/.xml"));

  24. return factoryBean.getObject();

  25. }

  26. }


2.4.新增 undo_log 日志表


  1. CREATE TABLE undo_log (

  2. id bigint(20) NOT NULL AUTO_INCREMENT,

  3. branch_id bigint(20) NOT NULL,

  4. xid varchar(100) NOT NULL,

  5. context varchar(128) NOT NULL,

  6. rollback_info longblob NOT NULL,

  7. log_status int(11) NOT NULL,

  8. log_created datetime NOT NULL,

  9. log_modified datetime NOT NULL,

  10. ext varchar(100) DEFAULT NULL,

  11. PRIMARY KEY (id),

  12. UNIQUE KEY ux_undo_log (xid,branch_id)

  13. ) ENGINE=InnoDB DEFAULT CHARSET=utf8;


2.5.SeataProperties


  1. package cn.enjoy.mt.order.config;

  2. import org.springframework.boot.context.properties.ConfigurationProperties;

  3. @ConfigurationProperties("spring.cloud.alibaba.seata")

  4. public class SeataProperties {

  5. private String txServiceGroup;

  6. public SeataProperties() {

  7. }

  8. public String getTxServiceGroup() {

  9. return this.txServiceGroup;

  10. }

  11. public void setTxServiceGroup(String txServiceGroup) {

  12. this.txServiceGroup = txServiceGroup;

  13. }

  14. }


2.6.修改启动类


  1. package cn.enjoy.mt.order;

  2. import org.apache.dubbo.config.spring.context.annotation.EnableDubbo;

  3. import org.mybatis.spring.annotation.MapperScan;

评论

发布
暂无评论
五分钟带你了解Seata分布式事务