美文网首页分布式事务
分布式事务-seata实践

分布式事务-seata实践

作者: 南园故剑00 | 来源:发表于2020-06-01 16:54 被阅读0次

选择案例 https://github.com/seata/seata-samples/tree/master/springcloud-eureka-feign-mybatis-seata

0. 建表语句

undo_log 为事务回滚之用

CREATE DATABASE `seat_account`;

USE `seat_account`;

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,
                            PRIMARY KEY (`id`),
                            UNIQUE KEY `ux_undo_log` (`xid`,`branch_id`)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;

CREATE TABLE `account` (
                           `id` bigint(11) NOT NULL AUTO_INCREMENT COMMENT 'id',
                           `user_id` bigint(11) DEFAULT NULL COMMENT '用户id',
                           `total` decimal(10,0) DEFAULT NULL COMMENT '总额度',
                           `used` decimal(10,0) DEFAULT NULL COMMENT '已用余额',
                           `residue` decimal(10,0) DEFAULT '0' COMMENT '剩余可用额度',
                           PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8;

INSERT INTO `seat_account`.`account` (`id`, `user_id`, `total`, `used`, `residue`) VALUES ('1', '1', '1000', '0', '100');


CREATE DATABASE `seat_order`;

USE `seat_order`;

CREATE TABLE `order` (
                         `id` bigint(11) NOT NULL AUTO_INCREMENT,
                         `user_id` bigint(11) DEFAULT NULL COMMENT '用户id',
                         `product_id` bigint(11) DEFAULT NULL COMMENT '产品id',
                         `count` int(11) DEFAULT NULL COMMENT '数量',
                         `money` decimal(11,0) DEFAULT NULL COMMENT '金额',
                         PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=7 DEFAULT CHARSET=utf8;

ALTER TABLE `order` ADD COLUMN `status` int(1) DEFAULT NULL COMMENT '订单状态:0:创建中;1:已完结' AFTER `money` ;

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,
                            PRIMARY KEY (`id`),
                            UNIQUE KEY `ux_undo_log` (`xid`,`branch_id`)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;

CREATE DATABASE seat_storage;

USE seat_storage;

CREATE TABLE `storage` (
                           `id` bigint(11) NOT NULL AUTO_INCREMENT,
                           `product_id` bigint(11) DEFAULT NULL COMMENT '产品id',
                           `total` int(11) DEFAULT NULL COMMENT '总库存',
                           `used` int(11) DEFAULT NULL COMMENT '已用库存',
                           `residue` int(11) DEFAULT NULL COMMENT '剩余库存',
                           PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8;

INSERT INTO `seat_storage`.`storage` (`id`, `product_id`, `total`, `used`, `residue`) VALUES ('1', '1', '100', '0', '100');

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,
                            PRIMARY KEY (`id`),
                            UNIQUE KEY `ux_undo_log` (`xid`,`branch_id`)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;

CREATE DATABASE seat_server;

USE seat_server;

-- the table to store GlobalSession data
drop table IF EXISTS `global_table`;
create table `global_table` (
                                `xid` varchar(128)  not null,
                                `transaction_id` bigint,
                                `status` tinyint not null,
                                `application_id` varchar(32),
                                `transaction_service_group` varchar(32),
                                `transaction_name` varchar(64),
                                `timeout` int,
                                `begin_time` bigint,
                                `application_data` varchar(2000),
                                `gmt_create` datetime,
                                `gmt_modified` datetime,
                                primary key (`xid`),
                                key `idx_gmt_modified_status` (`gmt_modified`, `status`),
                                key `idx_transaction_id` (`transaction_id`)
);

-- the table to store BranchSession data
drop table  IF EXISTS `branch_table`;
create table `branch_table` (
                                `branch_id` bigint not null,
                                `xid` varchar(128) not null,
                                `transaction_id` bigint ,
                                `resource_group_id` varchar(32),
                                `resource_id` varchar(256) ,
                                `lock_key` varchar(128) ,
                                `branch_type` varchar(8) ,
                                `status` tinyint,
                                `client_id` varchar(64),
                                `application_data` varchar(2000),
                                `gmt_create` datetime,
                                `gmt_modified` datetime,
                                primary key (`branch_id`),
                                key `idx_xid` (`xid`)
);

-- the table to store lock data
drop table  IF EXISTS `lock_table`;
create table `lock_table` (
                              `row_key` varchar(128) not null,
                              `xid` varchar(96),
                              `transaction_id` long ,
                              `branch_id` long,
                              `resource_id` varchar(256) ,
                              `table_name` varchar(32) ,
                              `pk` varchar(32) ,
                              `gmt_create` datetime ,
                              `gmt_modified` datetime,
                              primary key(`row_key`)
);

1. 踩坑

can not register RM,err:register error,role:RMROLE,err



can not register RM,err:can not connect to services-server.


解决:服务端和客户端的file.conf中的service配置中都改为default.grouplist = "195.168.36.101(外网IP):8091" 。并使用seata服务的版本为seata-server-1.2.0。

2. 流转

最初的order会创建一个订单,然后扣减库存,然后扣减账户,账户扣减完,会回头修改订单的金额和状态,调用成环。

3. 事务回滚

order

    @GlobalTransactional(name = "fsp-create-order",rollbackFor = Exception.class)
    public void create(Order order) {
        LOGGER.info("------->交易开始");
        //本地方法
        orderDao.create(order);

        //远程方法 扣减库存
        storageApi.decrease(order.getProductId(),order.getCount());

        //远程方法 扣减账户余额

        LOGGER.info("------->扣减账户开始order中");
        accountApi.decrease(order.getUserId(),order.getMoney());
        LOGGER.info("------->扣减账户结束order中");

        LOGGER.info("------->交易结束");
    }

事务回滚日志


account

    @Override
    public void decrease(Long userId, BigDecimal money) {
        LOGGER.info("------->扣减账户开始account中");
        //模拟超时异常,全局事务回滚
        try {
            Thread.sleep(30*1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        accountDao.decrease(userId,money);
        LOGGER.info("------->扣减账户结束account中");

        //修改订单状态,此调用会导致调用成环
        LOGGER.info("修改订单状态开始");
        String mes = orderApi.update(userId, money.multiply(new BigDecimal("0.09")),0);
        LOGGER.info("修改订单状态结束:{}",mes);
    }

storage

  1. 事务提交
    注释account中的模拟异常。提交成功



    执行结果

相关文章

网友评论

    本文标题:分布式事务-seata实践

    本文链接:https://www.haomeiwen.com/subject/uihszhtx.html