<kbd id="afajh"><form id="afajh"></form></kbd>
<strong id="afajh"><dl id="afajh"></dl></strong>
    <del id="afajh"><form id="afajh"></form></del>
        1. <th id="afajh"><progress id="afajh"></progress></th>
          <b id="afajh"><abbr id="afajh"></abbr></b>
          <th id="afajh"><progress id="afajh"></progress></th>

          Springcloud 分布式事務(wù)解決方案 集成Naco Seata

          共 1492字,需瀏覽 3分鐘

           ·

          2020-12-18 15:24

          點擊上方藍(lán)色字體,選擇“標(biāo)星公眾號”

          優(yōu)質(zhì)文章,第一時間送達(dá)

          66套java從入門到精通實戰(zhàn)課程分享

          前言:分布式系統(tǒng)架構(gòu)中,最最費勁的是分布式事務(wù),分布式事務(wù)解決方案網(wǎng)上大致分為兩種

          1. ? ?消息一致性

          2. ? 基于TCC分布式事務(wù)? ?

          ? ?不管基于那種解決方案,都是對侵入的代碼植入,以大量的代碼或者消息來作為代價,來實現(xiàn)分布式事務(wù)。

          ? ?有沒有一種可以非侵入的分布式事務(wù)解決方案,答案是有的。阿里Seata

          ? Seata 是一款開源的分布式事務(wù)解決方案,致力于提供高性能和簡單易用的分布式事務(wù)服務(wù)。Seata 將為用戶提供了 AT、TCC、SAGA 和 XA 事務(wù)模式,為用戶打造一站式的分布式解決方案。

          ??最重要的是非侵入性的。不用動原有的代碼邏輯,直接在方法體上加入注解@GlobalTransactional,就可以實現(xiàn)。

          ?zipkin? 依賴圖如下

          ?

          業(yè)務(wù)系統(tǒng)同時調(diào)用

          訂單服務(wù)? ? 新增一條訂單

          ?客戶服務(wù)? ? 用戶自己賬戶扣費

          庫存服務(wù)? ? 庫存減少

          ?例子是從Seata官網(wǎng)下載,自己修改了服務(wù)間調(diào)用,集成了Naco和鏈路追追zipkin。

          ?比較重要的代碼, 先解釋。

          @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");
          ????????}
          ????}

          配置的時候幾點注意說明,非常重要

          application.properties文件

          ?

          file.conf 文件


          其中紅色區(qū)域?business-service?一定一定一定要一樣。

          這是解釋:

          -?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`中的配置保持一致

          代碼如下

          https://gitee.com/yaobo2816/springcloud-AlibabaNaco/tree/master/seata-samples-master/springcloud-jpa-seata

          springcloud-jpa-seata這是代碼根路徑。

          ?

          代碼集成Naco? Seata OpenFeign??zipkin 鏈路分析

          相關(guān)截圖

          ?

          ?

          測試?

          http://localhost:8084/test/purchase/commit??購買下單,模擬全局事務(wù)提交? ? ?

          http://localhost:8084/test/purchase/rollback ??購買下單,當(dāng)發(fā)生請求超時,庫存不足,或者金額不足的時候,全局事務(wù)回滾。




          版權(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 秒


          感謝點贊支持下哈?

          瀏覽 81
          點贊
          評論
          收藏
          分享

          手機(jī)掃一掃分享

          分享
          舉報
          評論
          圖片
          表情
          推薦
          點贊
          評論
          收藏
          分享

          手機(jī)掃一掃分享

          分享
          舉報
          <kbd id="afajh"><form id="afajh"></form></kbd>
          <strong id="afajh"><dl id="afajh"></dl></strong>
            <del id="afajh"><form id="afajh"></form></del>
                1. <th id="afajh"><progress id="afajh"></progress></th>
                  <b id="afajh"><abbr id="afajh"></abbr></b>
                  <th id="afajh"><progress id="afajh"></progress></th>
                  成人黄色在线网站 | 上床视频网站在线 | 波多野吉衣中文字幕 | 韩国精品亚洲精品 | 色哟哟哟 入口国产精品 |