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

依賴環(huán)境準備
Demo代碼下載
Demo下載地址:github
代碼下載后結構:

MySQL安裝
請參考MySQL環(huán)境安裝
筆者安裝的時候的MySQL版本是8.0.15
庫表的創(chuàng)建
MySQL完成創(chuàng)建后,接下來創(chuàng)建Demo中依賴的庫表。

- 創(chuàng)建fescar庫
create database fescar;
use fescar;
-
創(chuàng)建相關的業(yè)務表和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連接信息的修改
- 修改pom.xml中mysql-connector的版本信息,原始的是5.1.37,根據(jù)你連接的MySQL的版本修改此處的版本信息。
<mysql-connector.version>8.0.15</mysql-connector.version>
-
修改jdbc.properties的連接信息,jdbc.properties文件位于dubbo子項目下的resources文件夾下面
jdbc.properties
將用戶名密碼和庫改為我們上面創(chuàng)建的即可,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
- 下載fescar-server相關的安裝包,https://github.com/alibaba/fescar/releases
- 解壓縮后,啟動server數(shù)據(jù)
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
}
啟動分布式服務調用
- 啟動
DubboAccountServiceStarter,在IDE中啟動即可,啟動中要加入-Djava.net.preferIPv4Stack=true優(yōu)秀選擇ipv4,下面的服務啟動也需要添加這個參數(shù) - 啟動
DubboOrderServiceStarter。 - 啟動
DubboStorageServiceStarter。 - 啟動
DubboBusinessTester。
上面前三步完畢后,可以在數(shù)據(jù)庫中查看到下面的記錄。


執(zhí)行完畢后會出現(xiàn)異常。
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)
不用擔心,代表你已經(jīng)全部成功了,此處只是為了測試回滾主動拋出的異常,你可以去掉BusinessServiceImpl中相關的異常即可。
至此,Seata的本地Demo環(huán)境的全部搭建完畢,接下來我們會進行相關源碼的解讀。

