SpringCloud14-Seata

SpringCloud14-Seata

1.Seata

  1. 一个功能或者业务的实现需要操作多个微服务,而一些更新的操作需要保证全局事务。
  2. Seata是一款开源的分布式事务解决方案,致力于在微服务架构下提供高性能和简单易用的分布式事务服务。
  3. Seata官网地址。http://seata.io/zh-cn/
  4. Seata下载地址。https://github.com/seata/seata/releases,生成环境选择-GA,表示当前最新的稳定的版本。

2.Seata三大核心概念

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

3.Seata下载、安装和运行

  1. Seata下载地址。https://github.com/seata/seata/releases
  2. 修改conf/file.conf文件。修改store模块,使用数据库存储事务信息,mode = "db",然后修改数据库的连接信息:username和password。
  3. 修改config/registe.conf文件。修改register和config模块,type = "nacos" ,然后修改Nacos的配置信息(Nacos配置信息不符合要求在进行修改)。
  4. 创建seata数据库。并在数据库中创建四张表:global_table、branch_table、lock_table、distributed_lock。
-- -------------------------------- The script used when storeMode is 'db' --------------------------------
-- the table to store GlobalSession data
CREATE TABLE IF NOT EXISTS `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`)
) ENGINE = InnoDB
  DEFAULT CHARSET = utf8;

-- the table to store BranchSession data
CREATE TABLE IF NOT EXISTS `branch_table`
(
    `branch_id`         BIGINT       NOT NULL,
    `xid`               VARCHAR(128) NOT NULL,
    `transaction_id`    BIGINT,
    `resource_group_id` VARCHAR(32),
    `resource_id`       VARCHAR(256),
    `branch_type`       VARCHAR(8),
    `status`            TINYINT,
    `client_id`         VARCHAR(64),
    `application_data`  VARCHAR(2000),
    `gmt_create`        DATETIME(6),
    `gmt_modified`      DATETIME(6),
    PRIMARY KEY (`branch_id`),
    KEY `idx_xid` (`xid`)
) ENGINE = InnoDB
  DEFAULT CHARSET = utf8;

-- the table to store lock data
CREATE TABLE IF NOT EXISTS `lock_table`
(
    `row_key`        VARCHAR(128) NOT NULL,
    `xid`            VARCHAR(128),
    `transaction_id` BIGINT,
    `branch_id`      BIGINT       NOT NULL,
    `resource_id`    VARCHAR(256),
    `table_name`     VARCHAR(32),
    `pk`             VARCHAR(36),
    `gmt_create`     DATETIME,
    `gmt_modified`   DATETIME,
    PRIMARY KEY (`row_key`),
    KEY `idx_branch_id` (`branch_id`)
) ENGINE = InnoDB
  DEFAULT CHARSET = utf8;

CREATE TABLE IF NOT EXISTS `distributed_lock`
(
    `lock_key`       CHAR(20) NOT NULL,
    `lock_value`     VARCHAR(20) NOT NULL,
    `expire`         BIGINT,
    primary key (`lock_key`)
) ENGINE = InnoDB
  DEFAULT CHARSET = utf8mb4;

INSERT INTO `distributed_lock` (lock_key, lock_value, expire) VALUES ('AsyncCommitting', ' ', 0);
INSERT INTO `distributed_lock` (lock_key, lock_value, expire) VALUES ('RetryCommitting', ' ', 0);
INSERT INTO `distributed_lock` (lock_key, lock_value, expire) VALUES ('RetryRollbacking', ' ', 0);
INSERT INTO `distributed_lock` (lock_key, lock_value, expire) VALUES ('TxTimeoutCheck', ' ', 0);
  1. 运行。双击bin/seata-server.bat运行。

  2. 创建具体的业务数据库,然后再每个业务数据库中创建undo_log表,记录用户事务的回滚信息。

-- for AT mode you must to init this sql for you business database. the seata server not need it.
CREATE TABLE IF NOT EXISTS `undo_log`
(
    `branch_id`     BIGINT       NOT NULL COMMENT 'branch transaction id',
    `xid`           VARCHAR(128) NOT NULL COMMENT 'global transaction id',
    `context`       VARCHAR(128) NOT NULL COMMENT 'undo_log context,such as serialization',
    `rollback_info` LONGBLOB     NOT NULL COMMENT 'rollback info',
    `log_status`    INT(11)      NOT NULL COMMENT '0:normal status,1:defense status',
    `log_created`   DATETIME(6)  NOT NULL COMMENT 'create datetime',
    `log_modified`  DATETIME(6)  NOT NULL COMMENT 'modify datetime',
    UNIQUE KEY `ux_undo_log` (`xid`, `branch_id`)
) ENGINE = InnoDB
  AUTO_INCREMENT = 1
  DEFAULT CHARSET = utf8 COMMENT ='AT transaction mode undo table';

4.创建Seata客户端微服务cloud-nacos-seata-order-service2001

  1. pom.xml
<dependencies>
    <!--nacos-->
    <dependency>
        <groupId>com.alibaba.cloud</groupId>
        <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
    </dependency>
    <!--seata-->
    <dependency>
        <groupId>com.alibaba.cloud</groupId>
        <artifactId>spring-cloud-starter-alibaba-seata</artifactId>
         <!-- 先启动当前微服务,观察是否报错。如果出错依赖的问题,在进行exclusions过滤。本次使用的seata版本需要过滤 -seata-spring-boot-starter,但是不需要添加seata-all->
        <!--<exclusions>
                <exclusion>
                    <groupId>io.seata</groupId>
                    <artifactId>seata-spring-boot-starter</artifactId>
                </exclusion>
            </exclusions>-->
    </dependency>

    <!-- 先启动当前微服务,观察是否报错。如果出错依赖的问题,在进行exclusions过滤。本次使用的seata版本需要过滤 -seata-spring-boot-starter,但是不需要添加seata-all->
    <!-- 当seata-all版本和seata-service版本不一致时,
        先移除spring-cloud-starter-alibaba-seata中的seata-all,然后引入于seata-service一致的版本 -->
    <!--<dependency>
            <groupId>io.seata</groupId>
            <artifactId>seata-all</artifactId>
            <version>1.4.2</version>
        </dependency>-->

    <!--openfeign-->
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-openfeign</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-loadbalancer</artifactId>
    </dependency>
</dependencies>
  1. yml
server:
  port: 2001

spring:
  application:
    name: cloud-nacos-seata-order-service
  cloud:
    nacos:
      discovery:
        server-addr: http://127.0.0.1:8848
    alibaba:
      seata:
        tx-service-group: fsp_tx_group
  datasource:
    driver-class-name: com.mysql.jdbc.Driver
    url: jdbc:mysql://127.0.0.1:3306/seata_order
    username: root
    password: 123456
    type: com.alibaba.druid.pool.DruidDataSource

mybatis:
  mapper-locations: classpath:mapper/*.xml
  configuration:
    # 驼峰命名 user_id -> userId
    map-underscore-to-camel-case: true
  1. 在/resources下添加file.conf。
transport {
  # tcp udt unix-domain-socket
  type = "TCP"
  #NIO NATIVE
  server = "NIO"
  #enable heartbeat
  heartbeat = true
  #thread factory for netty
  thread-factory {
    boss-thread-prefix = "NettyBoss"
    worker-thread-prefix = "NettyServerNIOWorker"
    server-executor-thread-prefix = "NettyServerBizHandler"
    share-boss-worker = false
    client-selector-thread-prefix = "NettyClientSelector"
    client-selector-thread-size = 1
    client-worker-thread-prefix = "NettyClientWorkerThread"
    # netty boss thread size,will not be used for UDT
    boss-thread-size = 1
    #auto default pin or 8
    worker-thread-size = 8
  }
  shutdown {
    # when destroy server, wait seconds
    wait = 3
  }
  serialization = "seata"
  compressor = "none"
}

service {

  # 需要注意 1.0后使用 vgroupMapping,而不是 vgroup_mapping。
  vgroupMapping.fsp_tx_group = "default" #修改自定义事务组名称

  default.grouplist = "127.0.0.1:8091"
  enableDegrade = false
  disable = false
  max.commit.retry.timeout = "-1"
  max.rollback.retry.timeout = "-1"
  disableGlobalTransaction = false
}


client {
  async.commit.buffer.limit = 10000
  lock {
    retry.internal = 10
    retry.times = 30
  }
  report.retry.count = 5
  tm.commit.retry.count = 1
  tm.rollback.retry.count = 1
}

## transaction log store
store {
  ## store mode: file、db
  mode = "db"

  ## file store
  file {
    dir = "sessionStore"

    # branch session size , if exceeded first try compress lockkey, still exceeded throws exceptions
    max-branch-session-size = 16384
    # globe session size , if exceeded throws exceptions
    max-global-session-size = 512
    # file buffer size , if exceeded allocate new buffer
    file-write-buffer-cache-size = 16384
    # when recover batch read size
    session.reload.read_size = 100
    # async, sync
    flush-disk-mode = async
  }

  ## database store
  db {
    ## the implement of javax.sql.DataSource, such as DruidDataSource(druid)/BasicDataSource(dbcp) etc.
    datasource = "dbcp"
    ## mysql/oracle/h2/oceanbase etc.
    db-type = "mysql"
    driver-class-name = "com.mysql.jdbc.Driver"
    url = "jdbc:mysql://127.0.0.1:3306/seata"
    user = "root"
    password = "123456"
    min-conn = 1
    max-conn = 3
    global.table = "global_table"
    branch.table = "branch_table"
    lock-table = "lock_table"
    query-limit = 100
  }
}
lock {
  ## the lock store mode: local、remote
  mode = "remote"

  local {
    ## store locks in user's database
  }

  remote {
    ## store locks in the seata's server
  }
}
recovery {
  #schedule committing retry period in milliseconds
  committing-retry-period = 1000
  #schedule asyn committing retry period in milliseconds
  asyn-committing-retry-period = 1000
  #schedule rollbacking retry period in milliseconds
  rollbacking-retry-period = 1000
  #schedule timeout retry period in milliseconds
  timeout-retry-period = 1000
}

transaction {
  undo.data.validation = true
  undo.log.serialization = "jackson"
  undo.log.save.days = 7
  #schedule delete expired undo_log in milliseconds
  undo.log.delete.period = 86400000
  undo.log.table = "undo_log"
}

## metrics settings
metrics {
  enabled = false
  registry-type = "compact"
  # multi exporters use comma divided
  exporter-list = "prometheus"
  exporter-prometheus-port = 9898
}

support {
  ## spring
  spring {
    # auto proxy the DataSource bean
    datasource.autoproxy = false
  }
}
  1. 在/resources/下添加register.conf。
registry {
  # file 、nacos 、eureka、redis、zk、consul、etcd3、sofa
  type = "nacos"

  nacos {
    serverAddr = "127.0.0.1:8848"
    namespace = ""
    cluster = "default"
  }
  eureka {
    serviceUrl = "http://localhost:8761/eureka"
    application = "default"
    weight = "1"
  }
  redis {
    serverAddr = "localhost:6379"
    db = "0"
  }
  zk {
    cluster = "default"
    serverAddr = "127.0.0.1:2181"
    session.timeout = 6000
    connect.timeout = 2000
  }
  consul {
    cluster = "default"
    serverAddr = "127.0.0.1:8500"
  }
  etcd3 {
    cluster = "default"
    serverAddr = "http://localhost:2379"
  }
  sofa {
    serverAddr = "127.0.0.1:9603"
    application = "default"
    region = "DEFAULT_ZONE"
    datacenter = "DefaultDataCenter"
    cluster = "default"
    group = "SEATA_GROUP"
    addressWaitTime = "3000"
  }
  file {
    name = "file.conf"
  }
}

config {
  # file、nacos 、apollo、zk、consul、etcd3
  type = "nacos"

  nacos {
    serverAddr = "127.0.0.1:8848"
    namespace = ""
  }
  consul {
    serverAddr = "127.0.0.1:8500"
  }
  apollo {
    app.id = "seata-server"
    apollo.meta = "http://192.168.1.204:8801"
  }
  zk {
    serverAddr = "127.0.0.1:2181"
    session.timeout = 6000
    connect.timeout = 2000
  }
  etcd3 {
    serverAddr = "http://localhost:2379"
  }
  file {
    name = "file.conf"
  }
}
  1. Main
@EnableFeignClients
@EnableDiscoveryClient
// 使用Seata对数据源进行代理,所以这里取消数据源的自动创建
@SpringBootApplication(exclude = {
        DataSourceAutoConfiguration.class
})
public class CloudNacosSeataOrderService2001Main {

    public static void main(String[] args) {
        SpringApplication.run(CloudNacosSeataOrderService2001Main.class, args);
    }
}
  1. config配置类
@Configuration
@MapperScan({"com.my.springcloud.dao"})
public class MyBatisConfig {
}

@Configuration
public class DataSourceProxyConfig {

    @Value("${mybatis.mapper-locations}")
    private String mapperLocations;

    @Bean
    @ConfigurationProperties(prefix = "spring.datasource")
    public DataSource druidDataSource(){
        return new DruidDataSource();
    }

    @Bean
    public DataSourceProxy dataSourceProxy(DataSource dataSource) {
        return new DataSourceProxy(dataSource);
    }

    @Bean
    public SqlSessionFactory sqlSessionFactoryBean(DataSourceProxy dataSourceProxy) throws Exception {
        SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();
        sqlSessionFactoryBean.setDataSource(dataSourceProxy);
        sqlSessionFactoryBean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources(mapperLocations));
        sqlSessionFactoryBean.setTransactionFactory(new SpringManagedTransactionFactory());
        return sqlSessionFactoryBean.getObject();
    }
}
  1. controller
@RestController
public class OrderController {

    @Resource
    private IOrderService orderService;

    @GetMapping("/order/create")
    public CommonResult<?> create(Order order) {
        orderService.createOrder(order);

        return new CommonResult<>(200, "创建订单成功", null);
    }
}
  1. service
public interface IOrderService {

    CommonResult<?> createOrder(Order order);
}

@Slf4j
@Service
public class OrderServiceImpl implements IOrderService  {

    @Resource
    private OrderDao orderDao;
    @Resource
    private IStoreService iStoreService;
    @Resource
    private IAccountService iAccountService;

    /**
     * name = "fsp-create-order"可以随意,但是不能冲突,全局唯一。
     * @GlobalTransactional,全局事务。
     * @param order
     * @return
     */
    @Override
    @GlobalTransactional(name = "fsp-create-order",rollbackFor = Exception.class)
    public CommonResult<?> createOrder(Order order) {
        log.info("==========> 创建订单 start");
        int update = orderDao.createOrder(order);

        log.info("----------> 减少库存 - start");
        iStoreService.decrementStore(order.getProductId(), order.getCount());
        log.info("----------> 减少库存 - end");

        log.info(">>>>>>>>>>> 在用户号账号扣除金额 start");
        iAccountService.decrementStore(order.getUserId(), order.getMoney());
        log.info(">>>>>>>>>>> 在用户号账号扣除金额 end");


        log.info("||||||||||| 都操作成功,修改账单状态 start");
        orderDao.updateOrderStatus(order.getUserId(), 0);
        log.info("||||||||||| 都操作成功,修改账单状态 end");

        log.info("==========> 创建订单 end");
        return null;
    }
}
  1. 创建微服务cloud-nacos-seata-storage-service2002和cloud-nacos-seata-account-service2003,和2001的配置保持一致。

5.Seata执行流程

  1. TM(TM,可以理解为有@GlobalTransactional注解的方法)开启分布式事务(TM向TC注册全局事务记录)。
  2. RM向TC汇报资源准备状态。按业务场景,编排数据库、服务等事务内资源(RM向TC汇报资源准备状态) 。
  3. TM结束分布式事务,事务一阶段结束(TM通知TC提交/回滚分布式事务) 。
  4. TC汇总事务信息,决定分布式事务是提交还是回滚。
  5. TC通知所有RM提交/回滚资源,事务二阶段结束。

6.Seate的一阶段提交和二阶段提交、回滚

  1. 一阶段,Seata会拦截“业务SQL”。
    1. 解析SQL语义,找到“业务SQL" 要更新的业务数据,在业务数据被更新前,将其保存成"before image”。
    2. 然后执行“业务SQL" 更新业务数据。
    3. 在业务数据更新之后,其保存成"after image”,最后生成行锁。
    4. 以上操作全部在一个数据库事务内完成, 这样保证了一阶段操作的原子性。
    5. 通俗说,就是通过要执行SQL的where条件找到该行数据执行前的状态,得到前镜像;然后执行该SQL;执行完成通过前镜像注解定位数据,得到后镜像。
    6. 然后将前镜像和后镜像 保存到undo_log表中的rollback_info字段,保存为json数据,其中提交前json数据的key为beforeImage;后镜像数据为afterImage。
  2. 二阶段提交。二阶段如果顺利提交的话,因为"业务SQL"在一阶段已经提交至数据库,所以Seata框架只需将一阶段保存的快照数据和行锁删掉,完成数据清理即可。
    1. 收到 TC 的分支提交请求,把请求放入一个异步任务的队列中,马上返回提交成功的结果给TC。
    2. 异步任务阶段的分支提交请求将异步和批量地删除相应UNDO LOG记录。
  3. 二阶段回滚。收到 TC 的分支回滚请求,开启一个本地事务,执行如下操作。
    1. 通过XID和Branch ID查找到相应的业务类数据库UNDO LOG表中的记录。
    2. 数据校验:拿 UNDO LOG 中的后镜与当前数据进行比较,如果有不同,说明数据被当前全局事务之外的动作做了修改,这种情况,需要根据配置策略来做处理,详细的说明在另外的文档中介绍。
    3. 根据 UNDO LOG中的前镜像和业务SQL的相关信息生成并执行回滚的语句。
    4. 提交本地事务,并把本地事务的执行结果(即分支事务回滚的结果)上报给 TC。
posted @ 2021-10-07 20:09  行稳致远方  阅读(175)  评论(0)    收藏  举报