SpringBoot整合Hmily實(shí)現(xiàn)TCC分布式事務(wù)
今天是分布式事務(wù)系列的第二篇,springBoot整合Hmily實(shí)現(xiàn)TCC分布式事務(wù)
第一篇鏈接:springBoot整合atomikos實(shí)現(xiàn)分布式事務(wù)
首先我們創(chuàng)建三個springboot項(xiàng)目
- eureka-server:eureka注冊中心
- test-server:服務(wù)一
- cpy-server:服務(wù)二
大家不要在意項(xiàng)目名稱了,我就隨意取的,大家好創(chuàng)建好三個服務(wù)

然后在 test-server 和 cpy-server服務(wù)的resources目錄下創(chuàng)建 hmily.yml 文件,文件內(nèi)容如下:
hmily:
server:
configMode: local
appName: test-server
# 如果server.configMode eq local 的時候才會讀取到這里的配置信息.
config:
appName: test-server
repository: mysql
ribbon:
rule:
enabled: true
repository:
database:
driverClassName: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://127.0.0.1:3306/hmily?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=Asia/Shanghai&allowPublicKeyRetrieval=true
username: root
password: 12345
metrics:
metricsName: prometheus
//注意:test-server和cpy-server的端口號要不一樣
port: 8801
然后我們需要創(chuàng)建一個hmily 的數(shù)據(jù)庫
test-server服務(wù)改造
現(xiàn)在我們要實(shí)現(xiàn)微服務(wù)之間的服務(wù)調(diào)用,在這里我們使用openfeign來調(diào)用cpy-server服務(wù)的接口

在test-server服務(wù)的TestServiceImpl類中實(shí)現(xiàn)具體的業(yè)務(wù)邏輯
@Service
public class TestServiceImpl implements TestService{
@Resource
private TestDao testDao;
@Resource
private CypFeign cypFeign;
//在需要事務(wù)的方法上加上 @HmilyTCC注解
@HmilyTCC(confirmMethod = "confirm",cancelMethod = "cancel")
@Override
public String insert(Test test) {
//本地服務(wù)調(diào)用
testDao.insert(test);
//調(diào)用cyp-server服務(wù)的接口
cypFeign.insert();
//模擬拋出異常
int i = 1/ 0;
return "success";
}
// confirm名字要與@HmilyTCC中的confirmMethod中配置的值一樣,而是參數(shù)要與方法一致
public String confirm(Test test) {
//修改狀態(tài)為1
System.out.println("test - confirm執(zhí)行了");
testDao.update(test.getId());
return "confirm";
}
// cancel名字要與@HmilyTCC中的cancelMethod中配置的值一樣,而是參數(shù)要與方法一致
public String cancel(Test test) {
System.out.println("test - cancel執(zhí)行了");
testDao.del(test.getId());
return "cancel";
}
然后在openFeign的接口也要加上 @Hmily 注解
@FeignClient(value = "cpy-server")
public interface CypFeign {
@PostMapping("/cyp/insert")
@Hmily
String insert();
}
cpy-server服務(wù)改造
@Service
public class CypServiceImpl implements CypService{
@Resource
private CypDao cypDao;
//在test-server調(diào)用的方法上加上 @HmilyTCC
@HmilyTCC(confirmMethod = "confirm",cancelMethod = "cancel")
public String insert(Cyp cyp) {
cypDao.insert(cyp);
return "success";
}
// confirm名字要與@HmilyTCC中的confirmMethod中配置的值一樣,而是參數(shù)要與方法一致
public String confirm(Cyp cyp) {
System.out.println("confirm執(zhí)行了");
cypDao.update(cyp.getId());
return "confirm";
}
// cancel名字要與@HmilyTCC中的cancelMethod中配置的值一樣,而是參數(shù)要與方法一致
public String cancel(Cyp cyp) {
System.out.println("cancel執(zhí)行了");
cypDao.del(cyp.getId());
return "cancel";
}
}
在有異常的時候,兩個服務(wù)都執(zhí)行了cancel階段的方法


當(dāng)把模擬異常的代碼去掉,發(fā)現(xiàn)兩個服務(wù)都正常的執(zhí)行了提交方法


到此,springboot微服務(wù)整合Hmily實(shí)現(xiàn)TCC分布式事務(wù)已經(jīng)完成了
但是TCC服務(wù)還是要有一些問題需要考慮的,主要有以下幾個
- 1:try階段異常
- 2:cancel階段異常
- 3:comfirm階段異常
- 4:本地事務(wù)與TCC事務(wù)沖突
- 5:空回滾
- 6:事務(wù)懸掛
至于如何解決以上問題,可以參考:TCC分布式事務(wù)七種異常情況小結(jié)
以上就是SpringBoot整合Hmily實(shí)現(xiàn)TCC分布式事務(wù)的詳細(xì)內(nèi)容,更多關(guān)于SpringBoot Hmily TCC分布式事務(wù)的資料請關(guān)注腳本之家其它相關(guān)文章!
- Springboot-dubbo-fescar 阿里分布式事務(wù)的實(shí)現(xiàn)方法
- SpringBoot+Dubbo+Seata分布式事務(wù)實(shí)戰(zhàn)詳解
- 詳解SpringBoot基于Dubbo和Seata的分布式事務(wù)解決方案
- springboot cloud使用eureka整合分布式事務(wù)組件Seata 的方法
- springboot整合rocketmq實(shí)現(xiàn)分布式事務(wù)
- springboot整合shardingsphere和seata實(shí)現(xiàn)分布式事務(wù)的實(shí)踐
- java?SpringBoot?分布式事務(wù)的解決方案(JTA+Atomic+多數(shù)據(jù)源)
- springboot整合Atomikos的示例詳解
相關(guān)文章
SpringMVC?@RequestMapping注解屬性詳細(xì)介紹
通過@RequestMapping注解可以定義不同的處理器映射規(guī)則,下面這篇文章主要給大家介紹了關(guān)于SpringMVC中@RequestMapping注解用法的相關(guān)資料,文中通過實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2023-02-02
深入學(xué)習(xí)spring cloud gateway 限流熔斷
這篇文章主要介紹了深入學(xué)習(xí)spring cloud gateway 限流熔斷,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-04-04
基于list stream: reduce的使用實(shí)例
這篇文章主要介紹了list stream: reduce的使用實(shí)例,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-09-09
@RequestMapping對不同參數(shù)的接收方式示例詳解
Spring?MVC框架中,@RequestMapping注解用于映射URL到控制器方法,不同的參數(shù)類型如簡單參數(shù)、實(shí)體參數(shù)、數(shù)組參數(shù)、集合參數(shù)、日期參數(shù)和JSON參數(shù),本文給大家介紹@RequestMapping對不同參數(shù)的接收方式,感興趣的朋友一起看看吧2024-10-10

