• 分布式事务解决方案Seata


    模拟场景

    在这里插入图片描述

    下载seata

    下载地址:https://github.com/seata/seata/releases/v0.9.0/

    修改配置文件

    将下载得到的压缩包进行解压,进入conf目录,调整下面的配置文件:
    registry.conf

    registry { 
    	type = "nacos" 
    	nacos { 
    		serverAddr = "localhost" 
    		namespace = "public" 
    		cluster = "default" 
    		} 
    	}
    config { 
    	type = "nacos" 
    	nacos { 
    		serverAddr = "localhost" 
    		namespace = "public" 
    		cluster = "default" 
    		} 
    	}
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16

    nacos-config.txt

    service.vgroup_mapping.service-product=default 
    service.vgroup_mapping.service-order=default
    
    • 1
    • 2

    这里的语法为: service.vgroup_mapping.${your-service-gruop}=default ,中间的
    ${your-service-gruop} 为自己定义的服务组名称, 这里需要我们在程序的配置文件bootstrap.yml中tx-service-group配置, 两个必须保持一致
    在这里插入图片描述

    初始化seata在nacos的配置

    # 初始化seata 的nacos配置 
    # 注意: 这里要保证nacos是已经正常运行的 
    cd conf 
    nacos-config.sh 127.0.0.1
    
    • 1
    • 2
    • 3
    • 4

    执行成功后可以打开Nacos的控制台,在配置列表中,可以看到初始化了很多Group为SEATA_GROUP
    的配置。

    启动seata服务

    cd bin 
    seata-server.bat -p 9000 -m file
    
    • 1
    • 2

    启动后在 Nacos 的服务列表下面可以看到一个名为 serverAddr 的服务。

    使用Seata实现事务控制

    初始化数据表

    在我们的数据库中加入一张undo_log表,这是Seata记录事务日志要用到的表

    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;
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13

    添加配置

    添加依赖

    <dependency> 
    	<groupId>com.alibaba.cloud</groupId> 
    	<artifactId>spring-cloud-starter-alibaba-seata</artifactId> 
    </dependency> 
    <dependency> 
    	<groupId>com.alibaba.cloud</groupId> 
    	<artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId> 
    </dependency>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    定义DataSourceProxyConfig

    Seata 是通过代理数据源实现事务分支的,所以需要配置 io.seata.rm.datasource.DataSourceProxy 的
    Bean,且是 @Primary默认的数据源,否则事务不会回滚,无法实现分布式事务

    @Configuration
    public class DataSourceProxyConfig {
        @Bean
        @ConfigurationProperties(prefix = "spring.datasource")
        public DruidDataSource druidDataSource() {
            return new DruidDataSource();
        }
    
        @Primary
        @Bean
        public DataSourceProxy dataSource(DruidDataSource druidDataSource) {
            return new DataSourceProxy(druidDataSource);
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14

    registry.conf

    在resources下添加Seata的配置文件 registry.conf

    registry {
        type = "nacos"
        nacos {
            serverAddr = "localhost"
            namespace = "public"
            cluster = "default"
        }
    }
    
    config {
        type = "nacos"
        nacos {
            serverAddr = "localhost"
            namespace = "public"
            cluster = "default"
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17

    bootstrap.yaml

    spring:
      application:
        name: service-product
      cloud:
        nacos:
          config:
            server-addr: localhost:8848 # nacos的服务端地址
            namespace: public
            group: SEATA_GROUP
        alibaba:
          seata:
            tx-service-group: service-product # 这里要跟nacos-config.txt中修改的配置保持一致
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12

    @GlobalTransactional

    再需要使用分布式全局事务的地方添加@GlobalTransactional注解即可

        @GlobalTransactional//全局事务控制
        public Order createOrder(Integer pid) {
            log.info("接收到{}号商品的下单请求,接下来调用商品微服务查询此商品信息", pid);
            //1 调用商品微服务,查询商品信息
            Product product = productService.findByPid(pid);
            log.info("查询到{}号商品的信息,内容是:{}", pid, JSON.toJSONString(product));
            //2 下单(创建订单)
            Order order = new Order();
            order.setUid(1);
            order.setUsername("测试用户");
            order.setPid(pid);
            order.setPname(product.getPname());
            order.setPprice(product.getPprice());
            order.setNumber(1);
            orderDao.save(order);
            log.info("创建订单成功,订单信息为{}", JSON.toJSONString(order));
            //3 扣库存m
            productService.reduceInventory(pid, order.getNumber());
            //4 向mq中投递一个下单成功的消息
            rocketMQTemplate.convertAndSend("order-topic", order);
            return order;
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22

    测试

    在扣减库存的方法中模拟异常

        @Transactional
        @Override
        public void reduceInventory(Integer pid, Integer number) {
            //查询
            Product product = productDao.findById(pid).get();
            //省略校验
    
            //内存中扣减
            product.setStock(product.getStock() - number);
    
            //模拟异常
            int i = 1 / 0;
    
            //保存
            productDao.save(product);
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16

    下单, 如期 出现异常
    在这里插入图片描述
    查看订单和库存, 发现没有生成订单, 库存也没有减少, 全局事务控制住了
    在这里插入图片描述
    在这里插入图片描述

  • 相关阅读:
    [nlp] 对抗学习 FGM, PGD到FreeLB
    数据结构(递归,链表实现递归)
    MySQL数据类型
    解决apt update执行时因签名而运行失败的问题
    Jmeter(六) - 从入门到精通 - 建立数据库测试计划(详解教程)
    Appium自动化测试<三>
    join、inner join、left join、right join、outer join的区别
    HyperLynx(二十八)板层噪声分析和SI/PI联合仿真实例
    基于SSM的家教系统的设计与实现毕业设计-附源码221752
    【web-解析目标】(1.2.3)解析应用程序:确定服务器端功能
  • 原文地址:https://blog.csdn.net/ZHUSHANGLIN/article/details/126369249