Commit 2fb10394 by 黄明步

更新

parent b03307c1
......@@ -99,7 +99,7 @@
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-all</artifactId>
<version>5.8.18</version>
<version>5.8.24</version>
</dependency>
<!-- 解决ModelAndView无法访问到templates目录下的页面 -->
......
......@@ -9,10 +9,7 @@ import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.ObjUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.extra.servlet.ServletUtil;
import cn.hutool.http.ContentType;
import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpResponse;
import cn.hutool.http.HttpUtil;
import cn.hutool.http.*;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.alibaba.fastjson.JSONArray;
......@@ -107,6 +104,11 @@ public class RetransmissionService {
**/
private static final String SERVER_REQUEST_TASK = "server-request-task-";
private static final String LOCK_NAME = "server-task";
/**
* 重试次数
*/
private static final int MAX_RETRIES = 3;
@PostConstruct
......@@ -178,24 +180,43 @@ public class RetransmissionService {
file.transferTo(tempFile);
paramMap.put("file", tempFile);
paramMap.put("createOfd", createOfd);
// 设置开始时间
long startTime = System.currentTimeMillis();
long endTime = System.currentTimeMillis();
String res = "";
int retryCount = 0;
// 循环重试
while (retryCount < MAX_RETRIES) {
// 设置开始时间
startTime = System.currentTimeMillis();
ocrServerAddress = getServerAddressByRequestCount();
if (ocrServerAddress == null) {
return OcrResult.error("OCR云平台未配置服务器信息");
log.error("OCR云平台未配置服务器信息");
return new OcrResult("007500", "识别失败,请重试", null, DateUtil.date());
}
addServerRequestCount(ocrServerAddress.getIp(), 1);
String ocrApi = "http://" + ocrServerAddress.getIp() + ":" + ocrServerAddress.getOcrPort();
// 开始阶段,设置请求开始时间
long startTime = System.currentTimeMillis();
this.setRequestToRedis(1, startTime, null, null, ocrServerAddress, request);
String res = HttpRequest.post(ocrApi + "/ofs/api/sync/v1/" + code)
.header("Authorization", request.getHeader("Authorization"))
.header("X-Timestamp", request.getHeader("X-Timestamp"))
.header("X-Checksum", request.getHeader("X-Checksum"))
.form(paramMap).execute().body();
// 记录请求结束时间
long endTime = System.currentTimeMillis();
String ocrApi = "http://" + ocrServerAddress.getIp() + ":" + ocrServerAddress.getOcrPort();
try {
res = HttpRequest.post(ocrApi + "/ofs/api/sync/v1/1001")
.headerMap(getHeaderMap(request), true)
.form(paramMap)
.execute().body();
// 设置结束时间
endTime = System.currentTimeMillis();
// 拿到结果后终止循环
if (StringUtils.isNotEmpty(res)) {
break;
}
} catch (HttpException e) {
addServerRequestCount(ocrServerAddress.getIp(), -1);
retryCount++;
log.error("OCR云平台接口异常, 准备重试 fileId={}, serverIp={}, 当前重试次数={}", fileId, ocrServerAddress.getIp(), retryCount, e);
}
}
if (StringUtils.isEmpty(res)) {
this.setRequestToRedis(2, startTime, endTime, null, ocrServerAddress, request);
......@@ -207,7 +228,7 @@ public class RetransmissionService {
saveToAppAbilityRecordAll(result, request, ocrServerAddress.getIp(), fileId);
return result;
} catch (Exception e) {
log.error("OCR云平台接口异常", e);
log.error("OCR云平台接口异常, fileId={}, serverIp={}", fileId, ocrServerAddress.getIp(), e);
return new OcrResult("007500", "识别失败,请重试", null, DateUtil.date());
} finally {
FileUtil.del(tempSaveDir);
......@@ -217,6 +238,36 @@ public class RetransmissionService {
}
}
public Map<String, String> getHeaderMap(HttpServletRequest request) {
Map<String, String> headerMap = new HashMap<>();
headerMap.put("Authorization", request.getHeader("Authorization"));
headerMap.put("X-Timestamp", request.getHeader("X-Timestamp"));
headerMap.put("X-Checksum", request.getHeader("X-Checksum"));
return headerMap;
}
/**
* 根据文件大小计算超时时间
* @param fileSizeBytes 文件大小,单位:字节
* @return
*/
private static int calculateTimeout(long fileSizeBytes) {
// 每1 MB 增加 3 秒超时时间
int baseTimeout = 120;
int timeoutIncrement = 3;
// 对小于等于1 MB的文件,超时时间固定为10秒
if (fileSizeBytes <= 1024 * 1024) {
return baseTimeout * 1000;
}
// 对大于1 MB的文件,每增加1 MB增加3秒超时时间
int additionalTimeout = (int) ((fileSizeBytes - 1024 * 1024) / (1024 * 1024) * timeoutIncrement);
int timeout = (baseTimeout + additionalTimeout) * 1000;
return timeout;
}
public OcrResult commonRecognitionV2(String code, String fileId, MultipartFile file, Boolean createOfd, HttpServletRequest request) {
HashMap<String, Object> paramMap = new HashMap<>();
paramMap.put("fileId", fileId);
......@@ -228,24 +279,43 @@ public class RetransmissionService {
file.transferTo(tempFile);
paramMap.put("file", tempFile);
paramMap.put("createOfd", createOfd);
// 设置开始时间
long startTime = System.currentTimeMillis();
long endTime = System.currentTimeMillis();
String res = "";
int retryCount = 0;
// 循环重试
while (retryCount < MAX_RETRIES) {
// 设置开始时间
startTime = System.currentTimeMillis();
ocrServerAddress = getServerAddressByRequestCount();
if (ocrServerAddress == null) {
return OcrResult.error("OCR云平台未配置服务器信息");
log.error("OCR云平台未配置服务器信息");
return new OcrResult("007500", "识别失败,请重试", null, DateUtil.date());
}
addServerRequestCount(ocrServerAddress.getIp(), 1);
String ocrApi = "http://" + ocrServerAddress.getIp() + ":" + ocrServerAddress.getOcrPort();
// 开始阶段,设置请求开始时间
long startTime = System.currentTimeMillis();
this.setRequestToRedis(1, startTime, null, null, ocrServerAddress, request);
String res = HttpRequest.post(ocrApi + "/ofs/api/sync/v2/" + code)
.header("Authorization", request.getHeader("Authorization"))
.header("X-Timestamp", request.getHeader("X-Timestamp"))
.header("X-Checksum", request.getHeader("X-Checksum"))
.form(paramMap).execute().body();
// 记录请求结束时间
long endTime = System.currentTimeMillis();
String ocrApi = "http://" + ocrServerAddress.getIp() + ":" + ocrServerAddress.getOcrPort();
try {
res = HttpRequest.post(ocrApi + "/ofs/api/sync/v2/1001")
.headerMap(getHeaderMap(request), true)
.form(paramMap)
.execute().body();
// 设置结束时间
endTime = System.currentTimeMillis();
// 拿到结果后终止循环
if (StringUtils.isNotEmpty(res)) {
break;
}
} catch (HttpException e) {
addServerRequestCount(ocrServerAddress.getIp(), -1);
retryCount++;
log.error("OCR云平台接口异常, 准备重试 fileId={}, serverIp={}, 当前重试次数={}", fileId, ocrServerAddress.getIp(), retryCount, e);
}
}
if (StringUtils.isEmpty(res)) {
this.setRequestToRedis(2, startTime, endTime, null, ocrServerAddress, request);
......@@ -257,7 +327,7 @@ public class RetransmissionService {
saveToAppAbilityRecordAll(result, request, ocrServerAddress.getIp(), fileId);
return result;
} catch (Exception e) {
log.error("OCR云平台接口异常, ", e);
log.error("OCR云平台接口异常, fileId={}, serverIp={}, tempPath={}", fileId, ocrServerAddress.getIp(), tempSaveDir, e);
return new OcrResult("007500", "识别失败,请重试", null, DateUtil.date());
} finally {
FileUtil.del(tempSaveDir);
......@@ -277,9 +347,7 @@ public class RetransmissionService {
}
String ocrApi = "http://" + ocrServerAddress.getIp() + ":" + ocrServerAddress.getOcrPort();
HttpResponse httpResponse = HttpRequest.post(ocrApi + "/ofs/api/sync/v1/pdf")
.header("Authorization", request.getHeader("Authorization"))
.header("X-Timestamp", request.getHeader("X-Timestamp"))
.header("X-Checksum", request.getHeader("X-Checksum"))
.headerMap(getHeaderMap(request), true)
.contentType("application/json;charset=UTF-8")
.body(body.toJSONString(0)).execute();
......@@ -353,9 +421,7 @@ public class RetransmissionService {
}
String ocrApi = "http://" + ocrServerAddress.getIp() + ":" + ocrServerAddress.getOcrPort();
HttpResponse httpResponse = HttpRequest.post(ocrApi + "/ofs/api/sync/v1/ofd")
.header("Authorization", request.getHeader("Authorization"))
.header("X-Timestamp", request.getHeader("X-Timestamp"))
.header("X-Checksum", request.getHeader("X-Checksum"))
.headerMap(getHeaderMap(request), true)
.contentType("application/json;charset=UTF-8")
.body(body.toJSONString(0)).execute();
Map<String, List<String>> headers = httpResponse.headers();
......@@ -417,35 +483,55 @@ public class RetransmissionService {
}
}
public OcrResult fullTextRecognition(String fileId, MultipartFile file, HttpServletRequest request) {
HashMap<String, Object> paramMap = new HashMap<>();
paramMap.put("fileId", fileId);
String tempSaveDir = tempDir + File.separator + IdUtil.fastSimpleUUID() + "_" + System.currentTimeMillis();
FileUtil.mkdir(tempSaveDir);
File tempFile = new File(tempSaveDir, Objects.requireNonNull(FileUtil.getName(file.getOriginalFilename())));
ServerInfo ocrServerAddress = getServerAddressByRequestCount();
ServerInfo ocrServerAddress = null;
try {
file.transferTo(tempFile);
paramMap.put("file", tempFile);
// 设置开始时间
long startTime = System.currentTimeMillis();
long endTime = System.currentTimeMillis();
String res = "";
int retryCount = 0;
// 循环重试
while (retryCount < MAX_RETRIES) {
// 设置开始时间
startTime = System.currentTimeMillis();
ocrServerAddress = getServerAddressByRequestCount();
if (ocrServerAddress == null) {
return OcrResult.error("OCR云平台未配置服务器信息");
log.error("OCR云平台未配置服务器信息");
return new OcrResult("007500", "识别失败,请重试", null, DateUtil.date());
}
addServerRequestCount(ocrServerAddress.getIp(), 1);
String ocrApi = "http://" + ocrServerAddress.getIp() + ":" + ocrServerAddress.getOcrPort();
// 设置开始时间
long startTime = System.currentTimeMillis();
this.setRequestToRedis(1, startTime, null, null, ocrServerAddress, request);
String res = HttpRequest.post(ocrApi + "/ofs/api/sync/v1/ft/1001")
.header("Authorization", request.getHeader("Authorization"))
.header("X-Timestamp", request.getHeader("X-Timestamp"))
.header("X-Checksum", request.getHeader("X-Checksum"))
.form(paramMap).execute().body();
String ocrApi = "http://" + ocrServerAddress.getIp() + ":" + ocrServerAddress.getOcrPort();
try {
res = HttpRequest.post(ocrApi + "/ofs/api/sync/v1/ft/1001")
.headerMap(getHeaderMap(request), true)
.form(paramMap)
.execute().body();
// 设置结束时间
long endTime = System.currentTimeMillis();
endTime = System.currentTimeMillis();
// 拿到结果后终止循环
if (StringUtils.isNotEmpty(res)) {
break;
}
} catch (HttpException e) {
addServerRequestCount(ocrServerAddress.getIp(), -1);
retryCount++;
log.error("OCR云平台接口异常, 准备重试 fileId={}, serverIp={}, 当前重试次数={}", fileId, ocrServerAddress.getIp(), retryCount, e);
}
}
if (StringUtils.isEmpty(res)) {
this.setRequestToRedis(2, startTime, endTime, null, ocrServerAddress, request);
......@@ -457,7 +543,7 @@ public class RetransmissionService {
saveToAppAbilityRecordAll(result, request, ocrServerAddress.getIp(), fileId);
return result;
} catch (IOException e) {
log.error("OCR云平台接口异常", e);
log.error("OCR云平台接口异常, fileId={}, serverIp={}", fileId, ocrServerAddress.getIp(), e);
return new OcrResult("007500", "识别失败,请重试", null, DateUtil.date());
} finally {
FileUtil.del(tempSaveDir);
......@@ -479,24 +565,43 @@ public class RetransmissionService {
paramMap.put("file", tempFile);
// 设置开始时间
long startTime = System.currentTimeMillis();
long endTime = System.currentTimeMillis();
String res = "";
int retryCount = 0;
// 循环重试
while (retryCount < MAX_RETRIES) {
// 设置开始时间
startTime = System.currentTimeMillis();
ocrServerAddress = getServerAddressByRequestCount();
if (ocrServerAddress == null) {
log.error("OCR云平台未配置服务器信息");
return new OcrResult("007500", "识别失败,请重试", null, DateUtil.date());
}
addServerRequestCount(ocrServerAddress.getIp(), 1);
String ocrApi = "http://" + ocrServerAddress.getIp() + ":" + ocrServerAddress.getOcrPort();
// 设置开始时间
long startTime = System.currentTimeMillis();
this.setRequestToRedis(1, startTime, null, null, ocrServerAddress, request);
String res = HttpRequest.post(ocrApi + "/ofs/api/sync/v2/ft/1001")
.header("Authorization", request.getHeader("Authorization"))
.header("X-Timestamp", request.getHeader("X-Timestamp"))
.header("X-Checksum", request.getHeader("X-Checksum"))
.form(paramMap).execute().body();
String ocrApi = "http://" + ocrServerAddress.getIp() + ":" + ocrServerAddress.getOcrPort();
try {
res = HttpRequest.post(ocrApi + "/ofs/api/sync/v2/1001")
.headerMap(getHeaderMap(request), true)
.form(paramMap)
.execute().body();
// 设置结束时间
long endTime = System.currentTimeMillis();
endTime = System.currentTimeMillis();
// 拿到结果后终止循环
if (StringUtils.isNotEmpty(res)) {
break;
}
} catch (HttpException e) {
addServerRequestCount(ocrServerAddress.getIp(), -1);
retryCount++;
log.error("OCR云平台接口异常, 准备重试 fileId={}, serverIp={}, 当前重试次数={}", fileId, ocrServerAddress.getIp(), retryCount);
}
}
if (StringUtils.isEmpty(res)) {
this.setRequestToRedis(2, startTime, endTime, null, ocrServerAddress, request);
......@@ -508,7 +613,7 @@ public class RetransmissionService {
saveToAppAbilityRecordAll(result, request, ocrServerAddress.getIp(), fileId);
return result;
} catch (IOException e) {
log.error("OCR云平台接口异常", e);
log.error("OCR云平台接口异常, fileId={}, serverIp={}", fileId, ocrServerAddress.getIp(), e);
return new OcrResult("007500", "识别失败,请重试", null, DateUtil.date());
} finally {
FileUtil.del(tempSaveDir);
......@@ -540,10 +645,9 @@ public class RetransmissionService {
long startTime = System.currentTimeMillis();
this.setRequestToRedis(1, startTime, null, null, ocrServerAddress, request);
String res = HttpRequest.post(ocrApi + "/ofs/api/sync/v1/kv/" + code)
.header("Authorization", request.getHeader("Authorization"))
.header("X-Timestamp", request.getHeader("X-Timestamp"))
.header("X-Checksum", request.getHeader("X-Checksum"))
.form(paramMap).execute().body();
.headerMap(getHeaderMap(request), true)
.form(paramMap)
.execute().body();
// 设置结束时间
long endTime = System.currentTimeMillis();
......@@ -557,7 +661,7 @@ public class RetransmissionService {
saveToAppAbilityRecordAll(result, request, ocrServerAddress.getIp(), fileId);
return result;
} catch (IOException e) {
log.error("OCR云平台接口异常", e);
log.error("OCR云平台接口异常, fileId={}, serverIp={}", fileId, ocrServerAddress.getIp(), e);
return new OcrResult("007500", "识别失败,请重试", null, DateUtil.date());
} finally {
FileUtil.del(tempSaveDir);
......@@ -588,9 +692,7 @@ public class RetransmissionService {
long startTime = System.currentTimeMillis();
this.setRequestToRedis(1, startTime, null, null, ocrServerAddress, request);
String res = HttpRequest.post(ocrApi + "/ofs/api/async/minio/v1/" + code)
.header("Authorization", request.getHeader("Authorization"))
.header("X-Timestamp", request.getHeader("X-Timestamp"))
.header("X-Checksum", request.getHeader("X-Checksum"))
.headerMap(getHeaderMap(request), true)
.contentType("application/json;charset=UTF-8")
.body(body.toJSONString(0)).execute().body();
// 设置结束时间
......@@ -632,10 +734,9 @@ public class RetransmissionService {
long startTime = System.currentTimeMillis();
this.setRequestToRedis(1, startTime, null, null, ocrServerAddress, request);
String res = HttpRequest.post(ocrApi + "/ofs/api/sync/v1/6002")
.header("Authorization", request.getHeader("Authorization"))
.header("X-Timestamp", request.getHeader("X-Timestamp"))
.header("X-Checksum", request.getHeader("X-Checksum"))
.form(paramMap).execute().body();
.headerMap(getHeaderMap(request), true)
.form(paramMap)
.execute().body();
// 设置结束时间
long endTime = System.currentTimeMillis();
......@@ -650,7 +751,7 @@ public class RetransmissionService {
saveToAppAbilityRecordAll(result, request, ocrServerAddress.getIp(), fileId);
return result;
} catch (Exception e) {
log.error("OCR云平台接口异常", e);
log.error("OCR云平台接口异常, fileId={}, serverIp={}", fileId, ocrServerAddress.getIp(), e);
return new OcrResult("007500", "识别失败,请重试", null, DateUtil.date());
} finally {
FileUtil.del(tempSaveDir);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment