SpringCloud Alibaba使用Seata處理分布式事務(wù)的技巧
Seata簡(jiǎn)介
在傳統(tǒng)的單體項(xiàng)目中,我們使用@Transactional注解就能實(shí)現(xiàn)基本的ACID事務(wù)了。
但是前提是:
1) 數(shù)據(jù)庫(kù)支持事務(wù)(如:MySQL的innoDB引擎)
2) 所有業(yè)務(wù)都在同一個(gè)數(shù)據(jù)庫(kù)中執(zhí)行
隨著微服務(wù)架構(gòu)的引入,需要對(duì)數(shù)據(jù)庫(kù)進(jìn)行分庫(kù)分表,每個(gè)服務(wù)擁有自己的數(shù)據(jù)庫(kù),這樣傳統(tǒng)的事務(wù)就不起作用了,那么我們?nèi)绾伪WC多個(gè)服務(wù)中數(shù)據(jù)的一致性呢?
這樣就出現(xiàn)了分布式事務(wù),而Seata就是為微服務(wù)架構(gòu)而生的一種高性能、易于使用的分布式事務(wù)解決方案。
Seata 中有三個(gè)基礎(chǔ)組件:
- Transaction Coordinator(TC協(xié)調(diào)者):維護(hù)全局和分支事務(wù)的狀態(tài),驅(qū)動(dòng)全局提交或回滾。
- Transaction Manager(TM事務(wù)管理):定義全局事務(wù)的范圍,開啟、提交或回滾一個(gè)全局事務(wù)。
- Resource Manager(RM資源管理):管理分支事務(wù)資源,與 TC 通訊并報(bào)告分支事務(wù)狀態(tài),管理本地事務(wù)的提交與回滾。
可以這么說(shuō)一個(gè)分布式事務(wù)就是全局事務(wù)GlobalTransaction,而全局事務(wù)是由一個(gè)個(gè)的分支事務(wù)組成的,每個(gè)分支事務(wù)就是一個(gè)本地事務(wù)。
Seata的生命周期
- TM 要求 TC 生成一個(gè)全局事務(wù),并由 TC 生成一個(gè)全局事務(wù)XID 返回。
- XID 通過(guò)微服務(wù)調(diào)用鏈傳播。
- RM 向 TC 注冊(cè)本地事務(wù),將其注冊(cè)到 ID 為 XID 的全局事務(wù)中。
- TM 要求 TC 提交或回滾XID 對(duì)應(yīng)的全局事務(wù)。
- TC 驅(qū)動(dòng) XID 對(duì)應(yīng)的全局事務(wù)對(duì)應(yīng)的所有的分支事務(wù)提交或回滾。
Seata安裝和配置
安裝nacos,本案例使用了nacos作為注冊(cè)中心
https://github.com/alibaba/nacos/releases
下載nacos,本文使用的是windows版本1.4.0
使用命令行進(jìn)入bin目錄,以單機(jī)模式啟動(dòng)nacos
startup -m standalone
安裝和配置Seata
http://seata.io/zh-cn/blog/download.html
下載Seata,這里是Windows版本的1.4.0
解壓后,進(jìn)入conf目錄,配置file.conf和registry.conf兩個(gè)文件
file.conf主要是數(shù)據(jù)庫(kù)的配置,配置如下
registry.conf 是注冊(cè)中心的配置
另外conf目錄中還需要一個(gè)腳本文件:nacos-config.sh 用于對(duì)nacos進(jìn)行初始化配置
在seata1.4.0中是沒有的,需要自行創(chuàng)建,內(nèi)容如下:
#!/usr/bin/env bash # 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 urlencode() { for ((i=0; i < ${#1}; i++)) do char="${1:$i:1}" case $char in [a-zA-Z0-9.~_-]) printf $char ;; *) printf '%%%02X' "'$char" ;; 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" failCount=0 tempLog=$(mktemp -u) function addConfig() { curl -X POST -H "${contentType}" "http://$nacosAddr/nacos/v1/cs/configs?dataId=$(urlencode $1)&group=$group&content=$(urlencode $2)&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++ )) fi } count=0 for line in $(cat $(dirname "$PWD")/config.txt | sed s/[[:space:]]//g); do (( count++ )) 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
在seata的根目錄,與conf同級(jí)的目錄下,還需要config.txt 配置文件,默認(rèn)也是沒有的
只需要對(duì)mysql的配置進(jìn)行修改
完整文件:
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.my_test_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=xx 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.jdbc.Driver store.db.url=jdbc:mysql://127.0.0.1:3306/seata?useUnicode=true&rewriteBatchedStatements=true store.db.user=root store.db.password=123456 store.db.minConn=5 store.db.maxConn=30 store.db.globalTable=global_table store.db.branchTable=branch_table 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=xx store.redis.sentinel.sentinelHosts=xx store.redis.maxConn=10 store.redis.minConn=1 store.redis.maxTotal=100 store.redis.database=0 store.redis.password=xx 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
在conf目錄中,使用Git Bash進(jìn)入命令行,輸入
sh nacos-config.sh 127.0.0.1
這是對(duì)Seata進(jìn)行初始化配置,上圖表示所有配置都成功設(shè)置了
在nacos中可以看到出現(xiàn)了seata相關(guān)的配置
接下來(lái)在seata數(shù)據(jù)庫(kù)中,新建三個(gè)表
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`) ); 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`) ); 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`) );
在項(xiàng)目相關(guān)的數(shù)據(jù)庫(kù)中,新建表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;
最后在bin目錄中,啟動(dòng)命令行,執(zhí)行seata-server.bat 啟動(dòng)Seata服務(wù)
項(xiàng)目應(yīng)用Seata
SpringCloud項(xiàng)目中有兩個(gè)服務(wù):訂單服務(wù)和庫(kù)存服務(wù),基本業(yè)務(wù)是:
- 購(gòu)買商品
- 插入訂單
- 減少庫(kù)存
訂單詳情表
DROP TABLE IF EXISTS `tb_order_detail`; CREATE TABLE `tb_order_detail` ( `id` bigint(20) NOT NULL AUTO_INCREMENT COMMENT '訂單詳情id ', `order_id` bigint(20) NOT NULL COMMENT '訂單id', `sku_id` bigint(20) NOT NULL COMMENT 'sku商品id', `num` int(11) NOT NULL COMMENT '購(gòu)買數(shù)量', `title` varchar(256) NOT NULL COMMENT '商品標(biāo)題', `own_spec` varchar(1024) DEFAULT '' COMMENT '商品動(dòng)態(tài)屬性鍵值集', `price` bigint(20) NOT NULL COMMENT '價(jià)格,單位:分', `image` varchar(128) DEFAULT '' COMMENT '商品圖片', PRIMARY KEY (`id`), KEY `key_order_id` (`order_id`) USING BTREE ) ENGINE=MyISAM AUTO_INCREMENT=131 DEFAULT CHARSET=utf8 COMMENT='訂單詳情表';
庫(kù)存表
DROP TABLE IF EXISTS `tb_stock`; CREATE TABLE `tb_stock` ( `sku_id` bigint(20) NOT NULL COMMENT '庫(kù)存對(duì)應(yīng)的商品sku id', `seckill_stock` int(9) DEFAULT '0' COMMENT '可秒殺庫(kù)存', `seckill_total` int(9) DEFAULT '0' COMMENT '秒殺總數(shù)量', `stock` int(9) NOT NULL COMMENT '庫(kù)存數(shù)量', PRIMARY KEY (`sku_id`) ) ENGINE=MyISAM DEFAULT CHARSET=utf8 COMMENT='庫(kù)存表,代表庫(kù)存,秒殺庫(kù)存等信息';
父項(xiàng)目定義了springboot、springcloud、springcloud-alibaba的版本
<parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.3.10.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-alibaba-dependencies</artifactId> <version>0.9.0.RELEASE</version> <type>pom</type> <scope>import</scope> </dependency> <dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-alibaba-dependencies</artifactId> <version>2.2.1.RELEASE</version> <type>pom</type> <scope>import</scope> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>Hoxton.SR8</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement>
子項(xiàng)目的依賴定義了nacos和seata客戶端
<dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <scope>runtime</scope> </dependency> <dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-boot-starter</artifactId> <version>3.3.2</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId> </dependency> <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.2.0</version> </dependency>
子項(xiàng)目配置文件
完整配置
server: port: 8001 spring: application: name: stock-service cloud: nacos: discovery: server-addr: localhost:8848 alibaba: seata: enabled: true enable-auto-data-source-proxy: true tx-service-group: my_test_tx_group registry: type: nacos nacos: application: seata-server server-addr: 127.0.0.1:8848 username: nacos password: nacos config: type: nacos nacos: server-addr: 127.0.0.1:8848 group: SEATA_GROUP username: nacos password: nacos service: vgroup-mapping: my_test_tx_group: default disable-global-transaction: false client: rm: report-success-enable: false datasource: driver-class-name: com.mysql.cj.jdbc.Driver url: jdbc:mysql://localhost:3306/eshop?serverTimezone=UTC&useUnicode=true&useSSL=false&characterEncoding=utf8 username: root password: 123456
庫(kù)存服務(wù)定義了減庫(kù)存的方法
@RestController public class StockController { @Autowired private IStockService stockService; @PutMapping("/stock") public ResponseEntity<Stock> reduceSkuStock(@RequestParam("skuId")Long skuId, @RequestParam("number")Integer number){ Stock stock = stockService.getById(skuId); if(stock.getStock() < number){ throw new RuntimeException("庫(kù)存不足,SkuId:" + skuId); } stock.setStock(stock.getStock() - number); stockService.updateById(stock); return ResponseEntity.ok(stock); } }
訂單服務(wù)在插入訂單后,使用Feign調(diào)用了減庫(kù)存的服務(wù)
@Service public class OrderDetailServiceImpl extends ServiceImpl<OrderDetailMapper, OrderDetail> implements IOrderDetailService { //庫(kù)存服務(wù)Feign @Autowired private StockFeignClient stockFeignClient; // @Transactional @GlobalTransactional(rollbackFor = {Exception.class}) @Override public void makeOrder(OrderDetail orderDetail) { this.save(orderDetail); //保存訂單 int x = 11 / 0; //拋出異常 //減庫(kù)存 stockFeignClient.reduceSkuStock(orderDetail.getSkuId(),orderDetail.getNum()); } }
插訂單和減庫(kù)存屬于兩個(gè)服務(wù),傳統(tǒng)的@Transactional已經(jīng)不能保證它們的原子性了
這里使用了Seata提供的@GlobalTransactional全局事務(wù)注解,出現(xiàn)任何異常后都能實(shí)現(xiàn)業(yè)務(wù)回滾。
測(cè)試用例:
@RunWith(SpringRunner.class) @SpringBootTest public class OrderServiceApplicationTests { @Autowired private IOrderDetailService orderDetailService; @Test public void testOrder() { OrderDetail orderDetail = new OrderDetail(); orderDetail.setNum(100); orderDetail.setOrderId(9999L); orderDetail.setPrice(9999L); orderDetail.setSkuId(27359021728L); orderDetail.setTitle(UUID.randomUUID().toString()); orderDetailService.makeOrder(orderDetail); } }
運(yùn)行后看到啟動(dòng)了全局事務(wù),發(fā)生異常后,兩個(gè)服務(wù)也都能成功回滾。
以上就是SpringCloud Alibaba使用Seata 分布式事務(wù)的詳細(xì)內(nèi)容,更多關(guān)于SpringCloud Alibaba分布式事務(wù)的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
Java實(shí)現(xiàn)飛機(jī)大戰(zhàn)-連接數(shù)據(jù)庫(kù)并把得分寫入數(shù)據(jù)庫(kù)
這篇文章給大家分享了Java實(shí)現(xiàn)飛機(jī)大戰(zhàn)中連接數(shù)據(jù)庫(kù)并把得分寫入數(shù)據(jù)庫(kù)的相關(guān)知識(shí)點(diǎn)和代碼,有興趣的可以學(xué)習(xí)參考下。2018-07-07Java 中Timer和TimerTask 定時(shí)器和定時(shí)任務(wù)使用的例子
這篇文章主要介紹了Java 中Timer和TimerTask 定時(shí)器和定時(shí)任務(wù)使用的例子,非常具有實(shí)用價(jià)值,需要的朋友可以參考下2017-05-05idea項(xiàng)目結(jié)構(gòu)中不顯示out文件夾的解決
本文通過(guò)圖片的方式詳細(xì)解釋操作步驟,使讀者能夠更直觀更方便地理解和執(zhí)行操作,同時(shí),文章末尾祝福讀者步步高升,一帆風(fēng)順,展現(xiàn)了作者的人情味和親和力,整體來(lái)說(shuō),這是一篇簡(jiǎn)單易懂、實(shí)用性強(qiáng)的操作指南2024-10-10spring boot自定義log4j2日志文件的實(shí)例講解
下面小編就為大家分享一篇spring boot自定義log4j2日志文件的實(shí)例講解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2017-11-11Java實(shí)現(xiàn)的各種排序算法(插入排序、選擇排序算法、冒泡排序算法)
本文是小編給大家?guī)?lái)的java各種排序算法知識(shí),包括插入排序、選擇排序算法、冒泡排序算法,代碼簡(jiǎn)單易懂,需要的朋友可以參考下2016-08-08Spring中@RabbitHandler和@RabbitListener的區(qū)別詳析
@RabbitHandler是用于處理消息的方法注解,它與@RabbitListener注解一起使用,這篇文章主要給大家介紹了關(guān)于Spring中@RabbitHandler和@RabbitListener區(qū)別的相關(guān)資料,需要的朋友可以參考下2024-02-02