将 onemall 老代码,统一到归档目录,后续不断迁移移除
This commit is contained in:
46
归档/common/mall-spring-boot-starter-system-error-code/pom.xml
Normal file
46
归档/common/mall-spring-boot-starter-system-error-code/pom.xml
Normal file
@@ -0,0 +1,46 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<parent>
|
||||
<artifactId>common</artifactId>
|
||||
<groupId>cn.iocoder.mall</groupId>
|
||||
<version>1.0-SNAPSHOT</version>
|
||||
</parent>
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<artifactId>mall-spring-boot-starter-system-error-code</artifactId>
|
||||
<description>
|
||||
错误码 ErrorCode 的自动配置功能,提供如下功能:
|
||||
1. 远程读取:项目启动时,从 system-service 服务,读取数据库中的 ErrorCode 错误码,实现错误码的提水可配置;
|
||||
2. 自动更新:管理员在管理后台修数据库中的 ErrorCode 错误码时,项目自动从 system-service 服务加载最新的 ErrorCode 错误码;
|
||||
3. 自动写入:项目启动时,将项目本地的错误码写到 system-service 服务中,方便管理员在管理后台编辑;
|
||||
</description>
|
||||
|
||||
<dependencies>
|
||||
<!-- Mall 相关 -->
|
||||
<dependency>
|
||||
<groupId>cn.iocoder.mall</groupId>
|
||||
<artifactId>system-service-api</artifactId>
|
||||
<version>1.0-SNAPSHOT</version>
|
||||
</dependency>
|
||||
|
||||
<!-- Spring 核心 -->
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-configuration-processor</artifactId>
|
||||
<optional>true</optional>
|
||||
</dependency>
|
||||
|
||||
<!-- RPC 相关 -->
|
||||
<dependency>
|
||||
<groupId>org.apache.dubbo</groupId>
|
||||
<artifactId>dubbo</artifactId>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
</project>
|
||||
@@ -0,0 +1,26 @@
|
||||
package cn.iocoder.mall.system.errorcode.config;
|
||||
|
||||
import cn.iocoder.mall.system.errorcode.core.ErrorCodeAutoGenerator;
|
||||
import cn.iocoder.mall.system.errorcode.core.ErrorCodeRemoteLoader;
|
||||
import org.springframework.boot.context.properties.EnableConfigurationProperties;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.scheduling.annotation.EnableScheduling;
|
||||
|
||||
@Configuration
|
||||
@EnableConfigurationProperties(ErrorCodeProperties.class)
|
||||
@EnableScheduling // 开启调度任务的功能,因为 ErrorCodeRemoteLoader 通过定时刷新错误码
|
||||
public class ErrorCodeAutoConfiguration {
|
||||
|
||||
// @Bean
|
||||
// public ErrorCodeAutoGenerator errorCodeAutoGenerator(ErrorCodeProperties errorCodeProperties) {
|
||||
// return new ErrorCodeAutoGenerator(errorCodeProperties.getGroup())
|
||||
// .setErrorCodeConstantsClass(errorCodeProperties.getConstantsClass());
|
||||
// }
|
||||
//
|
||||
// @Bean
|
||||
// public ErrorCodeRemoteLoader errorCodeRemoteLoader(ErrorCodeProperties errorCodeProperties) {
|
||||
// return new ErrorCodeRemoteLoader(errorCodeProperties.getGroup());
|
||||
// }
|
||||
|
||||
}
|
||||
@@ -0,0 +1,39 @@
|
||||
package cn.iocoder.mall.system.errorcode.config;
|
||||
|
||||
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||
import org.springframework.validation.annotation.Validated;
|
||||
|
||||
import javax.validation.constraints.NotNull;
|
||||
|
||||
@ConfigurationProperties("mall.error-code")
|
||||
@Validated
|
||||
public class ErrorCodeProperties {
|
||||
|
||||
/**
|
||||
* 应用分组
|
||||
*/
|
||||
@NotNull(message = "应用分组不能为空,请设置 mall.error-code.group 配置项,推荐直接使用 spring. application.name 配置项")
|
||||
private String group;
|
||||
/**
|
||||
* 错误码枚举类
|
||||
*/
|
||||
private String constantsClass;
|
||||
|
||||
public String getGroup() {
|
||||
return group;
|
||||
}
|
||||
|
||||
public ErrorCodeProperties setGroup(String group) {
|
||||
this.group = group;
|
||||
return this;
|
||||
}
|
||||
|
||||
public String getConstantsClass() {
|
||||
return constantsClass;
|
||||
}
|
||||
|
||||
public ErrorCodeProperties setConstantsClass(String constantsClass) {
|
||||
this.constantsClass = constantsClass;
|
||||
return this;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,84 @@
|
||||
package cn.iocoder.mall.system.errorcode.core;
|
||||
|
||||
import cn.iocoder.common.framework.exception.ErrorCode;
|
||||
import cn.iocoder.common.framework.util.StringUtils;
|
||||
import cn.iocoder.common.framework.vo.CommonResult;
|
||||
import cn.iocoder.mall.systemservice.rpc.errorcode.ErrorCodeFeign;
|
||||
import cn.iocoder.mall.systemservice.rpc.errorcode.dto.ErrorCodeAutoGenerateDTO;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
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;
|
||||
|
||||
public class ErrorCodeAutoGenerator {
|
||||
//
|
||||
// private Logger logger = LoggerFactory.getLogger(ErrorCodeAutoGenerator.class);
|
||||
//
|
||||
// /**
|
||||
// * 应用分组
|
||||
// */
|
||||
// private final String group;
|
||||
// /**
|
||||
// * 错误码枚举类
|
||||
// */
|
||||
// private String errorCodeConstantsClass;
|
||||
//
|
||||
//
|
||||
// @Autowired
|
||||
// private ErrorCodeFeign errorCodeFeign;
|
||||
// public ErrorCodeAutoGenerator(String group) {
|
||||
// this.group = group;
|
||||
// }
|
||||
//
|
||||
// public ErrorCodeAutoGenerator setErrorCodeConstantsClass(String errorCodeConstantsClass) {
|
||||
// this.errorCodeConstantsClass = errorCodeConstantsClass;
|
||||
// return this;
|
||||
// }
|
||||
//
|
||||
// @EventListener(ApplicationReadyEvent.class)
|
||||
// @Async // 异步,保证项目的启动过程,毕竟非关键流程
|
||||
// public void execute() {
|
||||
// // 校验 errorCodeConstantsClass 参数
|
||||
// if (!StringUtils.hasText(errorCodeConstantsClass)) {
|
||||
// logger.info("[execute][未配置 mall.error-code.constants-class 配置项,不进行自动写入到 system-service 服务]");
|
||||
// return;
|
||||
// }
|
||||
// Class errorCodeConstantsClazz;
|
||||
// try {
|
||||
// errorCodeConstantsClazz = Class.forName(errorCodeConstantsClass);
|
||||
// } catch (ClassNotFoundException e) {
|
||||
// logger.error("[execute][配置的 ({}) 找不到对应的类]", errorCodeConstantsClass);
|
||||
// return;
|
||||
// }
|
||||
// // 写入 system-service 服务
|
||||
// logger.info("[execute][自动将 ({}) 类的错误码,准备写入到 system-service 服务]", errorCodeConstantsClass);
|
||||
// List<ErrorCodeAutoGenerateDTO> autoGenerateDTOs = new ArrayList<>();
|
||||
// Arrays.stream(errorCodeConstantsClazz.getFields()).forEach(field -> {
|
||||
// if (field.getType() != ErrorCode.class) {
|
||||
// return;
|
||||
// }
|
||||
// try {
|
||||
// // TODO 芋艿:校验是否重复了;
|
||||
// ErrorCode errorCode = (ErrorCode) field.get(errorCodeConstantsClazz);
|
||||
// autoGenerateDTOs.add(new ErrorCodeAutoGenerateDTO().setGroup(group)
|
||||
// .setCode(errorCode.getCode()).setMessage(errorCode.getMessage()));
|
||||
// } catch (IllegalAccessException e) {
|
||||
// throw new RuntimeException(e);
|
||||
// }
|
||||
// });
|
||||
// CommonResult<Boolean> autoGenerateErrorCodesResult = errorCodeFeign.autoGenerateErrorCodes(autoGenerateDTOs);
|
||||
// if (autoGenerateErrorCodesResult.isSuccess()) {
|
||||
// logger.info("[execute][自动将 ({}) 类的错误码,成功写入到 system-service 服务]", errorCodeConstantsClass);
|
||||
// } else {
|
||||
// logger.error("[execute][自动将 ({}) 类的错误码,失败写入到 system-service 服务,原因为 ({}/{}/{})]", errorCodeConstantsClass,
|
||||
// autoGenerateErrorCodesResult.getCode(), autoGenerateErrorCodesResult.getMessage(), autoGenerateErrorCodesResult.getDetailMessage());
|
||||
// }
|
||||
// }
|
||||
|
||||
}
|
||||
@@ -0,0 +1,70 @@
|
||||
package cn.iocoder.mall.system.errorcode.core;
|
||||
|
||||
import cn.iocoder.common.framework.exception.util.ServiceExceptionUtil;
|
||||
import cn.iocoder.common.framework.util.CollectionUtils;
|
||||
import cn.iocoder.common.framework.util.DateUtil;
|
||||
import cn.iocoder.common.framework.vo.CommonResult;
|
||||
import cn.iocoder.mall.systemservice.rpc.errorcode.ErrorCodeFeign;
|
||||
import cn.iocoder.mall.systemservice.rpc.errorcode.vo.ErrorCodeVO;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.context.event.ApplicationReadyEvent;
|
||||
import org.springframework.context.event.EventListener;
|
||||
import org.springframework.scheduling.annotation.Scheduled;
|
||||
|
||||
import java.util.Date;
|
||||
import java.util.List;
|
||||
|
||||
public class ErrorCodeRemoteLoader {
|
||||
//
|
||||
// private static final int REFRESH_ERROR_CODE_PERIOD = 60 * 1000;
|
||||
//
|
||||
// private Logger logger = LoggerFactory.getLogger(ErrorCodeRemoteLoader.class);
|
||||
//
|
||||
// /**
|
||||
// * 应用分组
|
||||
// */
|
||||
// private final String group;
|
||||
//
|
||||
// @Autowired
|
||||
// private ErrorCodeFeign errorCodeFeign;
|
||||
// private Date maxUpdateTime;
|
||||
//
|
||||
// public ErrorCodeRemoteLoader(String group) {
|
||||
// this.group = group;
|
||||
// }
|
||||
//
|
||||
// @EventListener(ApplicationReadyEvent.class)
|
||||
// public void loadErrorCodes() {
|
||||
// // 从 errorCodeFeign 全量加载 ErrorCode 错误码
|
||||
// CommonResult<List<ErrorCodeVO>> listErrorCodesResult = errorCodeFeign.listErrorCodes(group, null);
|
||||
// listErrorCodesResult.checkError();
|
||||
// logger.info("[loadErrorCodes][从 group({}) 全量加载到 {} 个 ErrorCode 错误码]", group, listErrorCodesResult.getData().size());
|
||||
// // 写入到 ServiceExceptionUtil 到
|
||||
// listErrorCodesResult.getData().forEach(errorCodeVO -> {
|
||||
// ServiceExceptionUtil.put(errorCodeVO.getCode(), errorCodeVO.getMessage());
|
||||
// // 记录下更新时间,方便增量更新
|
||||
// maxUpdateTime = DateUtil.max(maxUpdateTime, errorCodeVO.getUpdateTime());
|
||||
// });
|
||||
// }
|
||||
//
|
||||
// @Scheduled(fixedDelay = REFRESH_ERROR_CODE_PERIOD, initialDelay = REFRESH_ERROR_CODE_PERIOD)
|
||||
// public void refreshErrorCodes() {
|
||||
// // 从 errorCodeFeign 增量加载 ErrorCode 错误码
|
||||
// // TODO 优化点:假设删除错误码的配置,会存在问题;
|
||||
// CommonResult<List<ErrorCodeVO>> listErrorCodesResult = errorCodeFeign.listErrorCodes(group, maxUpdateTime);
|
||||
// listErrorCodesResult.checkError();
|
||||
// if (CollectionUtils.isEmpty(listErrorCodesResult.getData())) {
|
||||
// return;
|
||||
// }
|
||||
// logger.info("[refreshErrorCodes][从 group({}) 增量加载到 {} 个 ErrorCode 错误码]", group, listErrorCodesResult.getData().size());
|
||||
// // 写入到 ServiceExceptionUtil 到
|
||||
// listErrorCodesResult.getData().forEach(errorCodeVO -> {
|
||||
// ServiceExceptionUtil.put(errorCodeVO.getCode(), errorCodeVO.getMessage());
|
||||
// // 记录下更新时间,方便增量更新
|
||||
// maxUpdateTime = DateUtil.max(maxUpdateTime, errorCodeVO.getUpdateTime());
|
||||
// });
|
||||
// }
|
||||
|
||||
}
|
||||
@@ -0,0 +1,2 @@
|
||||
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
|
||||
cn.iocoder.mall.system.errorcode.config.ErrorCodeAutoConfiguration
|
||||
Reference in New Issue
Block a user