• Springboot整合Seata


    为微服务创建 UNDO_LOG 表

    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. `context` varchar(128) NOT NULL,
    7. `rollback_info` longblob NOT NULL,
    8. `log_status` int(11) NOT NULL,
    9. `log_created` datetime NOT NULL,
    10. `log_modified` datetime NOT NULL,
    11. `ext` varchar(100) DEFAULT NULL,
    12. PRIMARY KEY (`id`),
    13. UNIQUE KEY `ux_undo_log` (`xid`,`branch_id`)
    14. ) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;

    导入依赖

    1. <dependency>
    2. <groupId>com.alibaba.cloud</groupId>
    3. <artifactId>spring-cloud-starter-alibaba-seata</artifactId>
    4. </dependency>

    查看生成包版本 seat-all-0.7.1

    安装事务协调器

    seat-server下载:Releases · seata/seata · GitHub

    安装解压:seata-server-0.7.1

    注册中心配置config/registry.conf:修改 registry type = "nacos"

    启动seata-server

    双击 /bin/seata-server.bat

    启动后seata服务会被注册到nacos中心

    每一个需要使用分布式事务,都要使用seata DataSourceProxy代理自己的数据源

    1. package com.hdb.pingmoweb.order.config;
    2. import com.zaxxer.hikari.HikariDataSource;
    3. import io.seata.rm.datasource.DataSourceProxy;
    4. import org.springframework.beans.factory.annotation.Autowired;
    5. import org.springframework.boot.autoconfigure.jdbc.DataSourceProperties;
    6. import org.springframework.context.annotation.Configuration;
    7. import org.springframework.util.StringUtils;
    8. import javax.sql.DataSource;
    9. @Configuration
    10. public class MySeataConfig {
    11. @Autowired
    12. DataSourceProperties dataSourceProperties;
    13. @Bean
    14. public DataSource dataSource(DataSourceProperties dataSourceProperties){
    15. HikariDataSource dataSource = dataSourceProperties.initializeDataSourceBuilder().type(HikariDataSource.class).build();
    16. if(StringUtils.hasText(dataSourceProperties.getName())){
    17. dataSource.setPoolName(dataSourceProperties.getName());
    18. }
    19. return new DataSourceProxy(dataSource);
    20. }
    21. }

    每一个微服务,都必须导入registry.conf,file.conf 

    文件在seata-server config目录下复制粘贴进去 

    registry.conf

    file.conf   修改 vgroup_mapping.{application.name}-fescar-service-group = "default"

    修改后

    vgroup_mapping.pingmoweb-order-fescar-service-group = "default"

    修改测试分布式事务

    1、给分布式大事务的入口标注@GlobalTransactional

    2、每一个远程的小事务用@Transactional

    1. @Autowired
    2. private WareFeignService wareFeignService;
    3. @GlobalTransactional
    4. @Transactional
    5. @RequestMapping("/submitOrder/{val}")
    6. public R submitOrder(@PathVariable Long val){
    7. wareFeignService.addWare(val+"",val+"",val+"");
    8. OrderEntity order = new OrderEntity();
    9. order.setMemberId(val);
    10. order.setOrderSn(val+"");
    11. order.setCouponId(val);
    12. orderService.save(order);
    13. int i = 10/0;
    14. return R.ok();
    15. }
    1. @Autowired
    2. private WareInfoService wareInfoService;
    3. @Transactional
    4. @RequestMapping("/addWare")
    5. public R addWare(String address,String name,String areaCode){
    6. WareInfoEntity wareInfo = new WareInfoEntity();
    7. wareInfo.setAddress(address);
    8. wareInfo.setName(name);
    9. wareInfo.setAreacode(areaCode);
    10. wareInfoService.save(wareInfo);
    11. return R.ok();
    12. }

    测试结果,大事务通过添加@GlobalTransactional可以回滚子事务,使用线程异步编排子事务不会回滚。

    Seata仅适用于并发性能要求不高的后台管理系统 

  • 相关阅读:
    想要精通算法和SQL的成长之路 - 打家劫舍系列问题
    人工智能AI 全栈体系(二)
    Java之BigDecima容器学习心得
    Spring Retry教程(3)-模板方式的实现
    Linux扩展篇之Shell编程四(正则表达式入门)
    Go泛型解密:从基础到实战的全方位解析
    C++项目实战——基于多设计模式下的同步&异步日志系统-⑨-同步日志器类与日志器建造者类设计
    Linux进程信号的处理
    自主研发的流程引擎怎么样?好用吗?
    图片地址GPS经纬度查询
  • 原文地址:https://blog.csdn.net/qq_29385297/article/details/127583166