springboot集成分布式事务fescar(Seata)

简介

github地址

spring-boot-starter-seata:https://github.com/itrickzhang/spring-boot-starter-seata

seata版本

server和client版本为0.4.1,Seata 一直在快速迭代在1.0 之前都有可能出现协议不兼容 尽量使用版本号一致

说明

目前提供的示例是针对使用dubbo的服务,那Spring Boot的项目如何集成fescar呢?

快速开始

使用案例

Business Service购买商品的业务逻辑。整个业务逻辑由3个微服务驱动:

  • Storage service: 扣除给定商品的库存量.

  • Order service: 根据采购需求创建订单.

  • Account service: 借记用户帐户上的余额.

请求逻辑

fescar下载

下载地址:https://github.com/alibaba/fescar/releases

脚本

业务脚本


    
    
  1. DROP TABLE IF EXISTS `storage_tbl`;

  2. CREATE TABLE `storage_tbl` (

  3. `id` int(11) NOT NULL AUTO_INCREMENT,

  4. `commodity_code` varchar(255) DEFAULT NULL,

  5. `count` int(11) DEFAULT 0,

  6. PRIMARY KEY (`id`),

  7. UNIQUE KEY (`commodity_code`)

  8. ) ENGINE=InnoDB DEFAULT CHARSET=utf8;

  9. DROP TABLE IF EXISTS `order_tbl`;

  10. CREATE TABLE `order_tbl` (

  11. `id` int(11) NOT NULL AUTO_INCREMENT,

  12. `user_id` varchar(255) DEFAULT NULL,

  13. `commodity_code` varchar(255) DEFAULT NULL,

  14. `count` int(11) DEFAULT 0,

  15. `money` int(11) DEFAULT 0,

  16. PRIMARY KEY (`id`)

  17. ) ENGINE=InnoDB DEFAULT CHARSET=utf8;

  18. DROP TABLE IF EXISTS `account_tbl`;

  19. CREATE TABLE `account_tbl` (

  20. `id` int(11) NOT NULL AUTO_INCREMENT,

  21. `user_id` varchar(255) DEFAULT NULL,

  22. `money` int(11) DEFAULT 0,

  23. PRIMARY KEY (`id`)

  24. ) ENGINE=InnoDB DEFAULT CHARSET=utf8;

事务脚本


    
    
  1. -- 注意此处0.3.0+ 增加唯一索引 ux_undo_log

  2. CREATE TABLE `undo_log` (

  3. `id` bigint(20) NOT NULL AUTO_INCREMENT,

  4. `branch_id` bigint(20) NOT NULL,

  5. `xid` varchar(100) 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 AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;

集成


    
    
  1. <dependency>

  2. <groupId>org.springframework.boot</groupId>

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

  4. <version>${project.version}</version>

  5. </dependency>

使用

使用注解@GlobalTransactional


    
    
  1. @GlobalTransactional(timeoutMills = 300000, name = "spring-cloud-demo-tx")

  2. @RequestMapping(value = "/fescar/feign", method = RequestMethod.GET, produces = "application/json")

  3. public String feign() {

  4. LOGGER.info("business Service Begin ... xid: " + RootContext.getXID());

  5. String result = storageService.storage(COMMODITY_CODE, ORDER_COUNT);

  6. if (!SUCCESS.equals(result)) {

  7. throw new RuntimeException();

  8. }

  9. result = orderService.order(USER_ID, COMMODITY_CODE, ORDER_COUNT);

  10. if (!SUCCESS.equals(result)) {

  11. throw new RuntimeException();

  12. }

  13. return SUCCESS;

  14. }

demo运行

spring-boot-starter-seata-sample

  • Start AccountService

  • Start StorageService

  • Start OrderService

  • Run BusinessService for demo test

运行结果

启动demo

访问demo

数据库数据

我的公众号

发布了75 篇原创文章 · 获赞 85 · 访问量 10万+

猜你喜欢

转载自blog.csdn.net/zhangchangbin123/article/details/89391982