分佈式事務框架Seata---demo搭建指南

分佈式事務框架(Seata)介紹

Seata:Simple Extensible Autonomous Transaction Architecture,簡易可擴展的自治式分佈式事務管理框架,其前身是fescar。阿里巴巴GTS的開源版實現,是一種分佈式事務的解決方案,詳情請參看seata官方文檔
seata主要由三個重要組件組成:
Transaction Coordinator(TC):管理全局的分支事務的狀態,用於全局性事務的提交和回滾。
Transaction Manager(TM):事務管理器,用於開啓全局事務、提交或者回滾全局事務,是全局事務的開啓者。
Resource Manager(RM):資源管理器,用於分支事務上的資源管理,向TC註冊分支事務,上報分支事務的狀態,接受TC的命令來提交或者回滾分支事務。

典型事務流程圖

 

依賴環境準備

Demo代碼下載

Demo下載地址:github
代碼下載後結構:

samples代碼結構

 

MySQL安裝

請參考MySQL環境安裝
筆者安裝的時候的MySQL版本是8.0.15

庫表的創建

MySQL完成創建後,接下來創建Demo中依賴的庫表。

 

鏈接MySQL

  1. 創建fescar庫

create database fescar;
use fescar;

  1. 創建相關的業務表和undo_log

     

    表的初始化SQL

DROP TABLE IF EXISTS `storage_tbl`;
CREATE 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`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;


DROP TABLE IF EXISTS `order_tbl`;
CREATE 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;


DROP TABLE IF EXISTS `account_tbl`;
CREATE 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;

-- 注意此處0.3.0+ 增加唯一索引 ux_undo_log
CREATE TABLE `undo_log` (
  `id` bigint(20) NOT NULL AUTO_INCREMENT,
  `branch_id` bigint(20) NOT NULL,
  `xid` varchar(100) 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;

MySQL連接信息的修改

  1. 修改pom.xml中mysql-connector的版本信息,原始的是5.1.37,根據你連接的MySQL的版本修改此處的版本信息。
 <mysql-connector.version>8.0.15</mysql-connector.version>
  1. 修改jdbc.properties的連接信息,jdbc.properties文件位於dubbo子項目下的resources文件夾下面

     

    jdbc.properties

     

    將用戶名密碼和庫改爲我們上面創建的即可,fescar_demo改爲fescar。

jdbc.account.url=jdbc:mysql://localhost:3306/fescar
jdbc.account.username=root
jdbc.account.password=12345678
jdbc.account.driver=com.mysql.jdbc.Driver
# storage db config
jdbc.storage.url=jdbc:mysql://localhost:3306/fescar
jdbc.storage.username=root
jdbc.storage.password=12345678
jdbc.storage.driver=com.mysql.jdbc.Driver
# order db config
jdbc.order.url=jdbc:mysql://localhost:3306/fescar
jdbc.order.username=root
jdbc.order.password=12345678
jdbc.order.driver=com.mysql.jdbc.Driver

服務的啓動

啓動fescar-server

  1. 下載fescar-server相關的安裝包,https://github.com/alibaba/fescar/releases
  2. 解壓縮後,啓動server數據
    sh fescar-server.sh 8091 /home/admin/fescar/data/
    端口設置爲8091,如果此處爲別的接口,需要修改file.conf中的default.grouplist的配置項,此處默認就可以,不用修改。
service {
  #vgroup->rgroup
  vgroup_mapping.my_test_tx_group = "default"
  #only support single node
  default.grouplist = "127.0.0.1:8091"
  #degrade current not support
  enableDegrade = false
  #disable
  disable = false
}

啓動分佈式服務調用

  1. 啓動DubboAccountServiceStarter,在IDE中啓動即可,啓動中要加入-Djava.net.preferIPv4Stack=true優秀選擇ipv4,下面的服務啓動也需要添加這個參數
  2. 啓動DubboOrderServiceStarter
  3. 啓動DubboStorageServiceStarter
  4. 啓動DubboBusinessTester

上面前三步完畢後,可以在數據庫中查看到下面的記錄。

 

account

 

storage

執行完畢後會出現異常。

log4j:WARN No appenders could be found for logger (org.springframework.context.support.ClassPathXmlApplicationContext).
log4j:WARN Please initialize the log4j system properly.
log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.
Exception in thread "main" java.lang.RuntimeException: xxx
    at com.alibaba.fescar.samples.dubbo.service.impl.BusinessServiceImpl.purchase(BusinessServiceImpl.java:48)
    at com.alibaba.fescar.samples.dubbo.service.impl.BusinessServiceImpl$$FastClassBySpringCGLIB$$ec52ad64.invoke(<generated>)
    at org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:218)
    at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.invokeJoinpoint(CglibAopProxy.java:749)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:163)
    at com.alibaba.fescar.spring.annotation.GlobalTransactionalInterceptor$2.execute(GlobalTransactionalInterceptor.java:97)
    at com.alibaba.fescar.tm.api.TransactionalTemplate.execute(TransactionalTemplate.java:64)
    at com.alibaba.fescar.spring.annotation.GlobalTransactionalInterceptor.handleGlobalTransaction(GlobalTransactionalInterceptor.java:94)
    at com.alibaba.fescar.spring.annotation.GlobalTransactionalInterceptor.invoke(GlobalTransactionalInterceptor.java:66)

不用擔心,代表你已經全部成功了,此處只是爲了測試回滾主動拋出的異常,你可以去掉BusinessServiceImpl中相關的異常即可。

至此,Seata的本地Demo環境的全部搭建完畢,接下來我們會進行相關源碼的解讀。

喜歡請關注,歡迎大家轉載!

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章