分布式事务框架学习实践心得

    最近研究了下我们这边使用分布式事务的场景,顺便学习了下分布式框架的源码。

    场景:

      积分兑换优惠券,两步操作 第一步调用积分服务扣积分,第二步发放优惠券。

    分布式框架支持场景

       1. 回补操作,简化的二阶段模型,一阶段做真实的扣除,一阶段成功则二阶段不做处理,一阶段失败则做回补操作。比如:扣积分成功 ,发放优惠券失败,那么则调用回补积分的接口进行补偿。

       2. 二阶段,try conform cancel  一阶段进行冻结,二阶段真实的扣除或者取消。

    分布式事务框架调用流程
    
     分布式事务框架学习实践心得
 
   

    流程说明:

      1. 状态是分为业务状态和事务状态。状态流程: 业务状态init, 业务状态成功or失败。初始化事务状态process_wating,  二阶段处理事务状态processing,每个action做二阶段处理同时更新每个action的status,根据每个action的综合结果将整个事务的处理结果置为成功或者失败。

      2. 最后几步只会执行其中一步,如果业务状态成功且是回补操作则啥也不用干,如果业务状态失败且是回补操作则执行回补参与者。如果是二阶段则根据业务状态做cancel或者confirm参与者和发起者。

     db脚本:

   

tx_info | CREATE TABLE `tx_info` (
  `id` bigint(64) NOT NULL COMMENT 'id',
  `tx_id` varchar(256) NOT NULL COMMENT '事务id',
  `business_type` varchar(32) NOT NULL COMMENT '业务类型',
  `business_id` varchar(128) NOT NULL COMMENT '业务id',
  `status` varchar(32) NOT NULL COMMENT '事务状态',
  `retry_count` int(11) DEFAULT NULL COMMENT '重试次数,针对部分状态才有意义',
  `check_back_info` text COMMENT '事务回查信息',
  `participants_info` text COMMENT '参与者基本信息',
  `ext_properties` text COMMENT '额外属性字段',
  `version` int(11) DEFAULT NULL COMMENT '版本控制',
  `create_time` datetime NOT NULL COMMENT '创建时间',
  `db_update_time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  `biz_status` varchar(32) NOT NULL DEFAULT 'UNKOWN' COMMENT '业务执行状态',
  PRIMARY KEY (`id`,`create_time`),
  KEY `idx_tx_id` (`tx_id`(255)),
  KEY `idx_business_id` (`business_id`),
  KEY `idx_create_time` (`create_time`),
  KEY `idx_update_time` (`db_update_time`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='事务信息表'
/*!50100 PARTITION BY RANGE (to_days(create_time))
(PARTITION p20161101 VALUES LESS THAN (736634) ENGINE = InnoDB,
 PARTITION p20161201 VALUES LESS THAN (736664) ENGINE = InnoDB,
 PARTITION p20170101 VALUES LESS THAN (736695) ENGINE = InnoDB,
 PARTITION p20170201 VALUES LESS THAN (736726) ENGINE = InnoDB,
 PARTITION p20170301 VALUES LESS THAN (736754) ENGINE = InnoDB,
 PARTITION p20170401 VALUES LESS THAN (736785) ENGINE = InnoDB,
 PARTITION p20170501 VALUES LESS THAN (736815) ENGINE = InnoDB,
 PARTITION p20170601 VALUES LESS THAN (736846) ENGINE = InnoDB,
 PARTITION p20170701 VALUES LESS THAN (736876) ENGINE = InnoDB) 


 action_info | CREATE TABLE `action_info` (
  `id` bigint(20) NOT NULL COMMENT '主键id',
  `tx_id` varchar(256) NOT NULL COMMENT '事务id',
  `business_type` varchar(32) NOT NULL COMMENT '业务类型',
  `business_id` varchar(128) NOT NULL COMMENT '业务id',
  `action_type` varchar(32) NOT NULL COMMENT '操作类型',
  `action_name` varchar(64) DEFAULT NULL COMMENT '操作名',
  `status` varchar(32) NOT NULL COMMENT '操作状态',
  `ext_properties` text COMMENT '额外属性字段',
  `version` int(11) NOT NULL COMMENT '版本信息',
  `create_time` datetime NOT NULL COMMENT '创建时间',
  `db_update_time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  PRIMARY KEY (`id`,`create_time`),
  UNIQUE KEY `idx_txid_aname` (`tx_id`(255),`action_name`,`create_time`),
  KEY `idx_business_id` (`business_id`),
  KEY `idx_create_time` (`create_time`),
  KEY `idx_update_time` (`db_update_time`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='参与者操作信息表'
/*!50100 PARTITION BY RANGE (to_days(create_time))
(PARTITION p20161101 VALUES LESS THAN (736634) ENGINE = InnoDB,
 PARTITION p20161201 VALUES LESS THAN (736664) ENGINE = InnoDB,
 PARTITION p20170101 VALUES LESS THAN (736695) ENGINE = InnoDB,
 PARTITION p20170201 VALUES LESS THAN (736726) ENGINE = InnoDB,
 PARTITION p20170301 VALUES LESS THAN (736754) ENGINE = InnoDB,
 PARTITION p20170401 VALUES LESS THAN (736785) ENGINE = InnoDB,
 PARTITION p20170501 VALUES LESS THAN (736815) ENGINE = InnoDB,
 PARTITION p20170601 VALUES LESS THAN (736846) ENGINE = InnoDB,
 PARTITION p20170701 VALUES LESS THAN (736876) ENGINE = InnoDB)

        

     技术原理:

     1. dubbo泛化调用,参照上一篇博客。

   

相关推荐