SpringCloud 微服务实战8 - SpringCloud Alibaba Seata 处理分布式事务

1、分布式事务问题

单机单库没有这个问题。

分布式之后单体应用被拆分成微服务应用,原来的三个模块被拆分成三个独立的应用,分别使用三个独立的数据源。业务操作需要调用三个服务来完成。此时每个微服务内部的数据一致性由本地事务来保证,但是全局的数据一致性问题没有办法保证。

例如:用户购买商品的业务逻辑。整个业务逻辑由3个微服务提供支持:

  • 仓储服务:对给定的商品扣除仓储数量。
  • 订单服务:根据采购需求创建订单。
  • 账户服务:从账户中扣除余额。

一句话总结:一次业务操作需要跨多个数据源或需要跨多个系统进行远程调用,就会产生分布式事务问题。

2、Seata 简介

Seata 是什么?

官网地址:http://seata.io/zh-cn/

Seata 是一款开源的分布式事务解决方案,致力于在微服务架构下提供高性能和简单易用的分布式事务服务。

Seata 是一个典型的分布式事务过程。

分布式事务处理过程的 一ID + 三组件模型

  • Transaction ID  -  XID :全局唯一的事务ID
  • TC(Transaction Coordinator):事务协调者,维护全局和分支事务的状态,驱动全局事务提交或回滚。
  • TM(Transaction Manager):事务管理器,定义全局事务的范围(开始全局事务、提交或回滚全局事务)。
  • RM(Resource Manager):管理分支事务处理的资源,与 TC 交谈以注册分支事务和报告分支事务状态,并驱动分支事务提交或回滚。

处理过程

  • TM 向 TC申请开启一个全局事务,全局事务创建成功并生成一个全局唯一的 XID;
  • XID 在微服务调用链路的上下文中传播。
  • RM 向 TC 注册分支事务,将其纳入 XID 对应全局事务的管辖;
  • TM 向 TC 发起针对 XID 的全局提交或回滚决议。
  • TC 调度 XID 下管辖的全部分支事务完成提交或回滚请求。

 Seata  怎么用?

之前单机的时候,我们使用的是 @Transaction;全局分布式事务,我们主需要一个 @GlobalTransaction 注解在业务方法上。

3、Seata - Server 安装

下载地址:https://github.com/seata/seata/tags ,我下载的是 seata-server-1.4.0.zip

下载完成后,将 seata-server-1.4.0.zip 解压到指定目录,并修改 conf 目录下的 file.conf 配置文件。

修改 file.conf 、registry.conf 配置文件

file.conf :mode、数据库相关配置

 修改 registry.conf :分别修改 registry 和 config 下的内容

nacos-config.sh 脚本准备

可以自己下载,下载地址: https://github.com/seata/seata/blob/develop/script/config-center/nacos/nacos-config.sh 

或者使用下面的内容

#!/bin/sh
# Copyright 1999-2019 Seata.io Group.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at、
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

while getopts ":h:p:g:t:u:w:" opt
do
  case $opt in
  h)
    host=$OPTARG
    ;;
  p)
    port=$OPTARG
    ;;
  g)
    group=$OPTARG
    ;;
  t)
    tenant=$OPTARG
    ;;
  u)
    username=$OPTARG
    ;;
  w)
    password=$OPTARG
    ;;
  ?)
    echo " USAGE OPTION: $0 [-h host] [-p port] [-g group] [-t tenant] [-u username] [-w password] "
    exit 1
    ;;
  esac
done

if [ -z ${host} ]; then
    host=localhost
fi
if [ -z ${port} ]; then
    port=8848
fi
if [ -z ${group} ]; then
    group="SEATA_GROUP"
fi
if [ -z ${tenant} ]; then
    tenant=""
fi
if [ -z ${username} ]; then
    username=""
fi
if [ -z ${password} ]; then
    password=""
fi

nacosAddr=$host:$port
contentType="content-type:application/json;charset=UTF-8"

echo "set nacosAddr=$nacosAddr"
echo "set group=$group"

urlencode() {
  length="${#1}"
  i=0
  while [ $length -gt $i ]; do
    char="${1:$i:1}"
    case $char in
    [a-zA-Z0-9.~_-]) printf $char ;;
    *) printf '%%%02X' "'$char" ;;
    esac
    i=`expr $i + 1`
  done
}

failCount=0
tempLog=$(mktemp -u)
function addConfig() {
  dataId=`urlencode $1`
  content=`urlencode $2`
  curl -X POST -H "${contentType}" "http://$nacosAddr/nacos/v1/cs/configs?dataId=$dataId&group=$group&content=$content&tenant=$tenant&username=$username&password=$password" >"${tempLog}" 2>/dev/null
  if [ -z $(cat "${tempLog}") ]; then
    echo " Please check the cluster status. "
    exit 1
  fi
  if [ "$(cat "${tempLog}")" == "true" ]; then
    echo "Set $1=$2 successfully "
  else
    echo "Set $1=$2 failure "
    failCount=`expr $failCount + 1`
  fi
}

count=0
for line in $(cat $(dirname "$PWD")/config.txt | sed s/[[:space:]]//g); do
    count=`expr $count + 1`
	key=${line%%=*}
    value=${line#*=}
	addConfig "${key}" "${value}"
done

echo "========================================================================="
echo " Complete initialization parameters,  total-count:$count ,  failure-count:$failCount "
echo "========================================================================="

if [ ${failCount} -eq 0 ]; then
	echo " Init nacos config finished, please start seata-server. "
else
	echo " init nacos config fail. "
fi

config.txt  准备

config.txt就是seata各种详细的配置,执行 nacos-config.sh 即可将这些配置导入到nacos,这样就不需要将file.conf和registry.conf放到我们的项目中了,需要什么配置就直接从nacos中读取。

    在conf同级目录,需要config.txt文件,1.4.0版本也没有,获取地址(https://github.com/seata/seata/blob/develop/script/config-center/config.txt)。

修改 config.txt 的数据库相关配置:

这是我修改后的config.txt:

transport.type=TCP
transport.server=NIO
transport.heartbeat=true
transport.enableClientBatchSendRequest=true
transport.threadFactory.bossThreadPrefix=NettyBoss
transport.threadFactory.workerThreadPrefix=NettyServerNIOWorker
transport.threadFactory.serverExecutorThreadPrefix=NettyServerBizHandler
transport.threadFactory.shareBossWorker=false
transport.threadFactory.clientSelectorThreadPrefix=NettyClientSelector
transport.threadFactory.clientSelectorThreadSize=1
transport.threadFactory.clientWorkerThreadPrefix=NettyClientWorkerThread
transport.threadFactory.bossThreadSize=1
transport.threadFactory.workerThreadSize=default
transport.shutdown.wait=3
service.vgroupMapping.fsp_tx_group=default
service.default.grouplist=127.0.0.1:8091
service.enableDegrade=false
service.disableGlobalTransaction=false
client.rm.asyncCommitBufferLimit=10000
client.rm.lock.retryInterval=10
client.rm.lock.retryTimes=30
client.rm.lock.retryPolicyBranchRollbackOnConflict=true
client.rm.reportRetryCount=5
client.rm.tableMetaCheckEnable=false
client.rm.tableMetaCheckerInterval=60000
client.rm.sqlParserType=druid
client.rm.reportSuccessEnable=false
client.rm.sagaBranchRegisterEnable=false
client.rm.tccActionInterceptorOrder=-2147482648
client.tm.commitRetryCount=5
client.tm.rollbackRetryCount=5
client.tm.defaultGlobalTransactionTimeout=60000
client.tm.degradeCheck=false
client.tm.degradeCheckAllowTimes=10
client.tm.degradeCheckPeriod=2000
client.tm.interceptorOrder=-2147482648
store.mode=file
store.lock.mode=file
store.session.mode=file
store.publicKey=
store.file.dir=file_store/data
store.file.maxBranchSessionSize=16384
store.file.maxGlobalSessionSize=512
store.file.fileWriteBufferCacheSize=16384
store.file.flushDiskMode=async
store.file.sessionReloadReadSize=100
store.db.datasource=druid
store.db.dbType=mysql
store.db.driverClassName=com.mysql.cj.jdbc.Driver
store.db.url=jdbc:mysql://127.0.0.1:3306/seata?useUnicode=true&rewriteBatchedStatements=true
store.db.user=root
store.db.password=root
store.db.minConn=5
store.db.maxConn=30
store.db.globalTable=global_table
store.db.branchTable=branch_table
store.db.distributedLockTable=distributed_lock
store.db.queryLimit=100
store.db.lockTable=lock_table
store.db.maxWait=5000
store.redis.mode=single
store.redis.single.host=127.0.0.1
store.redis.single.port=6379
store.redis.sentinel.masterName=
store.redis.sentinel.sentinelHosts=
store.redis.maxConn=10
store.redis.minConn=1
store.redis.maxTotal=100
store.redis.database=0
store.redis.password=
store.redis.queryLimit=100
server.recovery.committingRetryPeriod=1000
server.recovery.asynCommittingRetryPeriod=1000
server.recovery.rollbackingRetryPeriod=1000
server.recovery.timeoutRetryPeriod=1000
server.maxCommitRetryTimeout=-1
server.maxRollbackRetryTimeout=-1
server.rollbackRetryTimeoutUnlockEnable=false
server.distributedLockExpireTime=10000
client.undo.dataValidation=true
client.undo.logSerialization=jackson
client.undo.onlyCareUpdateColumns=true
server.undo.logSaveDays=7
server.undo.logDeletePeriod=86400000
client.undo.logTable=undo_log
client.undo.compress.enable=true
client.undo.compress.type=zip
client.undo.compress.threshold=64k
log.exceptionRate=100
transport.serialization=seata
transport.compressor=none
metrics.enabled=false
metrics.registryType=compact
metrics.exporterList=prometheus
metrics.exporterPrometheusPort=9898

执行导入

导入前要先启动 nacos。

打开git bash,输入如下命令

sh nacos-config.sh -h localhost -p 8848 -t b61c94e1-b56d-4259-9a3b-b2dae2a68c39 -g SEATA_GROUP -u nacos -w nacos

命令解析:

  • -h 指定nacos地址
  • -p 指定nacos的端口
  • -g 指定配置的分组,注意,是配置的分组;
  • -t  指定命名空间id;
  • -u 指定nacos的用户名
  • -w 指定nacos的密码

我这里指定了命名空间如下:

执行完成后,Nacos 配置中增加了如下内容

 seata 数据库

在 seata 数据库中建表

-- 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(128),
  `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(36) ,
  `gmt_create` DATETIME ,
  `gmt_modified` DATETIME,
  PRIMARY KEY(`row_key`)
);

先启动 Nacos,待Nacos启动成功,再启动Seata

进入 Nacos 面板的服务列表可以看到 Seata 已经注册到 Nacos

4、订单/库存/账户业务数据库准备

说明

我们会创建三个微服务,一个订单服务,一个库存服务,一个账户服务。

当用户下单时,会在订单服务中创建一个订单,然后通过远程调用库存服务来扣减下单商品的库存,再通过远程调用账户服务来扣减账户里面的余额,最后在订单服务中修改订单状态为已完成。

该操作跨越三个数据库,有两次远程调用,很明显会有分布式事务问题。

创建业务数据库

seata_order:存储订单的数据库

seata_storage:存储库存的数据库

seata_account:存储账户信息的数据库

分别创建对应业务表

seata_order 库下建 t_order 表

CREATE TABLE t_order
(
   id                   BIGINT NOT NULL AUTO_INCREMENT,
   user_id              BIGINT COMMENT '用户id',
   product_id           BIGINT COMMENT '产品id',
   COUNT                INT COMMENT '数量',
   money                DECIMAL(11,0) COMMENT '金额',
   STATUS               TINYINT COMMENT '订单状态: 0-创建中 1-已完结',
   PRIMARY KEY (id)
)ENGINE=INNODB AUTO_INCREMENT=7 DEFAULT CHARSET=utf8;

seata_storage 库下建 t_storage 表

CREATE TABLE t_storage
(
   id                   BIGINT NOT NULL AUTO_INCREMENT,
   product_id           BIGINT COMMENT '产品id',
   total                INT COMMENT '总库存',
   used                 INT COMMENT '已用库存',
   residue              INT COMMENT '剩余库存',
   PRIMARY KEY (id)
)ENGINE=INNODB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8;

INSERT INTO seata_storage.t_storage(id,product_id,total,used,residue) VALUES (1,1,100,0,100);

seata_account 库下建 t_account 表

CREATE TABLE t_account
(
   id                   BIGINT NOT NULL AUTO_INCREMENT,
   user_id              BIGINT COMMENT '用户id',
   total                DECIMAL(10,0) COMMENT '总额度',
   used                 DECIMAL(10,0) COMMENT '已用余额',
   residue              DECIMAL(10,0) COMMENT '剩余可用额度',
   PRIMARY KEY (id)
)ENGINE=INNODB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8;

INSERT INTO seata_account.t_account(id,user_id,total,used,residue) VALUES (1,1,1000,0,1000);

分别在每个数据库下创建对应的回滚日志表

-- the table to store seata xid data
-- 0.7.0+ add context
-- you must to init this sql for you business databese. the seata server not need it.
-- 此脚本必须初始化在你当前的业务数据库中,用于AT 模式XID记录。与server端无关(注:业务数据库)
-- 注意此处0.3.0+ 增加唯一索引 ux_undo_log
DROP TABLE `undo_log`;
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;

新建完成后如下: 

 

5、订单/库存/账户业务微服务准备

5.1 seata-order-service2001

新建 Module :seata-order-service2001

pom:注意 seata 的版本要与我们实际使用的 seata-server 的版本保持一致。

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <parent>
        <artifactId>cloud-study</artifactId>
        <groupId>com.cloud.study</groupId>
        <version>1.0</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>seata-order-service2001</artifactId>

    <dependencies>
        <!-- alibaba nacos -->
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
        </dependency>
        <!-- alibaba seata,seata 的版本号要与我们使用的一直 -->
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-seata</artifactId>
            <exclusions>
                <exclusion>
                    <groupId>io.seata</groupId>
                    <artifactId>seata-spring-boot-starter</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>io.seata</groupId>
            <artifactId>seata-spring-boot-starter</artifactId>
            <version>1.4.0</version>
        </dependency>
        <!-- openfeign -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-openfeign</artifactId>
        </dependency>
        <!-- SpringBoot 整合 web组件+actuator -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>

        <!-- 数据库相关 -->
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
        </dependency>
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid-spring-boot-starter</artifactId>
        </dependency>
        <!-- mysql-connector-java -->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
        </dependency>
        <!-- jdbc -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-jdbc</artifactId>
        </dependency>
        <!--开启热部署-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
            <optional>true</optional>
        </dependency>
        <!-- lombok -->
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <!-- test -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>
</project>

yaml

server:
  port: 2001

spring:
  application:
    name: seata-order-service
  cloud:
    nacos:
      discovery:
        server-addr: localhost:8848 #nacos 服务注册中心
  datasource:
    driver-class-name: com.mysql.cj.jdbc.Driver
    url: jdbc:mysql://localhost:3306/seata_order?useUnicode=true?characterEncoding=utf-8&serverTimezone=Asia/Shanghai
    username: root
    password: root

feign:
  hystrix:
    enabled: false
logging:
  level:
    io:
      seata: info

mybatis:
  mapper-locations: classpath:mapper/*.xml
  type-aliases-package: com.cloud.study #实体扫描,多个package用逗号或者分号分隔


seata:
  enabled: true #是否开启spring-boot自动装配
  enable-auto-data-source-proxy: true  #是否开启数据源自动代理
  tx-service-group: fsp_tx_group  #事务分组
  registry:
    type: nacos #nacos 作为注册中心
    nacos:
      application: seata-server #默认seata-server,Server和Client端的值需一致
      server-addr: localhost:8848 #nacos 地址
      username: nacos  #nacos 用户名
      password: nacos  #nacos 密码
  config:
    type: nacos #nacos 作为配置中心
    nacos:
      server-addr: localhost:8848  #nacos 地址
      username: nacos #nacos 用户名
      password: nacos #nacos 密码
      group: SEATA_GROUP #nacos 中配置所属分组
      namespace: b61c94e1-b56d-4259-9a3b-b2dae2a68c39 #配置文件所属命名空间
  service:
    vgroup-mapping:
      fsp_tx_group: default  #事务分组fsp_tx_group 的配置
    disable-global-transaction: false  #全局事务开关	默认false。false为开启,true为关闭
  client:
    rm:
      report-success-enable: false  #是否上报一阶段成功

主启动

@SpringBootApplication(exclude={DruidDataSourceAutoConfigure.class})
@EnableFeignClients
@EnableDiscoveryClient
public class SeataOrderMain2001 {
    public static void main(String[] args) {
        SpringApplication.run(SeataOrderMain2001.class,args);
    }
}

config

@Configuration
@MapperScan("com.cloud.study.dao")
public class MybatisConfig {
}

domain

@Data
@AllArgsConstructor
@NoArgsConstructor
public class Order {
    private Long id;
    private Long userId;
    private Long productId;
    private Integer count;
    private BigDecimal money;

    /** 订单状态 0-创建中 1-已完结 */
    private Integer status;
}

@Configuration
@MapperScan("com.cloud.study.dao")
public class MybatisConfig {
}

 dao

@Mapper
public interface OrderDao {
    //创建订单
    void create(Order order);

    //修改订单状态:0->1
    void update (@Param("userId") Long userId, @Param("status") Integer status);
}

resource/mapper/OrderMapper.xml 

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">

<mapper namespace="com.cloud.study.dao.OrderDao">
    <resultMap id="BaseResultMap" type="com.cloud.study.domain.Order">
        <id column="id" property="id" jdbcType="BIGINT"/>
        <result column="user_id" property="userId" jdbcType="BIGINT"/>
        <result column="product_id" property="productId" jdbcType="BIGINT"/>
        <result column="count" property="count" jdbcType="INTEGER"/>
        <result column="money" property="money" jdbcType="DECIMAL"/>
        <result column="status" property="status" jdbcType="INTEGER"/>
    </resultMap>

    <insert id="create">
        insert into t_order (id,user_id,product_id,count,money,status) values
            (null, #{userId},#{productId},#{count},#{money},0);
    </insert>

    <update id="update">
        update t_order set status=1 where user_id=#{userId} and status=#{status}
    </update>
</mapper>

service

public interface OrderService {
    //创建订单
    void create(Order order);
}


@Service
@Slf4j
public class OrderServiceImpl implements OrderService {
    @Resource
    private OrderDao orderDao;
    @Resource
    private StorageService storageService;
    @Resource
    private AccountService accountService;

    @Override
    public void create(Order order) {
        log.info("----> 开始新建订单");
        orderDao.create(order);

        log.info("----> 订单微服务开始调用库存,做扣减");
        storageService.decrease(order.getProductId(), order.getCount());
        log.info("----> 订单微服务开始调用库存,做扣减 end");

        log.info("----> 订单微服务开始调用账户,做扣减");
        accountService.decrease(order.getUserId(), order.getMoney());
        log.info("----> 订单微服务开始调用账户,做扣减 end");

        log.info("----> 修改订单的状态开始");
        orderDao.update(order.getUserId(), 0);
        log.info("----> 修改订单的状态结束");

        log.info("----> 下订单结束了");
    }
}
@FeignClient(value = "seata-storage-service")
public interface StorageService {
    @PostMapping(value = "/storage/decrease")
    R decrease(@RequestParam("productId") Long productId, @RequestParam("count") Integer count);
}
@FeignClient(value = "seata-account-service")
public interface AccountService {

    @PostMapping(value = "/account/decrease")
    R decrease(@RequestParam("userId") Long userId, @RequestParam("money") BigDecimal money);
}

controller

@RestController
public class OrderController {
    @Resource
    private OrderService orderService;

    @GetMapping("/order/create")
    public R create(Order order){
        orderService.create(order);
        return new R(200,"订单创建成功");
    }
}

5.2 storage-module

pom 文件与 seata-order-service2001 一致。

yaml 修改如下图

主启动基本与  seata-order-service2001 一致,只是类名不同。

配置类与seata-order-service2001 一致。

 dao

@Mapper
public interface StorageDao {
    /**
     * 扣减库存
     */
    void decrease(@Param("productId") Long productId, @Param("count") Integer count);
}

resource/mapper/StorageMapper.xml 

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">

<mapper namespace="com.cloud.study.dao.StorageDao">
    <resultMap id="BaseResultMap" type="com.cloud.study.domain.Storage">
        <id column="id" property="id" jdbcType="BIGINT"/>
        <result column="product_id" property="productId" jdbcType="BIGINT"/>
        <result column="total" property="total" jdbcType="INTEGER"/>
        <result column="used" property="used" jdbcType="DECIMAL"/>
        <result column="residue" property="residue" jdbcType="INTEGER"/>
    </resultMap>

    <update id="decrease">
        update t_storage set used=used+#{count}, residue = residue - #{count}
        where product_id=#{productId}
    </update>
</mapper>

service

public interface StorageService {
    /**
     * 扣减库存
     */
    void decrease(Long productId, Integer count);
}

@Slf4j
@Service
public class StorageServiceImpl implements StorageService {
    @Resource
    private StorageDao storageDao;

    /**
     * 扣减库存
     */
    @Override
    public void decrease(Long productId, Integer count) {
        log.info("----> storage-service 中扣减库存开始");
        storageDao.decrease(productId,count);
        log.info("----> storage-service 中扣减库存结束");
    }
}

controller

@RestController
public class StorageController {
    @Resource
    private StorageService storageService;

    /**
     * 扣减库存
     */
    @PostMapping("/storage/decrease")
    public R decrease(Long productId, Integer count) {
        storageService.decrease(productId, count);
        return new R (200,"扣减库存成功");
    }
}

5.3 account-module

pom 文件与 seata-order-service2001 一致。

yaml 修改如下图

主启动基本与  seata-order-service2001 一致,只是类名不同。

配置类与seata-order-service2001 一致。

 dao

@Mapper
public interface AccountDao {
    /**
     * 扣减账户
     */
    void decrease(@Param("userId") Long userId, @Param("money") BigDecimal money);
}

resource/mapper/AccountMapper.xml 

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">

<mapper namespace="com.cloud.study.dao.AccountDao">
    <resultMap id="BaseResultMap" type="com.cloud.study.domain.Account">
        <id column="id" property="id" jdbcType="BIGINT"/>
        <result column="user_id" property="userId" jdbcType="BIGINT"/>
        <result column="total" property="total" jdbcType="INTEGER"/>
        <result column="used" property="used" jdbcType="DECIMAL"/>
        <result column="residue" property="residue" jdbcType="INTEGER"/>
    </resultMap>

    <update id="decrease">
        update t_account set used=used+#{money}, residue = residue - #{money}
        where user_id=#{userId}
    </update>
</mapper>

service

public interface AccountService {

    /**
     * 扣减账户
     */
    void decrease(Long userId, BigDecimal money);
}

@Service
@Slf4j
public class AccountServiceImpl implements AccountService {
    @Resource
    private AccountDao accountDao;

    @Override
    public void decrease(Long userId, BigDecimal money) {
        log.info("----> account-service 中扣减账户开始");
        accountDao.decrease(userId,money);
        log.info("----> account-service 中扣减账户结束");
    }
}

controller

@RestController
public class AccountController {
    @Resource
    private AccountService accountService;

    @PostMapping("/account/decrease")
    public R decrease(Long userId, BigDecimal money){
        accountService.decrease(userId, money);
        return new R(200,"扣减账户成功");
    }
}

6、测试

测试前个表数据如下

正常测试

浏览器输入:

http://localhost:2001/order/create?userId=1&productId=1&count=10&money=100

各数据库表如下: 

异常测试(不加 @GlobalTransaction) 

默认OpenFeign的超时时间是1秒,我们给 账户微服务的扣减操作添加 5秒延迟,就会出现异常。

重启 2003 微服务,输入 

http://localhost:2001/order/create?userId=1&productId=1&count=10&money=100

此时页面出现报错: 

各数据库表如下: 

说明,订单表的更新操作没有执行,但是订单创建、库存扣减、账户扣减操作都已完成,这样的话,在生产环境中时有问题的,因为他们是一个整体的事务。 

异常测试(加 @GlobalTransaction

账户微服务的扣减操作添加 5秒延迟保留,并在我们的订单微服务的 service 中加入@GlobalTransaction

 重启 2001 微服务,输入 

http://localhost:2001/order/create?userId=1&productId=1&count=10&money=100

此时整个逻辑就正常了,当账户微服务出现异常的时候,会将订单表和库存表 进行回滚,在控制台日志中可以看到:

 而过了5秒后,账户微服务则会在执行语句时自己抛出异常,组织数据更新操作。

7、Seata 之原理简介

7.1 Seata

2019年1月份蚂蚁金服和阿里巴巴共同开源分布式事务解决方案。

Simple Extensible Autonomous Transaction Architecture,简单可扩展自治事务框架。

推荐使用 1.0 以后的版本,因为 0.9 不支持集群。

7.2 再看 TC/TM/RM 三大组件

分布式事务的执行流程

  • TM开启分布式事务(TM向TC注册全局事务记录)
  • 按业务场景,编排数据库、服务等事务资源(RM 向 TC 汇报资源准备状态)
  • TM 结束分布式事务,事务一阶段结束(TM 通知 TC 提交/回滚分布式事务)
  • TC 汇总事务信息,决定分布式事务提交还是回滚
  • TC 通知所有 RM 提交/回滚 资源,事务二阶段结束 

7.3 AT 模式如何做到对业务的无侵入

Seta 有以下几种模式,默认使用 AT模式:

  • AT模式:提供无侵入自动补偿的事务模式,目前已支持 Mysql、Oracle、PostgreSQL 和 TiDB 的 AT 模式,H2 开发中。
  • TCC模式:支持 TCC 模式并可与 AT 混用,灵活度更高。
  • SAGA模式:为长事务提供有效的解决方案,在 Saga 模式中,业务流程每个参与者都提交本地事务,当出现一个参与者失败则补偿前面已经成功的参与者,一阶段正向服务和二阶段补偿服务都由业务开发实现。
  • XA模式:支持已实现 XA 接口的数据库的 XA 模式。

AT 模式是什么

前提:基于支持本地 ACID 事务的关系型数据库;Java应用,通过 JDBC 访问数据库。

整体机制:

  • 一阶段:业务数据库和回滚日志记录在同一个本地事务中提交,释放本地锁和连接资源。
  • 二阶段:提交异步化,非常快速的完成;回滚通过一阶段的回滚日志进行反向补偿。

一阶段加载

在一阶段,Seata 会拦截“业务SQL”:

  • 解析 SQL 语义,找到 “业务SQL”要更新的业务数据,在业务数据被更新前,将其保存成“before image”(执行前快照)。
  • 执行 “业务SQL”更新业务数据,在业务数据更新之后,
  • 将其保存成 “after image”,最后成产行锁(执行后快照)。

以上全部操作在一个数据库事务内完成,这样就保证了一阶段操作的完整性。

二阶段提交

二阶段如果是顺利提交的话,因为“业务SQL”在一阶段已经提交至数据库,所以 Seata 只需要将一阶段保存的快照数据和行锁删掉,完成数据清理即可

二阶段回滚

二阶段如果是回滚的话,Seata 就需要回滚一阶段已经执行的 “业务SQL”,还原业务数据。回滚方式便是用 “before image”还原业务数据,但在还原之前要首先校验脏写,对比“数据库当前业务数据”和“after image”,如果两份数据完全一直就说明没有脏写,可以还原业务数据,如果不一致就说明有脏写,出现脏写就需要人工处理。

回滚后,自动清理 快照数据和行锁。

7.4 debug

我们在账户微服务下打断点:

debug进入后查看,在三个业务数据库下的 undo_log 下出现记录,下图事务截取的一个

 在 rollback 字段的 json 字符串中保存了“before image”(保存了update前各个字段的值)和“after image”(保存了update后各个字段的值)。

放行 Debug,发现 undo_log 中的记录被清楚了。

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值