场景一:客户端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 有关系,和调用多少次数没有关系