??作者主頁:青花鎖 ??簡介:Java領域優(yōu)質創(chuàng)作者??、Java微服務架構公號作者??
??簡歷模板、學習資料、面試題庫、技術互助
??文末獲取聯系方式 ??
往期熱門專欄回顧
專欄 | 描述 |
---|---|
Java項目實戰(zhàn) | 介紹Java組件安裝、使用;手寫框架等 |
Aws服務器實戰(zhàn) | Aws Linux服務器上操作nginx、git、JDK、Vue |
Java微服務實戰(zhàn) | Java 微服務實戰(zhàn),Spring Cloud Netflix套件、Spring Cloud Alibaba套件、Seata、gateway、shadingjdbc等實戰(zhàn)操作 |
Java基礎篇 | Java基礎閑聊,已出HashMap、String、StringBuffer等源碼分析,JVM分析,持續(xù)更新中 |
Springboot篇 | 從創(chuàng)建Springboot項目,到加載數據庫、靜態(tài)資源、輸出RestFul接口、跨越問題解決到統一返回、全局異常處理、Swagger文檔 |
Spring MVC篇 | 從創(chuàng)建Spring MVC項目,到加載數據庫、靜態(tài)資源、輸出RestFul接口、跨越問題解決到統一返回 |
華為云服務器實戰(zhàn) | 華為云Linux服務器上操作nginx、git、JDK、Vue等,以及使用寶塔運維操作添加Html網頁、部署Springboot項目/Vue項目等 |
Java爬蟲 | 通過Java+Selenium+GoogleWebDriver 模擬真人網頁操作爬取花瓣網圖片、bing搜索圖片等 |
Vue實戰(zhàn) | 講解Vue3的安裝、環(huán)境配置,基本語法、循環(huán)語句、生命周期、路由設置、組件、axios交互、Element-ui的使用等 |
Spring | 講解Spring(Bean)概念、IOC、AOP、集成jdbcTemplate/redis/事務等 |
系列文章目錄
第一章 Java線程池技術應用
第二章 CountDownLatch和Semaphone的應用
第三章 Spring Cloud 簡介
第四章 Spring Cloud Netflix 之 Eureka
第五章 Spring Cloud Netflix 之 Ribbon
第六章 Spring Cloud 之 OpenFeign
第七章 Spring Cloud 之 GateWay
第八章 Spring Cloud Netflix 之 Hystrix
第九章 代碼管理gitlab 使用
第十章 SpringCloud Alibaba 之 Nacos discovery
第十一章 SpringCloud Alibaba 之 Nacos Config
第十二章 Spring Cloud Alibaba 之 Sentinel
第十三章 JWT
第十四章 RabbitMQ應用
第十五章 RabbitMQ 延遲隊列
第十六章 spring-cloud-stream
第十七章 Windows系統安裝Redis、配置環(huán)境變量
第十八章 查看、修改Redis配置,介紹Redis類型
第十九章 Redis RDB AOF
第二十章 Spring boot 操作 Redis
第二十一章 Java多線程安全與鎖
第二十二章 Java微服務分布式事務框架seata
第二十三章 Java微服務分布式事務框架seata的TCC模式
前言
上一章節(jié)介紹微服務分布式項目中,使用的事務框架seata,本章節(jié)介紹分布式事務框架seata TCC模式,以及集成到Springboot、微服務框架里。
1、TCC模式
一個分布式的全局事務,整體是 兩階段提交 的模型。全局事務是由若干分支事務組成的,分支事務要滿足 兩階段提交 的模型要求,即需要每個分支事務都具備自己的:
- 一階段 prepare 行為
- 二階段 commit 或 rollback 行為
根據兩階段行為模式的不同,我們將分支事務劃分為 Automatic (Branch) Transaction Mode 和 TCC (Branch) Transaction Mode.
1.1、AT 模式(參考鏈接 TBD)基于 支持本地 ACID 事務 的 關系型數據庫
1.1.1、 一階段 prepare 行為
在本地事務中,一并提交業(yè)務數據更新和相應回滾日志記錄。
1.1.2、二階段 commit 行為
馬上成功結束,自動 異步批量清理回滾日志。
1.1.3、二階段 rollback 行為
通過回滾日志,自動 生成補償操作,完成數據回滾。
1.2、TCC 模式,不依賴于底層數據資源的事務支持
1.2.1、一階段 prepare 行為
調用 自定義 的 prepare 邏輯。try
1.2.2、二階段 commit 行為
調用 自定義 的 commit 邏輯。confirm
1.2.3、二階段 rollback 行為
調用 自定義 的 rollback 邏輯。cancel
2、例子
2.1、定義controller
/**
* 采購
*/
@PostMapping("/purchaseTCC")
@GlobalTransactional
public String purchaseTCC(@RequestBody OrderDTO orderDTO){
this.businessTCCService.purchase(orderDTO);
return "success";
}
2.2、定義service
@LocalTCC
public interface BusinessTCCService {
/**
* 采購 執(zhí)行資源檢查及預留操作
*/
@TwoPhaseBusinessAction(name = "purchase",commitMethod = "commit",rollbackMethod = "rollback")
public void purchase(@BusinessActionContextParameter(paramName = "orderDTO") OrderDTO orderDTO);
/**
* 全局事務進行提交
* @param businessActionContext
* @return
*/
boolean commit(BusinessActionContext businessActionContext);
/**
* 全局事務進行不回滾
* @param businessActionContext
* @return
*/
boolean rollback(BusinessActionContext businessActionContext);
}
package com.xxxx.store.business.service.impl;
import cn.hutool.crypto.SecureUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.xxxx.store.business.service.BusinessService;
import com.xxxx.store.business.service.BusinessTCCService;
import com.xxxx.store.business.service.OrderService;
import com.xxxx.store.business.service.StorageService;
import com.xxxx.store.common.dto.OrderDTO;
import com.xxxx.store.common.dto.StorageDTO;
import io.seata.rm.tcc.api.BusinessActionContext;
import io.seata.spring.annotation.GlobalTransactional;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import java.io.File;
@Service
public class BusinessTCCServiceImpl implements BusinessTCCService {
@Value("${file.path}")
private String filePath;
@Autowired
private OrderService orderService;
@Autowired
private StorageService storageService;
@Override
public void purchase(OrderDTO orderDTO) {
//減庫存
this.storageService.deduct(new StorageDTO(null,orderDTO.getCommodityCode(),orderDTO.getCount()));
//創(chuàng)建訂單
this.orderService.create(orderDTO);
}
@Override
public boolean commit(BusinessActionContext businessActionContext) {
System.out.println("事務ID:" + businessActionContext.getXid());
return true;
}
@Override
public boolean rollback(BusinessActionContext businessActionContext) {
JSONObject jSONObject = (JSONObject)businessActionContext.getActionContext("orderDTO");
OrderDTO orderDTO = jSONObject.toJavaObject(OrderDTO.class);
StorageDTO storageDTO = new StorageDTO(null, orderDTO.getCommodityCode(), orderDTO.getCount());
String s = JSON.toJSONString(storageDTO);
String md5 = SecureUtil.md5(s);
System.out.println("**************觸發(fā)回滾操作:" + filePath + md5);
File file = new File(filePath + md5);
file.delete();
return true;
}
/*@Override
public void purchase(OrderDTO orderDTO) {
//減庫存
this.storageService.deduct(new StorageDTO(null,orderDTO.getCommodityCode(),orderDTO.getCount()));
//創(chuàng)建訂單
this.orderService.create(orderDTO);
}*/
}
注解 | 描述 |
---|---|
@LocalTCC | 一定需要注解在接口上,否則不生效,此接口可以是尋常的業(yè)務接口,只要實現了TCC的兩階段提交對應方法便可,適用于SpringCloud+Feign模式下的TCC。 |
@TwoPhaseBusinessAction | 注解try方法,其中name為當前tcc方法的bean名稱,寫方法名便可(全局唯一),commitMethod指向提交方法,rollbackMethod指向事務回滾方法。指定好三個方法之后,seata會根據全局事務的成功或失敗,自動調用提交方法或者回滾方法。 |
@BusinessActionContextParameter | 使用該注解可以將參數傳遞到二階段commit或者rollback的方法中,方便調用。 |
BusinessActionContext | TCC事務上下文,使用BusinessActionContext.getActionContext(“params”)便可以得到一階段try中定義的參數,在二階段參考此參數進行業(yè)務回滾操作。 |
建議:可以在try方法中使用@Transational,直接通過spring來控制關系型數據庫的事務,進行回滾的操作,而非關系型數據庫等中間件的回滾操作可以交給rollbackMethod方法處理。
建議:try接口不可以捕獲異常,否則TCC將識別該操作為成功,直接執(zhí)行二階段commit方法。
資料獲取,更多粉絲福利,關注下方公眾號獲取文章來源:http://www.zghlxwxcb.cn/news/detail-842617.html
文章來源地址http://www.zghlxwxcb.cn/news/detail-842617.html
到了這里,關于Java微服務分布式事務框架seata的TCC模式的文章就介紹完了。如果您還想了解更多內容,請在右上角搜索TOY模板網以前的文章或繼續(xù)瀏覽下面的相關文章,希望大家以后多多支持TOY模板網!