場景一:客戶端timeout 的時間給的很短
?//100毫秒
private static final int HTTP_TIMEOUT_MS = 1 * 100;?
response = HttpUtil.createPost(patrolresultconfirmUrl).body(JSONObject.toJSONString(search)).header("Authorization", token).timeout(HTTP_TIMEOUT_MS).executeAsync();
Caused by: java.net.SocketTimeoutException: Read timed out
場景二:服務端 返回的時間調的很長
客戶端的時間timeout 給 30s
服務設置40s 的模擬延遲
Read timed out
場景問題
1.我們是調用方,我們不能奢望或者要求服務其它提供方的程序及時響應
我們可以通過調大客戶端的timeout
舉個例子: 聯調環境日志
[ERROR | 2025-08-07 11:02:09.058 | pms-schedule-2] [c.q.i.t.l.s.i.CommonPushServiceImpl:251] 推送巡視設備可靠性指標報錯:cn.hutool.http.HttpException: Read timed out,Read timed out?
[INFO | 2025-08-07 11:02:09.058 | pms-schedule-2] [c.q.i.t.j.PushPatrolDeviceReliability2XNJob:80] 結束巡視設備可靠性指標當前時間: 2025-08-07 11:02:09?
[ERROR | 2025-08-07 11:02:09.062 | pms-schedule-1] [c.q.i.t.l.s.i.CommonPushServiceImpl:216] 推送巡視設備在離線數據報錯:cn.hutool.http.HttpException: Read timed out,Read timed out?
[INFO | 2025-08-07 11:02:09.062 | pms-schedule-1] [c.q.i.t.j.PushPatrolDeviceStatus2XNJob:48] 結束執行推送巡視設備狀態定時任務當前時間: 2025-08-07 11:02:09?
客戶端調用服務端接口進行推送,服務端沒有及時返回,對面調試打debug 就會報出這樣的錯,和場景二 吻合
hutool 工具的異步問題怎么判斷
?? ?response = HttpUtil.createPost(patrolresultconfirmUrl).body(JSONObject.toJSONString(search)).header("Authorization", token).timeout(HTTP_TIMEOUT_MS).executeAsync();
executeAsync 這個是不是異步 循環調用兩次,如果第二次需要等第一次調用后才調用就說明是 同步的
要等第一次執行完 才調用第二次,說明上面的實現是同步,怎么改就變成異步呢如下:
CompletableFuture.supplyAsync(() -> HttpUtil.createPost(patrolresultconfirmUrl).body(JSONObject.toJSONString(search)).header("Authorization", token).timeout(HTTP_TIMEOUT_MS).executeAsync());List<CompletableFuture<com.alibaba.fastjson.JSONObject>> futures = new ArrayList<>();
for (int i = 0; i < 2; i++) {CompletableFuture<com.alibaba.fastjson.JSONObject> future = CompletableFuture.supplyAsync(() -> stationSampleService.collectionSample(stationSampleReq));futures.add(future);
}
CompletableFuture<Void> allFutures = CompletableFuture.allOf(futures.toArray(new CompletableFuture[0]));//收集所有結果
CompletableFuture<List<com.alibaba.fastjson.JSONObject>> allResultsFuture = ?allFutures.thenApply(v -> {List<com.alibaba.fastjson.JSONObject> results = new ArrayList<>();for (CompletableFuture<com.alibaba.fastjson.JSONObject> future : futures) {results.add(future.join());}return results;
});
總結
超不超時 和單筆的req ?resp 有關系,和調用多少次數沒有關系