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,8 +49,12 @@ public class ErrorCodeAutoGeneratorImpl implements ErrorCodeAutoGenerator { | ||
| 49 | log.info("[execute][解析到错误码数量为 ({}) 个]", autoGenerateDTOs.size()); | 49 | log.info("[execute][解析到错误码数量为 ({}) 个]", autoGenerateDTOs.size()); |
| 50 | 50 | ||
| 51 | // 第二步,写入到 system 服务 | 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
| @@ -21,4 +21,14 @@ public interface ErrorCodeLoader { | @@ -21,4 +21,14 @@ public interface ErrorCodeLoader { | ||
| 21 | ServiceExceptionUtil.put(code, msg); | 21 | ServiceExceptionUtil.put(code, msg); |
| 22 | } | 22 | } |
| 23 | 23 | ||
| 24 | + /** | ||
| 25 | + * 刷新错误码 | ||
| 26 | + */ | ||
| 27 | + void refreshErrorCodes(); | ||
| 28 | + | ||
| 29 | + /** | ||
| 30 | + * 加载错误码 | ||
| 31 | + */ | ||
| 32 | + void loadErrorCodes(); | ||
| 33 | + | ||
| 24 | } | 34 | } |
yudao-framework/yudao-spring-boot-starter-biz-error-code/src/main/java/cn/iocoder/yudao/framework/errorcode/core/loader/ErrorCodeLoaderImpl.java
| 1 | package cn.iocoder.yudao.framework.errorcode.core.loader; | 1 | package cn.iocoder.yudao.framework.errorcode.core.loader; |
| 2 | 2 | ||
| 3 | import cn.hutool.core.collection.CollUtil; | 3 | import cn.hutool.core.collection.CollUtil; |
| 4 | +import cn.hutool.core.exceptions.ExceptionUtil; | ||
| 4 | import cn.iocoder.yudao.framework.common.util.date.DateUtils; | 5 | import cn.iocoder.yudao.framework.common.util.date.DateUtils; |
| 5 | import cn.iocoder.yudao.module.system.api.errorcode.ErrorCodeApi; | 6 | import cn.iocoder.yudao.module.system.api.errorcode.ErrorCodeApi; |
| 6 | import cn.iocoder.yudao.module.system.api.errorcode.dto.ErrorCodeRespDTO; | 7 | import cn.iocoder.yudao.module.system.api.errorcode.dto.ErrorCodeRespDTO; |
| @@ -8,6 +9,7 @@ import lombok.RequiredArgsConstructor; | @@ -8,6 +9,7 @@ import lombok.RequiredArgsConstructor; | ||
| 8 | import lombok.extern.slf4j.Slf4j; | 9 | import lombok.extern.slf4j.Slf4j; |
| 9 | import org.springframework.boot.context.event.ApplicationReadyEvent; | 10 | import org.springframework.boot.context.event.ApplicationReadyEvent; |
| 10 | import org.springframework.context.event.EventListener; | 11 | import org.springframework.context.event.EventListener; |
| 12 | +import org.springframework.scheduling.annotation.Async; | ||
| 11 | import org.springframework.scheduling.annotation.Scheduled; | 13 | import org.springframework.scheduling.annotation.Scheduled; |
| 12 | 14 | ||
| 13 | import java.time.LocalDateTime; | 15 | import java.time.LocalDateTime; |
| @@ -43,31 +45,38 @@ public class ErrorCodeLoaderImpl implements ErrorCodeLoader { | @@ -43,31 +45,38 @@ public class ErrorCodeLoaderImpl implements ErrorCodeLoader { | ||
| 43 | */ | 45 | */ |
| 44 | private LocalDateTime maxUpdateTime; | 46 | private LocalDateTime maxUpdateTime; |
| 45 | 47 | ||
| 48 | + @Override | ||
| 46 | @EventListener(ApplicationReadyEvent.class) | 49 | @EventListener(ApplicationReadyEvent.class) |
| 50 | + @Async // 异步,保证项目的启动过程,毕竟非关键流程 | ||
| 47 | public void loadErrorCodes() { | 51 | public void loadErrorCodes() { |
| 48 | - this.loadErrorCodes0(); | 52 | + loadErrorCodes0(); |
| 49 | } | 53 | } |
| 50 | 54 | ||
| 55 | + @Override | ||
| 51 | @Scheduled(fixedDelay = REFRESH_ERROR_CODE_PERIOD, initialDelay = REFRESH_ERROR_CODE_PERIOD) | 56 | @Scheduled(fixedDelay = REFRESH_ERROR_CODE_PERIOD, initialDelay = REFRESH_ERROR_CODE_PERIOD) |
| 52 | public void refreshErrorCodes() { | 57 | public void refreshErrorCodes() { |
| 53 | - this.loadErrorCodes0(); | 58 | + loadErrorCodes0(); |
| 54 | } | 59 | } |
| 55 | 60 | ||
| 56 | private void loadErrorCodes0() { | 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 | } |