欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

SpringBoot整合Hmily實(shí)現(xiàn)TCC分布式事務(wù)

 更新時(shí)間:2024年11月04日 10:02:00   作者:我是小趴菜  
這篇文章主要為大家詳細(xì)介紹了SpringBoot如何整合Hmily實(shí)現(xiàn)TCC分布式事務(wù),文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下

今天是分布式事務(wù)系列的第二篇,springBoot整合Hmily實(shí)現(xiàn)TCC分布式事務(wù)

第一篇鏈接:springBoot整合atomikos實(shí)現(xiàn)分布式事務(wù)

首先我們創(chuàng)建三個(gè)springboot項(xiàng)目

  • eureka-server:eureka注冊(cè)中心
  • test-server:服務(wù)一
  • cpy-server:服務(wù)二

大家不要在意項(xiàng)目名稱了,我就隨意取的,大家好創(chuàng)建好三個(gè)服務(wù)

然后在 test-servercpy-server服務(wù)的resources目錄下創(chuàng)建 hmily.yml 文件,文件內(nèi)容如下:

hmily:
  server:
    configMode: local
    appName: test-server

  #  如果server.configMode eq local 的時(shí)候才會(huì)讀取到這里的配置信息.
  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的端口號(hào)要不一樣
  port: 8801

然后我們需要?jiǎng)?chuàng)建一個(gè)hmily 的數(shù)據(jù)庫(kù)

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";
    }
}

在有異常的時(shí)候,兩個(gè)服務(wù)都執(zhí)行了cancel階段的方法

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

到此,springboot微服務(wù)整合Hmily實(shí)現(xiàn)TCC分布式事務(wù)已經(jīng)完成了

但是TCC服務(wù)還是要有一些問題需要考慮的,主要有以下幾個(gè)

  • 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ù)的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

最新評(píng)論