Commit f2ff30e4652952cc31302f46249a6fa39c6f1f30
1 parent
6d7491fb
优化 error-code 加载逻辑,失败不影响启动
Showing
3 changed files
with
40 additions
and
17 deletions
yudao-framework/yudao-spring-boot-starter-biz-error-code/src/main/java/cn/iocoder/yudao/framework/errorcode/core/generator/ErrorCodeAutoGeneratorImpl.java
| ... | ... | @@ -49,8 +49,12 @@ public class ErrorCodeAutoGeneratorImpl implements ErrorCodeAutoGenerator { |
| 49 | 49 | log.info("[execute][解析到错误码数量为 ({}) 个]", autoGenerateDTOs.size()); |
| 50 | 50 | |
| 51 | 51 | // 第二步,写入到 system 服务 |
| 52 | - errorCodeApi.autoGenerateErrorCodeList(autoGenerateDTOs); | |
| 53 | - log.info("[execute][写入到 system 组件完成]"); | |
| 52 | + try { | |
| 53 | + errorCodeApi.autoGenerateErrorCodeList(autoGenerateDTOs); | |
| 54 | + log.info("[execute][写入到 system 组件完成]"); | |
| 55 | + } catch (Exception ex) { | |
| 56 | + log.error("[execute][写入到 system 组件失败({})]", ExceptionUtil.getRootCauseMessage(ex)); | |
| 57 | + } | |
| 54 | 58 | } |
| 55 | 59 | |
| 56 | 60 | /** | ... | ... |
yudao-framework/yudao-spring-boot-starter-biz-error-code/src/main/java/cn/iocoder/yudao/framework/errorcode/core/loader/ErrorCodeLoader.java
yudao-framework/yudao-spring-boot-starter-biz-error-code/src/main/java/cn/iocoder/yudao/framework/errorcode/core/loader/ErrorCodeLoaderImpl.java
| 1 | 1 | package cn.iocoder.yudao.framework.errorcode.core.loader; |
| 2 | 2 | |
| 3 | 3 | import cn.hutool.core.collection.CollUtil; |
| 4 | +import cn.hutool.core.exceptions.ExceptionUtil; | |
| 4 | 5 | import cn.iocoder.yudao.framework.common.util.date.DateUtils; |
| 5 | 6 | import cn.iocoder.yudao.module.system.api.errorcode.ErrorCodeApi; |
| 6 | 7 | import cn.iocoder.yudao.module.system.api.errorcode.dto.ErrorCodeRespDTO; |
| ... | ... | @@ -8,6 +9,7 @@ import lombok.RequiredArgsConstructor; |
| 8 | 9 | import lombok.extern.slf4j.Slf4j; |
| 9 | 10 | import org.springframework.boot.context.event.ApplicationReadyEvent; |
| 10 | 11 | import org.springframework.context.event.EventListener; |
| 12 | +import org.springframework.scheduling.annotation.Async; | |
| 11 | 13 | import org.springframework.scheduling.annotation.Scheduled; |
| 12 | 14 | |
| 13 | 15 | import java.time.LocalDateTime; |
| ... | ... | @@ -43,31 +45,38 @@ public class ErrorCodeLoaderImpl implements ErrorCodeLoader { |
| 43 | 45 | */ |
| 44 | 46 | private LocalDateTime maxUpdateTime; |
| 45 | 47 | |
| 48 | + @Override | |
| 46 | 49 | @EventListener(ApplicationReadyEvent.class) |
| 50 | + @Async // 异步,保证项目的启动过程,毕竟非关键流程 | |
| 47 | 51 | public void loadErrorCodes() { |
| 48 | - this.loadErrorCodes0(); | |
| 52 | + loadErrorCodes0(); | |
| 49 | 53 | } |
| 50 | 54 | |
| 55 | + @Override | |
| 51 | 56 | @Scheduled(fixedDelay = REFRESH_ERROR_CODE_PERIOD, initialDelay = REFRESH_ERROR_CODE_PERIOD) |
| 52 | 57 | public void refreshErrorCodes() { |
| 53 | - this.loadErrorCodes0(); | |
| 58 | + loadErrorCodes0(); | |
| 54 | 59 | } |
| 55 | 60 | |
| 56 | 61 | private void loadErrorCodes0() { |
| 57 | - // 加载错误码 | |
| 58 | - List<ErrorCodeRespDTO> errorCodeRespDTOs = errorCodeApi.getErrorCodeList(applicationName, maxUpdateTime); | |
| 59 | - if (CollUtil.isEmpty(errorCodeRespDTOs)) { | |
| 60 | - return; | |
| 61 | - } | |
| 62 | - log.info("[loadErrorCodes0][加载到 ({}) 个错误码]", errorCodeRespDTOs.size()); | |
| 62 | + try { | |
| 63 | + // 加载错误码 | |
| 64 | + List<ErrorCodeRespDTO> errorCodeRespDTOs = errorCodeApi.getErrorCodeList(applicationName, maxUpdateTime); | |
| 65 | + if (CollUtil.isEmpty(errorCodeRespDTOs)) { | |
| 66 | + return; | |
| 67 | + } | |
| 68 | + log.info("[loadErrorCodes0][加载到 ({}) 个错误码]", errorCodeRespDTOs.size()); | |
| 63 | 69 | |
| 64 | - // 刷新错误码的缓存 | |
| 65 | - errorCodeRespDTOs.forEach(errorCodeRespDTO -> { | |
| 66 | - // 写入到错误码的缓存 | |
| 67 | - putErrorCode(errorCodeRespDTO.getCode(), errorCodeRespDTO.getMessage()); | |
| 68 | - // 记录下更新时间,方便增量更新 | |
| 69 | - maxUpdateTime = DateUtils.max(maxUpdateTime, errorCodeRespDTO.getUpdateTime()); | |
| 70 | - }); | |
| 70 | + // 刷新错误码的缓存 | |
| 71 | + errorCodeRespDTOs.forEach(errorCodeRespDTO -> { | |
| 72 | + // 写入到错误码的缓存 | |
| 73 | + putErrorCode(errorCodeRespDTO.getCode(), errorCodeRespDTO.getMessage()); | |
| 74 | + // 记录下更新时间,方便增量更新 | |
| 75 | + maxUpdateTime = DateUtils.max(maxUpdateTime, errorCodeRespDTO.getUpdateTime()); | |
| 76 | + }); | |
| 77 | + } catch (Exception ex) { | |
| 78 | + log.error("[loadErrorCodes0][加载错误码失败({})]", ExceptionUtil.getRootCauseMessage(ex)); | |
| 79 | + } | |
| 71 | 80 | } |
| 72 | 81 | |
| 73 | 82 | } | ... | ... |