Springcloud 分布式事務(wù)解決方案 集成Naco Seata
點擊上方藍(lán)色字體,選擇“標(biāo)星公眾號”
優(yōu)質(zhì)文章,第一時間送達(dá)
? ?消息一致性
? 基于TCC分布式事務(wù)? ?
業(yè)務(wù)系統(tǒng)同時調(diào)用@GlobalTransactional 是Seata的注釋
orderFeignClient 訂單微服務(wù)
accountFeignClient 客戶微服務(wù)
storageFeignClient 庫存微服務(wù)
@GlobalTransactional
????public?void?purchase(String?userId,?String?commodityCode,?int?orderCount)?{
?
????????//?創(chuàng)建訂單
????????orderFeignClient.create(userId,?commodityCode,?orderCount);
????????//?金額減少
????????accountFeignClient.debit(userId,?orderCount);
????????//?庫存服務(wù)?減少庫存
????????storageFeignClient.deduct(commodityCode,?orderCount);
????}
@FeignClient(name?=?"account-service")
public?interface?AccountFeignClient?{
?
????@GetMapping("/test/debit")
????Boolean?debit(@RequestParam("userId")?String?userId,?@RequestParam("count")?int?count);
}
?@Transactional(rollbackFor?=?Exception.class)
????public?void?debit(String?userId,?BigDecimal?num)?{
????????Account?account?=?accountDAO.findByUserId(userId);
????????account.setMoney(account.getMoney().subtract(num));
????????if(account.getMoney().intValue()????????????throw?new?RuntimeException("余額不足");
????????}
?
????????accountDAO.save(account);
?
????????if?(ERROR_USER_ID.equals(userId))?{
????????????throw?new?RuntimeException("account?branch?exception");
????????}
????}
-?file.conf?的?service.vgroup_mapping?配置必須和`spring.application.name`一致
?
在?`org.springframework.cloud:spring-cloud-starter-alibaba-seata`的`org.springframework.cloud.alibaba.seata.GlobalTransactionAutoConfiguration`類中,默認(rèn)會使用?`${spring.application.name}-fescar-service-group`作為服務(wù)名注冊到?Seata?Server上,如果和`file.conf`中的配置不一致,會提示?`no?available?server?to?connect`錯誤
?
也可以通過配置?`spring.cloud.alibaba.seata.tx-service-group`修改后綴,但是必須和`file.conf`中的配置保持一致
?版權(quán)聲明:本文為博主原創(chuàng)文章,遵循 CC 4.0 BY-SA 版權(quán)協(xié)議,轉(zhuǎn)載請附上原文出處鏈接和本聲明。
本文鏈接:
https://blog.csdn.net/yaobo2816/article/details/111127301
粉絲福利:Java從入門到入土學(xué)習(xí)路線圖
???

?長按上方微信二維碼?2 秒
感謝點贊支持下哈?
評論
圖片
表情






