mirror of
				https://gitee.com/hhyykk/ipms-sjy.git
				synced 2025-11-01 02:38:43 +08:00 
			
		
		
		
	【移除】错误码的管理,简化项目的复杂度
This commit is contained in:
		| @@ -1,30 +0,0 @@ | ||||
| package cn.iocoder.yudao.framework.errorcode.config; | ||||
|  | ||||
| import lombok.Data; | ||||
| import org.springframework.boot.context.properties.ConfigurationProperties; | ||||
| import org.springframework.validation.annotation.Validated; | ||||
|  | ||||
| import jakarta.validation.constraints.NotNull; | ||||
| import java.util.List; | ||||
|  | ||||
| /** | ||||
|  * 错误码的配置属性类 | ||||
|  * | ||||
|  * @author dlyan | ||||
|  */ | ||||
| @ConfigurationProperties("yudao.error-code") | ||||
| @Data | ||||
| @Validated | ||||
| public class ErrorCodeProperties { | ||||
|  | ||||
|     /** | ||||
|      * 是否开启 | ||||
|      */ | ||||
|     private Boolean enable = true; | ||||
|     /** | ||||
|      * 错误码枚举类 | ||||
|      */ | ||||
|     @NotNull(message = "错误码枚举类不能为空") | ||||
|     private List<String> constantsClassList; | ||||
|  | ||||
| } | ||||
| @@ -1,39 +0,0 @@ | ||||
| package cn.iocoder.yudao.framework.errorcode.config; | ||||
|  | ||||
| import cn.iocoder.yudao.framework.errorcode.core.generator.ErrorCodeAutoGenerator; | ||||
| import cn.iocoder.yudao.framework.errorcode.core.generator.ErrorCodeAutoGeneratorImpl; | ||||
| import cn.iocoder.yudao.framework.errorcode.core.loader.ErrorCodeLoader; | ||||
| import cn.iocoder.yudao.framework.errorcode.core.loader.ErrorCodeLoaderImpl; | ||||
| import cn.iocoder.yudao.module.system.api.errorcode.ErrorCodeApi; | ||||
| import org.springframework.beans.factory.annotation.Value; | ||||
| import org.springframework.boot.autoconfigure.AutoConfiguration; | ||||
| import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; | ||||
| import org.springframework.boot.context.properties.EnableConfigurationProperties; | ||||
| import org.springframework.context.annotation.Bean; | ||||
| import org.springframework.scheduling.annotation.EnableScheduling; | ||||
|  | ||||
| /** | ||||
|  * 错误码配置类 | ||||
|  * | ||||
|  * @author 芋道源码 | ||||
|  */ | ||||
| @AutoConfiguration | ||||
| @ConditionalOnProperty(prefix = "yudao.error-code", value = "enable", matchIfMissing = true) // 允许使用 yudao.error-code.enable=false 禁用访问日志 | ||||
| @EnableConfigurationProperties(ErrorCodeProperties.class) | ||||
| @EnableScheduling // 开启调度任务的功能,因为 ErrorCodeRemoteLoader 通过定时刷新错误码 | ||||
| public class YudaoErrorCodeAutoConfiguration { | ||||
|  | ||||
|     @Bean | ||||
|     public ErrorCodeAutoGenerator errorCodeAutoGenerator(@Value("${spring.application.name}") String applicationName, | ||||
|                                                          ErrorCodeProperties errorCodeProperties, | ||||
|                                                          ErrorCodeApi errorCodeApi) { | ||||
|         return new ErrorCodeAutoGeneratorImpl(applicationName, errorCodeProperties.getConstantsClassList(), errorCodeApi); | ||||
|     } | ||||
|  | ||||
|     @Bean | ||||
|     public ErrorCodeLoader errorCodeLoader(@Value("${spring.application.name}") String applicationName, | ||||
|                                            ErrorCodeApi errorCodeApi) { | ||||
|         return new ErrorCodeLoaderImpl(applicationName, errorCodeApi); | ||||
|     } | ||||
|  | ||||
| } | ||||
| @@ -1,15 +0,0 @@ | ||||
| package cn.iocoder.yudao.framework.errorcode.core.generator; | ||||
|  | ||||
| /** | ||||
|  * 错误码的自动生成器 | ||||
|  * | ||||
|  * @author dylan | ||||
|  */ | ||||
| public interface ErrorCodeAutoGenerator { | ||||
|  | ||||
|     /** | ||||
|      * 将配置类到错误码写入数据库 | ||||
|      */ | ||||
|     void execute(); | ||||
|  | ||||
| } | ||||
| @@ -1,108 +0,0 @@ | ||||
| package cn.iocoder.yudao.framework.errorcode.core.generator; | ||||
|  | ||||
| import cn.hutool.core.collection.CollUtil; | ||||
| import cn.hutool.core.exceptions.ExceptionUtil; | ||||
| import cn.hutool.core.util.ClassUtil; | ||||
| import cn.hutool.core.util.ReflectUtil; | ||||
| import cn.iocoder.yudao.framework.common.exception.ErrorCode; | ||||
| import cn.iocoder.yudao.module.system.api.errorcode.ErrorCodeApi; | ||||
| import cn.iocoder.yudao.module.system.api.errorcode.dto.ErrorCodeAutoGenerateReqDTO; | ||||
| import lombok.RequiredArgsConstructor; | ||||
| import lombok.extern.slf4j.Slf4j; | ||||
| import org.springframework.boot.context.event.ApplicationReadyEvent; | ||||
| import org.springframework.context.event.EventListener; | ||||
| import org.springframework.scheduling.annotation.Async; | ||||
|  | ||||
| import java.util.ArrayList; | ||||
| import java.util.Arrays; | ||||
| import java.util.List; | ||||
|  | ||||
| /** | ||||
|  * ErrorCodeAutoGenerator 的实现类 | ||||
|  * 目的是,扫描指定的 {@link #constantsClassList} 类,写入到 system 服务中 | ||||
|  * | ||||
|  * @author dylan | ||||
|  */ | ||||
| @RequiredArgsConstructor | ||||
| @Slf4j | ||||
| public class ErrorCodeAutoGeneratorImpl implements ErrorCodeAutoGenerator { | ||||
|  | ||||
|     /** | ||||
|      * 应用分组 | ||||
|      */ | ||||
|     private final String applicationName; | ||||
|     /** | ||||
|      * 错误码枚举类 | ||||
|      */ | ||||
|     private final List<String> constantsClassList; | ||||
|     /** | ||||
|      * 错误码 Api | ||||
|      */ | ||||
|     private final ErrorCodeApi errorCodeApi; | ||||
|  | ||||
|     @Override | ||||
|     @EventListener(ApplicationReadyEvent.class) | ||||
|     @Async // 异步,保证项目的启动过程,毕竟非关键流程 | ||||
|     public void execute() { | ||||
|         // 第一步,解析错误码 | ||||
|         List<ErrorCodeAutoGenerateReqDTO> autoGenerateDTOs = parseErrorCode(); | ||||
|         log.info("[execute][解析到错误码数量为 ({}) 个]", autoGenerateDTOs.size()); | ||||
|  | ||||
|         // 第二步,写入到 system 服务 | ||||
|         try { | ||||
|             errorCodeApi.autoGenerateErrorCodeList(autoGenerateDTOs); | ||||
|             log.info("[execute][写入到 system 组件完成]"); | ||||
|         } catch (Exception ex) { | ||||
|             log.error("[execute][写入到 system 组件失败({})]", ExceptionUtil.getRootCauseMessage(ex)); | ||||
|         } | ||||
|     } | ||||
|  | ||||
|     /** | ||||
|      * 解析 constantsClassList 变量,转换成错误码数组 | ||||
|      * | ||||
|      * @return 错误码数组 | ||||
|      */ | ||||
|     private List<ErrorCodeAutoGenerateReqDTO> parseErrorCode() { | ||||
|         // 校验 errorCodeConstantsClass 参数 | ||||
|         if (CollUtil.isEmpty(constantsClassList)) { | ||||
|             log.info("[execute][未配置 yudao.error-code.constants-class-list 配置项,不进行自动写入到 system 服务中]"); | ||||
|             return new ArrayList<>(); | ||||
|         } | ||||
|  | ||||
|         // 解析错误码 | ||||
|         List<ErrorCodeAutoGenerateReqDTO> autoGenerateDTOs = new ArrayList<>(); | ||||
|         constantsClassList.forEach(constantsClass -> { | ||||
|             try { | ||||
|                 // 解析错误码枚举类 | ||||
|                 Class<?> errorCodeConstantsClazz = ClassUtil.loadClass(constantsClass); | ||||
|                 // 解析错误码 | ||||
|                 autoGenerateDTOs.addAll(parseErrorCode(errorCodeConstantsClazz)); | ||||
|             } catch (Exception ex) { | ||||
|                 log.warn("[parseErrorCode][constantsClass({}) 加载失败({})]", constantsClass, | ||||
|                         ExceptionUtil.getRootCauseMessage(ex)); | ||||
|             } | ||||
|         }); | ||||
|         return autoGenerateDTOs; | ||||
|     } | ||||
|  | ||||
|     /** | ||||
|      * 解析错误码类,获得错误码数组 | ||||
|      * | ||||
|      * @return 错误码数组 | ||||
|      */ | ||||
|     private List<ErrorCodeAutoGenerateReqDTO> parseErrorCode(Class<?> constantsClass) { | ||||
|         List<ErrorCodeAutoGenerateReqDTO> autoGenerateDTOs = new ArrayList<>(); | ||||
|         Arrays.stream(constantsClass.getFields()).forEach(field -> { | ||||
|             if (field.getType() != ErrorCode.class) { | ||||
|                 return; | ||||
|             } | ||||
|             // 转换成 ErrorCodeAutoGenerateReqDTO 对象 | ||||
|             ErrorCode errorCode = (ErrorCode) ReflectUtil.getFieldValue(constantsClass, field); | ||||
|             autoGenerateDTOs.add(new ErrorCodeAutoGenerateReqDTO().setApplicationName(applicationName) | ||||
|                     .setCode(errorCode.getCode()).setMessage(errorCode.getMsg())); | ||||
|         }); | ||||
|         return autoGenerateDTOs; | ||||
|     } | ||||
|  | ||||
| } | ||||
|  | ||||
| @@ -1,34 +0,0 @@ | ||||
| package cn.iocoder.yudao.framework.errorcode.core.loader; | ||||
|  | ||||
| import cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil; | ||||
|  | ||||
| /** | ||||
|  * 错误码加载器 | ||||
|  * | ||||
|  * 注意,错误码最终加载到 {@link ServiceExceptionUtil} 的 MESSAGES 变量中! | ||||
|  * | ||||
|  * @author dlyan | ||||
|  */ | ||||
| public interface ErrorCodeLoader { | ||||
|  | ||||
|     /** | ||||
|      * 添加错误码 | ||||
|      * | ||||
|      * @param code 错误码的编号 | ||||
|      * @param msg 错误码的提示 | ||||
|      */ | ||||
|     default void putErrorCode(Integer code, String msg) { | ||||
|         ServiceExceptionUtil.put(code, msg); | ||||
|     } | ||||
|  | ||||
|     /** | ||||
|      * 刷新错误码 | ||||
|      */ | ||||
|     void refreshErrorCodes(); | ||||
|  | ||||
|     /** | ||||
|      * 加载错误码 | ||||
|      */ | ||||
|     void loadErrorCodes(); | ||||
|  | ||||
| } | ||||
| @@ -1,82 +0,0 @@ | ||||
| package cn.iocoder.yudao.framework.errorcode.core.loader; | ||||
|  | ||||
| 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.module.system.api.errorcode.ErrorCodeApi; | ||||
| import cn.iocoder.yudao.module.system.api.errorcode.dto.ErrorCodeRespDTO; | ||||
| import lombok.RequiredArgsConstructor; | ||||
| import lombok.extern.slf4j.Slf4j; | ||||
| import org.springframework.boot.context.event.ApplicationReadyEvent; | ||||
| import org.springframework.context.event.EventListener; | ||||
| import org.springframework.scheduling.annotation.Async; | ||||
| import org.springframework.scheduling.annotation.Scheduled; | ||||
|  | ||||
| import java.time.LocalDateTime; | ||||
| import java.util.List; | ||||
|  | ||||
| /** | ||||
|  * ErrorCodeLoader 的实现类,从 infra 的数据库中,加载错误码。 | ||||
|  * | ||||
|  * 考虑到错误码会刷新,所以按照 {@link #REFRESH_ERROR_CODE_PERIOD} 频率,增量加载错误码。 | ||||
|  * | ||||
|  * @author dlyan | ||||
|  */ | ||||
| @RequiredArgsConstructor | ||||
| @Slf4j | ||||
| public class ErrorCodeLoaderImpl implements ErrorCodeLoader { | ||||
|  | ||||
|     /** | ||||
|      * 刷新错误码的频率,单位:毫秒 | ||||
|      */ | ||||
|     private static final int REFRESH_ERROR_CODE_PERIOD = 60 * 1000; | ||||
|  | ||||
|     /** | ||||
|      * 应用分组 | ||||
|      */ | ||||
|     private final String applicationName; | ||||
|     /** | ||||
|      * 错误码 Api | ||||
|      */ | ||||
|     private final ErrorCodeApi errorCodeApi; | ||||
|  | ||||
|     /** | ||||
|      * 缓存错误码的最大更新时间,用于后续的增量轮询,判断是否有更新 | ||||
|      */ | ||||
|     private LocalDateTime maxUpdateTime; | ||||
|  | ||||
|     @Override | ||||
|     @EventListener(ApplicationReadyEvent.class) | ||||
|     @Async // 异步,保证项目的启动过程,毕竟非关键流程 | ||||
|     public void loadErrorCodes() { | ||||
|         loadErrorCodes0(); | ||||
|     } | ||||
|  | ||||
|     @Override | ||||
|     @Scheduled(fixedDelay = REFRESH_ERROR_CODE_PERIOD, initialDelay = REFRESH_ERROR_CODE_PERIOD) | ||||
|     public void refreshErrorCodes() { | ||||
|         loadErrorCodes0(); | ||||
|     } | ||||
|  | ||||
|     private void loadErrorCodes0() { | ||||
|         try { | ||||
|             // 加载错误码 | ||||
|             List<ErrorCodeRespDTO> errorCodeRespDTOs = errorCodeApi.getErrorCodeList(applicationName, maxUpdateTime); | ||||
|             if (CollUtil.isEmpty(errorCodeRespDTOs)) { | ||||
|                 return; | ||||
|             } | ||||
|             log.info("[loadErrorCodes0][加载到 ({}) 个错误码]", errorCodeRespDTOs.size()); | ||||
|  | ||||
|             // 刷新错误码的缓存 | ||||
|             errorCodeRespDTOs.forEach(errorCodeRespDTO -> { | ||||
|                 // 写入到错误码的缓存 | ||||
|                 putErrorCode(errorCodeRespDTO.getCode(), errorCodeRespDTO.getMessage()); | ||||
|                 // 记录下更新时间,方便增量更新 | ||||
|                 maxUpdateTime = DateUtils.max(maxUpdateTime, errorCodeRespDTO.getUpdateTime()); | ||||
|             }); | ||||
|         } catch (Exception ex) { | ||||
|             log.error("[loadErrorCodes0][加载错误码失败({})]", ExceptionUtil.getRootCauseMessage(ex)); | ||||
|         } | ||||
|     } | ||||
|  | ||||
| } | ||||
| @@ -1,10 +0,0 @@ | ||||
| /** | ||||
|  * 错误码 ErrorCode 的自动配置功能,提供如下功能: | ||||
|  * | ||||
|  * 1. 远程读取:项目启动时,从 system-service 服务,读取数据库中的 ErrorCode 错误码,实现错误码的提水可配置; | ||||
|  * 2. 自动更新:管理员在管理后台修数据库中的 ErrorCode 错误码时,项目自动从 system-service 服务加载最新的 ErrorCode 错误码; | ||||
|  * 3. 自动写入:项目启动时,将项目本地的错误码写到 system-server 服务中,方便管理员在管理后台编辑; | ||||
|  * | ||||
|  * @author 芋道源码 | ||||
|  */ | ||||
| package cn.iocoder.yudao.framework.errorcode; | ||||
| @@ -3,5 +3,4 @@ cn.iocoder.yudao.framework.jackson.config.YudaoJacksonAutoConfiguration | ||||
| cn.iocoder.yudao.framework.swagger.config.YudaoSwaggerAutoConfiguration | ||||
| cn.iocoder.yudao.framework.web.config.YudaoWebAutoConfiguration | ||||
| cn.iocoder.yudao.framework.xss.config.YudaoXssAutoConfiguration | ||||
| cn.iocoder.yudao.framework.banner.config.YudaoBannerAutoConfiguration | ||||
| cn.iocoder.yudao.framework.errorcode.config.YudaoErrorCodeAutoConfiguration | ||||
| cn.iocoder.yudao.framework.banner.config.YudaoBannerAutoConfiguration | ||||
		Reference in New Issue
	
	Block a user
	 YunaiV
					YunaiV