Spring Boot 集成 Seata 解決分佈式事務問題

seata 簡介

Seata 是 阿里巴巴2019年開源的分佈式事務解決方案,致力於在微服務架構下提供高性能和簡單易用的分佈式事務服務。在 Seata 開源之前,Seata 對應的內部版本在阿里內部一直扮演著分佈式一致性中間件的角色,幫助阿里度過歷年的雙11,對各業務進行了有力的支撐。經過多年沉澱與積累,2019.1 Seata 正式宣佈對外開源 。目前 Seata 1.0 已經 GA。

微服務中的分佈式事務問題

讓我們想象一下傳統的單片應用程序,它的業務由3個模塊組成,他們使用單個本地數據源。自然,本地事務將保證數據的一致性。

Spring Boot 集成 Seata 解決分佈式事務問題

微服務架構已發生了變化。上面提到的3個模塊被設計為3種服務。本地事務自然可以保證每個服務中的數據一致性。但是整個業務邏輯範圍如何?

Spring Boot 集成 Seata 解決分佈式事務問題

Seata怎麼辦?

Spring Boot 集成 Seata 解決分佈式事務問題

我們說,分佈式事務是由一批分支事務組成的全局事務,通常分支事務只是本地事務。

Spring Boot 集成 Seata 解決分佈式事務問題

Seata有3個基本組成部分:

  • 事務協調器(TC):維護全局事務和分支事務的狀態,驅動全局提交或回滾。
  • 事務管理器TM:定義全局事務的範圍:開始全局事務,提交或回滾全局事務。
  • 資源管理器(RM):管理正在處理的分支事務的資源,與TC對話以註冊分支事務並報告分支事務的狀態,並驅動分支事務的提交或回滾。
Spring Boot 集成 Seata 解決分佈式事務問題

Seata管理的分佈式事務的典型生命週期:

  1. TM要求TC開始一項新的全局事務。TC生成代表全局事務的XID。
  2. XID通過微服務的調用鏈傳播。
  3. RM將本地事務註冊為XID到TC的相應全局事務的分支。
  4. TM要求TC提交或回退相應的XID全局事務。
  5. TC驅動XID的相應全局事務下的所有分支事務以完成分支提交或回滾。
Spring Boot 集成 Seata 解決分佈式事務問題

快速開始

用例

用戶購買商品的業務邏輯。整個業務邏輯由3個微服務提供支持:

  • 倉儲服務:對給定的商品扣除倉儲數量。
  • 訂單服務:根據採購需求創建訂單。
  • 帳戶服務:從用戶帳戶中扣除餘額。

環境準備

步驟 1:建立數據庫

<code># db_seataDROP SCHEMA IF EXISTS db_seata;CREATE SCHEMA db_seata;USE db_seata;# AccountCREATE TABLE `account_tbl` (  `id` INT(11) NOT NULL AUTO_INCREMENT,  `user_id` VARCHAR(255) DEFAULT NULL,  `money` INT(11) DEFAULT 0,  PRIMARY KEY (`id`)) ENGINE = InnoDB DEFAULT CHARSET = utf8;INSERT INTO account_tbl (id, user_id, money)VALUES (1, '1001', 10000);INSERT INTO account_tbl (id, user_id, money)VALUES (2, '1002', 10000);# OrderCREATE TABLE `order_tbl`(  `id` INT(11) NOT NULL AUTO_INCREMENT,  `user_id` VARCHAR(255) DEFAULT NULL,  `commodity_code` VARCHAR(255) DEFAULT NULL,  `count` INT(11) DEFAULT '0',  `money` INT(11) DEFAULT '0',  PRIMARY KEY (`id`)) ENGINE = InnoDB DEFAULT CHARSET = utf8;# StorageCREATE TABLE `storage_tbl` (  `id` INT(11) NOT NULL AUTO_INCREMENT,  `commodity_code` VARCHAR(255) DEFAULT NULL,  `count` INT(11) DEFAULT '0',  PRIMARY KEY (`id`),  UNIQUE KEY `commodity_code` (`commodity_code`)) ENGINE = InnoDB DEFAULT CHARSET = utf8;INSERT INTO storage_tbl (id, commodity_code, count)VALUES (1, '2001', 1000);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;/<code>

seata AT 模式需要 undo_log 表,另外三張是業務表。

步驟 2: 啟動 Seata Server

Server端存儲模式(store.mode)現有file、db兩種(後續將引入raft),file模式無需改動,直接啟動即可。db模式需要導入用於存儲全局事務回話信息的三張表。

注:file模式為單機模式,全局事務會話信息內存中讀寫並持久化本地文件root.data,性能較高;


db模式為高可用模式,全局事務會話信息通過db共享,相應性能差些

可以直接通過bash 腳本啟動 Seata Server,也可以通過 Docker 鏡像啟動,但是 Docker 方式目前只支持使用 file 模式,不支持將 Seata-Server 註冊到 Eureka 或 Nacos 等註冊中心。

通過腳本啟動

在 https://github.com/seata/seata/releases 下載相應版本的 Seata Server,解壓後執行以下命令啟動,這裡使用 file 配置

通過 Docker 啟動

<code>docker run --name seata-server -p 8091:8091 seataio/seata-server:latest/<code>

項目介紹


Spring Boot 集成 Seata 解決分佈式事務問題

核心代碼

為了不讓篇幅太長,這裡只給出部分代碼,詳細代碼文末會給出源碼地址

maven 引入 seata 的依賴 eata-spring-boot-starter

<code><dependency>    <groupid>io.seata/<groupid>    <artifactid>seata-spring-boot-starter/<artifactid>    <version>1.0.0/<version>/<dependency>/<code>

倉儲服務

application.properties

<code>spring.application.name=account-serviceserver.port=8081spring.datasource.url=jdbc:mysql://172.16.2.101:3306/db_seata?useSSL=false&serverTimezone=UTCspring.datasource.username=rootspring.datasource.password=123456seata.tx-service-group=my_test_tx_groupmybatis.mapper-locations=classpath*:mapper/*Mapper.xmlseata.service.grouplist=172.16.2.101:8091logging.level.io.seata=infologging.level.io.seata.samples.account.persistence.AccountMapper=debug/<code>

StorageService

<code>public interface StorageService {    /**     * 扣除存儲數量     */    void deduct(String commodityCode, int count);}/<code>

訂單服務

<code>public interface OrderService {    /**     * 創建訂單     */    Order create(String userId, String commodityCode, int orderCount);}/<code>

帳戶服務

<code>public interface AccountService {    /**     * 從用戶賬戶中借出     */    void debit(String userId, int money);}/<code>

主要業務邏輯

只需要使用一個 @GlobalTransactional 註解在業務方法上。

<code>@GlobalTransactionalpublic void purchase(String userId, String commodityCode, int orderCount) {    LOGGER.info("purchase begin ... xid: " + RootContext.getXID());    storageClient.deduct(commodityCode, orderCount);    orderClient.create(userId, commodityCode, orderCount);}/<code>

XID 的傳遞

全局事務ID的跨服務傳遞,需要我們自己實現,這裡通過攔截器的方式。每個服務都需要添加下面兩個類。

SeataFilter

<code>@Componentpublic class SeataFilter implements Filter {    @Override    public void init(FilterConfig filterConfig) throws ServletException {    }    @Override    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {        HttpServletRequest req = (HttpServletRequest) servletRequest;        String xid = req.getHeader(RootContext.KEY_XID.toLowerCase());        boolean isBind = false;        if (StringUtils.isNotBlank(xid)) {            RootContext.bind(xid);            isBind = true;        }        try {            filterChain.doFilter(servletRequest, servletResponse);        } finally {            if (isBind) {                RootContext.unbind();            }        }    }    @Override    public void destroy() {    }}/<code>

SeataRestTemplateAutoConfiguration

<code>@Configurationpublic class SeataRestTemplateAutoConfiguration {    @Autowired(            required = false    )    private Collection<resttemplate> restTemplates;    @Autowired    private SeataRestTemplateInterceptor seataRestTemplateInterceptor;    public SeataRestTemplateAutoConfiguration() {    }    @Bean    public SeataRestTemplateInterceptor seataRestTemplateInterceptor() {        return new SeataRestTemplateInterceptor();    }    @PostConstruct    public void init() {        if (this.restTemplates != null) {            Iterator var1 = this.restTemplates.iterator();            while (var1.hasNext()) {                RestTemplate restTemplate = (RestTemplate) var1.next();                List<clienthttprequestinterceptor> interceptors = new ArrayList(restTemplate.getInterceptors());                interceptors.add(this.seataRestTemplateInterceptor);                restTemplate.setInterceptors(interceptors);            }        }    }}/<clienthttprequestinterceptor>/<resttemplate>/<code>

測試

測試成功場景:

<code>curl -X POST http://127.0.0.1:8084/api/business/purchase/commit/<code>

此時返回結果為:true

測試失敗場景:

UserId 為1002 的用戶下單,sbm-account-service會拋出異常,事務會回滾

<code>http://127.0.0.1:8084/api/business/purchase/rollback/<code>

此時返回結果為:false

查看 undo_log 的日誌或者主鍵,可以看到在執行過程中有保存數據。
如查看主鍵自增的值,在執行前後的值會發生變化,在執行前是 1,執行後是 7 。

源碼地址

https://github.com/gf-huanchupk/SpringBootLearning/tree/master/springboot-seata


分享到:


相關文章: