Browse Source

feat:配置openfeign的超时时间,在各个业务代码的加入睡眠代码,模拟耗时操作,模拟链路调用超时异常导致的数据不一致问题

yang yi 1 year ago
parent
commit
6a1b03319f

+ 9 - 0
seata_ccount_micro_service/src/main/java/space/anyi/seataAccountMicroService/controller/AccountController.java

@@ -7,6 +7,8 @@ import org.springframework.web.bind.annotation.RequestParam;
 import org.springframework.web.bind.annotation.RestController;
 import space.anyi.seataAccountMicroService.service.AccountService;
 
+import java.util.concurrent.TimeUnit;
+
 /**
  * @ProjectName: distributedSystemLearn
  * @FileName: AccountController
@@ -20,6 +22,13 @@ public class AccountController {
     private AccountService accountService;
     @PostMapping("/account/reduce")
     public ResponseResult reduce(@RequestParam("userId")Long userId, @RequestParam("money")Integer money){
+        //模拟耗时操作
+        try {
+            TimeUnit.SECONDS.sleep(12);
+        } catch (InterruptedException e) {
+            e.printStackTrace();
+        }
+        //扣减账户余额
         accountService.reduce(userId,money);
         return ResponseResult.okResult(200,"扣减账户余额OK");
     }

+ 12 - 0
seata_order_micro_servic/src/main/resources/application.yaml

@@ -24,3 +24,15 @@ logging:
   level:
     io:
       seata: info
+feign:
+  client:
+    config:
+      #配置超时时间
+      seata-storage-micro-service:
+        #读取资源超时时间
+        readTimeout: 6000
+        #建立连接超时时间
+        connectTimeout: 1000
+      seata-account-micro-service:
+        readTimeout: 6000
+        connectTimeout: 1000

+ 8 - 0
seata_storage_micro_service/src/main/java/space/anyi/seataStorageMicroService/controller/StorageController.java

@@ -6,6 +6,8 @@ import org.springframework.web.bind.annotation.PostMapping;
 import org.springframework.web.bind.annotation.RestController;
 import space.anyi.seataStorageMicroService.service.StorageService;
 
+import java.util.concurrent.TimeUnit;
+
 /**
  * @ProjectName: distributedSystemLearn
  * @FileName: StorageController
@@ -20,6 +22,12 @@ public class StorageController {
     //扣减库存
     @PostMapping("/storage/reduce")
     public ResponseResult reduce(Long productId, Integer nums){
+        //模拟一个延迟,使得调用链路超时,导致的调用失败
+        try {
+            TimeUnit.SECONDS.sleep(12);
+        } catch (InterruptedException e) {
+            e.printStackTrace();
+        }
         storageService.reduce(productId,nums);
         return ResponseResult.okResult("扣减库存成功ok",null);
     }