优化 error-code 加载逻辑,失败不影响启动

This commit is contained in:
YunaiV 2023-11-25 23:00:31 +08:00
parent 6d7491fb02
commit f2ff30e465
3 changed files with 40 additions and 17 deletions

View File

@ -49,8 +49,12 @@ public class ErrorCodeAutoGeneratorImpl implements ErrorCodeAutoGenerator {
log.info("[execute][解析到错误码数量为 ({}) 个]", autoGenerateDTOs.size()); log.info("[execute][解析到错误码数量为 ({}) 个]", autoGenerateDTOs.size());
// 第二步写入到 system 服务 // 第二步写入到 system 服务
try {
errorCodeApi.autoGenerateErrorCodeList(autoGenerateDTOs); errorCodeApi.autoGenerateErrorCodeList(autoGenerateDTOs);
log.info("[execute][写入到 system 组件完成]"); log.info("[execute][写入到 system 组件完成]");
} catch (Exception ex) {
log.error("[execute][写入到 system 组件失败({})]", ExceptionUtil.getRootCauseMessage(ex));
}
} }
/** /**

View File

@ -21,4 +21,14 @@ public interface ErrorCodeLoader {
ServiceExceptionUtil.put(code, msg); ServiceExceptionUtil.put(code, msg);
} }
/**
* 刷新错误码
*/
void refreshErrorCodes();
/**
* 加载错误码
*/
void loadErrorCodes();
} }

View File

@ -1,6 +1,7 @@
package cn.iocoder.yudao.framework.errorcode.core.loader; package cn.iocoder.yudao.framework.errorcode.core.loader;
import cn.hutool.core.collection.CollUtil; import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.exceptions.ExceptionUtil;
import cn.iocoder.yudao.framework.common.util.date.DateUtils; import cn.iocoder.yudao.framework.common.util.date.DateUtils;
import cn.iocoder.yudao.module.system.api.errorcode.ErrorCodeApi; import cn.iocoder.yudao.module.system.api.errorcode.ErrorCodeApi;
import cn.iocoder.yudao.module.system.api.errorcode.dto.ErrorCodeRespDTO; import cn.iocoder.yudao.module.system.api.errorcode.dto.ErrorCodeRespDTO;
@ -8,6 +9,7 @@ import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.context.event.ApplicationReadyEvent; import org.springframework.boot.context.event.ApplicationReadyEvent;
import org.springframework.context.event.EventListener; import org.springframework.context.event.EventListener;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.Scheduled; import org.springframework.scheduling.annotation.Scheduled;
import java.time.LocalDateTime; import java.time.LocalDateTime;
@ -43,17 +45,21 @@ public class ErrorCodeLoaderImpl implements ErrorCodeLoader {
*/ */
private LocalDateTime maxUpdateTime; private LocalDateTime maxUpdateTime;
@Override
@EventListener(ApplicationReadyEvent.class) @EventListener(ApplicationReadyEvent.class)
@Async // 异步保证项目的启动过程毕竟非关键流程
public void loadErrorCodes() { public void loadErrorCodes() {
this.loadErrorCodes0(); loadErrorCodes0();
} }
@Override
@Scheduled(fixedDelay = REFRESH_ERROR_CODE_PERIOD, initialDelay = REFRESH_ERROR_CODE_PERIOD) @Scheduled(fixedDelay = REFRESH_ERROR_CODE_PERIOD, initialDelay = REFRESH_ERROR_CODE_PERIOD)
public void refreshErrorCodes() { public void refreshErrorCodes() {
this.loadErrorCodes0(); loadErrorCodes0();
} }
private void loadErrorCodes0() { private void loadErrorCodes0() {
try {
// 加载错误码 // 加载错误码
List<ErrorCodeRespDTO> errorCodeRespDTOs = errorCodeApi.getErrorCodeList(applicationName, maxUpdateTime); List<ErrorCodeRespDTO> errorCodeRespDTOs = errorCodeApi.getErrorCodeList(applicationName, maxUpdateTime);
if (CollUtil.isEmpty(errorCodeRespDTOs)) { if (CollUtil.isEmpty(errorCodeRespDTOs)) {
@ -68,6 +74,9 @@ public class ErrorCodeLoaderImpl implements ErrorCodeLoader {
// 记录下更新时间方便增量更新 // 记录下更新时间方便增量更新
maxUpdateTime = DateUtils.max(maxUpdateTime, errorCodeRespDTO.getUpdateTime()); maxUpdateTime = DateUtils.max(maxUpdateTime, errorCodeRespDTO.getUpdateTime());
}); });
} catch (Exception ex) {
log.error("[loadErrorCodes0][加载错误码失败({})]", ExceptionUtil.getRootCauseMessage(ex));
}
} }
} }