1、error-code写入时机新增重试;2、重复error-code错误码处理;

This commit is contained in:
gaoqr
2025-07-17 15:01:17 +08:00
parent 641348c59b
commit 66b42db3c6
4 changed files with 68 additions and 35 deletions
@@ -13,6 +13,7 @@ import org.springframework.boot.context.event.ApplicationReadyEvent;
import org.springframework.context.event.EventListener;
import org.springframework.scheduling.annotation.Async;
import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
@@ -44,16 +45,52 @@ public class ErrorCodeAutoGeneratorImpl implements ErrorCodeAutoGenerator {
@EventListener(ApplicationReadyEvent.class)
@Async // 异步,保证项目的启动过程,毕竟非关键流程
public void execute() {
// 第一步,解析错误码
List<ErrorCodeAutoGenerateReqDTO> autoGenerateDTOs = parseErrorCode();
log.info("[execute][解析到错误码数量为 ({}) 个]", autoGenerateDTOs.size());
// 第二步,写入到 system 服务
try {
errorCodeApi.autoGenerateErrorCodeList(autoGenerateDTOs).checkError();
log.info("[execute][写入到 system 组件完成]");
} catch (Exception ex) {
log.error("[execute][写入到 system 组件失败({})]", ExceptionUtil.getRootCauseMessage(ex));
// 手动重试配置
int maxAttempts = 5;
int attempt = 0;
boolean success = false;
long initialDelay = 1000; // 初始延迟1秒
long maxDelay = 10000; // 最大延迟10秒
while (attempt < maxAttempts && !success) {
attempt++;
try {
errorCodeApi.autoGenerateErrorCodeList(autoGenerateDTOs).checkError();
log.info("[execute][第 {} 次尝试 - 写入到 system 组件成功]", attempt);
success = true;
} catch (Exception ex) {
// 特别处理UnknownHostException
if (ExceptionUtil.getRootCause(ex) instanceof UnknownHostException) {
log.warn("[execute][第 {} 次尝试 - DNS解析失败,等待重试]", attempt);
} else {
log.warn("[execute][第 {} 次尝试 - 请求失败({})]",
attempt, ExceptionUtil.getRootCauseMessage(ex));
}
if (attempt >= maxAttempts) {
log.error("[execute][写入到 system 组件失败,已达到最大重试次数({})]",
ExceptionUtil.getRootCauseMessage(ex));
break;
}
// 每次重试间隔时间指数增长:1s, 2s, 4s, 8s, 10s(最大10s
long delay = Math.min(
(long) (initialDelay * Math.pow(2, attempt - 1)),
maxDelay
);
try {
log.info("[execute][等待 {}ms 后进行第 {} 次重试]", delay, attempt + 1);
Thread.sleep(delay);
} catch (InterruptedException ie) {
Thread.currentThread().interrupt();
log.error("[execute][重试过程被中断]");
break;
}
}
}
}