refactor(error-code): 修改为框架定义接口,modules实现
parent
e973a4368f
commit
5bd6316363
|
@ -1,6 +1,7 @@
|
||||||
package cn.iocoder.dashboard.framework.errorcode.config;
|
package cn.iocoder.dashboard.framework.errorcode.config;
|
||||||
|
|
||||||
import cn.iocoder.dashboard.framework.errorcode.core.ErrorCodeRemoteLoader;
|
import cn.iocoder.dashboard.modules.system.service.errorcode.ErrorCodeRemoteLoaderImpl;
|
||||||
|
import cn.iocoder.dashboard.modules.system.service.errorcode.ErrorCodeAutoGeneratorImpl;
|
||||||
import org.springframework.boot.context.properties.EnableConfigurationProperties;
|
import org.springframework.boot.context.properties.EnableConfigurationProperties;
|
||||||
import org.springframework.context.annotation.Bean;
|
import org.springframework.context.annotation.Bean;
|
||||||
import org.springframework.context.annotation.Configuration;
|
import org.springframework.context.annotation.Configuration;
|
||||||
|
@ -12,14 +13,14 @@ import org.springframework.scheduling.annotation.EnableScheduling;
|
||||||
public class ErrorCodeAutoConfiguration {
|
public class ErrorCodeAutoConfiguration {
|
||||||
|
|
||||||
@Bean
|
@Bean
|
||||||
public cn.iocoder.dashboard.framework.errorcode.core.ErrorCodeAutoGenerator errorCodeAutoGenerator(ErrorCodeProperties errorCodeProperties) {
|
public ErrorCodeAutoGeneratorImpl errorCodeAutoGenerator(ErrorCodeProperties errorCodeProperties) {
|
||||||
return new cn.iocoder.dashboard.framework.errorcode.core.ErrorCodeAutoGenerator(errorCodeProperties.getGroup())
|
return new ErrorCodeAutoGeneratorImpl(errorCodeProperties.getGroup())
|
||||||
.setErrorCodeConstantsClass(errorCodeProperties.getConstantsClass());
|
.setErrorCodeConstantsClass(errorCodeProperties.getConstantsClass());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Bean
|
@Bean
|
||||||
public ErrorCodeRemoteLoader errorCodeRemoteLoader(ErrorCodeProperties errorCodeProperties) {
|
public ErrorCodeRemoteLoaderImpl errorCodeRemoteLoader(ErrorCodeProperties errorCodeProperties) {
|
||||||
return new ErrorCodeRemoteLoader(errorCodeProperties.getGroup());
|
return new ErrorCodeRemoteLoaderImpl(errorCodeProperties.getGroup());
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
|
@ -1,82 +1,12 @@
|
||||||
package cn.iocoder.dashboard.framework.errorcode.core;
|
package cn.iocoder.dashboard.framework.errorcode.core;
|
||||||
|
|
||||||
import cn.iocoder.dashboard.common.exception.ErrorCode;
|
/**
|
||||||
import cn.iocoder.dashboard.common.pojo.CommonResult;
|
* @author dylan
|
||||||
import cn.iocoder.dashboard.modules.system.controller.errorcode.dto.ErrorCodeAutoGenerateDTO;
|
*/
|
||||||
import cn.iocoder.dashboard.modules.system.service.errorcode.ErrorCodeService;
|
public interface ErrorCodeAutoGenerator {
|
||||||
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.util.StringUtils;
|
|
||||||
|
|
||||||
import javax.annotation.Resource;
|
|
||||||
import java.util.ArrayList;
|
|
||||||
import java.util.Arrays;
|
|
||||||
import java.util.List;
|
|
||||||
|
|
||||||
@Slf4j
|
|
||||||
public class ErrorCodeAutoGenerator {
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 应用分组
|
* 将配置类到错误码写入数据库
|
||||||
*/
|
*/
|
||||||
private final String group;
|
void execute();
|
||||||
/**
|
|
||||||
* 错误码枚举类
|
|
||||||
*/
|
|
||||||
private String errorCodeConstantsClass;
|
|
||||||
|
|
||||||
@Resource
|
|
||||||
private ErrorCodeService errorCodeService;
|
|
||||||
|
|
||||||
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)) {
|
|
||||||
log.info("[execute][未配置 yudao.error-code.constants-class 配置项,不进行自动写入到当前服务中]");
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
Class errorCodeConstantsClazz;
|
|
||||||
try {
|
|
||||||
errorCodeConstantsClazz = Class.forName(errorCodeConstantsClass);
|
|
||||||
} catch (ClassNotFoundException e) {
|
|
||||||
log.error("[execute][配置的 ({}) 找不到对应的类]", errorCodeConstantsClass);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
// 写入 system-service 服务
|
|
||||||
log.info("[execute][自动将 ({}) 类的错误码,准备写入到当前服务]", 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 = errorCodeService.autoGenerateErrorCodes1(autoGenerateDTOs);
|
|
||||||
if (autoGenerateErrorCodesResult.isSuccess()) {
|
|
||||||
log.info("[execute][自动将 ({}) 类的错误码,成功写入到当前服务]", errorCodeConstantsClass);
|
|
||||||
} else {
|
|
||||||
log.error("[execute][自动将 ({}) 类的错误码,失败写入到当前服务,原因为 ({}/{})]", errorCodeConstantsClass,
|
|
||||||
autoGenerateErrorCodesResult.getCode(), autoGenerateErrorCodesResult.getMsg());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,69 +1,14 @@
|
||||||
package cn.iocoder.dashboard.framework.errorcode.core;
|
package cn.iocoder.dashboard.framework.errorcode.core;
|
||||||
|
|
||||||
import cn.hutool.core.collection.CollUtil;
|
public interface ErrorCodeRemoteLoader {
|
||||||
import cn.iocoder.dashboard.common.exception.util.ServiceExceptionUtil;
|
|
||||||
import cn.iocoder.dashboard.common.pojo.CommonResult;
|
|
||||||
import cn.iocoder.dashboard.modules.system.controller.errorcode.vo.ErrorCodeVO;
|
|
||||||
import cn.iocoder.dashboard.modules.system.service.errorcode.ErrorCodeService;
|
|
||||||
import cn.iocoder.dashboard.util.date.DateUtils;
|
|
||||||
import lombok.extern.slf4j.Slf4j;
|
|
||||||
import org.springframework.boot.context.event.ApplicationReadyEvent;
|
|
||||||
import org.springframework.context.event.EventListener;
|
|
||||||
import org.springframework.scheduling.annotation.Scheduled;
|
|
||||||
|
|
||||||
import javax.annotation.Resource;
|
|
||||||
import java.util.Date;
|
|
||||||
import java.util.List;
|
|
||||||
|
|
||||||
@Slf4j
|
|
||||||
public class ErrorCodeRemoteLoader {
|
|
||||||
|
|
||||||
private static final int REFRESH_ERROR_CODE_PERIOD = 60 * 1000;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 应用分组
|
* 全量加载 ErrorCode 错误码
|
||||||
*/
|
*/
|
||||||
private final String group;
|
void loadErrorCodes();
|
||||||
|
|
||||||
@Resource
|
|
||||||
private ErrorCodeService errorCodeService;
|
|
||||||
|
|
||||||
private Date maxUpdateTime;
|
|
||||||
|
|
||||||
public ErrorCodeRemoteLoader(String group) {
|
|
||||||
this.group = group;
|
|
||||||
}
|
|
||||||
|
|
||||||
@EventListener(ApplicationReadyEvent.class)
|
|
||||||
public void loadErrorCodes() {
|
|
||||||
// 从 ErrorCodeRpc 全量加载 ErrorCode 错误码
|
|
||||||
CommonResult<List<ErrorCodeVO>> listErrorCodesResult = errorCodeService.listErrorCodes1(group, null);
|
|
||||||
listErrorCodesResult.checkError();
|
|
||||||
log.info("[loadErrorCodes][从 group({}) 全量加载到 {} 个 ErrorCode 错误码]", group, listErrorCodesResult.getData().size());
|
|
||||||
// 写入到 ServiceExceptionUtil 到
|
|
||||||
listErrorCodesResult.getData().forEach(errorCodeVO -> {
|
|
||||||
ServiceExceptionUtil.put(errorCodeVO.getCode(), errorCodeVO.getMessage());
|
|
||||||
// 记录下更新时间,方便增量更新
|
|
||||||
maxUpdateTime = DateUtils.max(maxUpdateTime, errorCodeVO.getUpdateTime());
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
@Scheduled(fixedDelay = REFRESH_ERROR_CODE_PERIOD, initialDelay = REFRESH_ERROR_CODE_PERIOD)
|
|
||||||
public void refreshErrorCodes() {
|
|
||||||
// 从 ErrorCodeRpc 增量加载 ErrorCode 错误码
|
|
||||||
// TODO 优化点:假设删除错误码的配置,会存在问题;
|
|
||||||
CommonResult<List<ErrorCodeVO>> listErrorCodesResult = errorCodeService.listErrorCodes1(group, maxUpdateTime);
|
|
||||||
listErrorCodesResult.checkError();
|
|
||||||
if (CollUtil.isEmpty(listErrorCodesResult.getData())) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
log.info("[refreshErrorCodes][从 group({}) 增量加载到 {} 个 ErrorCode 错误码]", group, listErrorCodesResult.getData().size());
|
|
||||||
// 写入到 ServiceExceptionUtil 到
|
|
||||||
listErrorCodesResult.getData().forEach(errorCodeVO -> {
|
|
||||||
ServiceExceptionUtil.put(errorCodeVO.getCode(), errorCodeVO.getMessage());
|
|
||||||
// 记录下更新时间,方便增量更新
|
|
||||||
maxUpdateTime = DateUtils.max(maxUpdateTime, errorCodeVO.getUpdateTime());
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 增量加载 ErrorCode 错误码
|
||||||
|
*/
|
||||||
|
void refreshErrorCodes();
|
||||||
}
|
}
|
||||||
|
|
|
@ -0,0 +1,80 @@
|
||||||
|
package cn.iocoder.dashboard.modules.system.service.errorcode;
|
||||||
|
|
||||||
|
import cn.iocoder.dashboard.common.exception.ErrorCode;
|
||||||
|
import cn.iocoder.dashboard.common.pojo.CommonResult;
|
||||||
|
import cn.iocoder.dashboard.framework.errorcode.core.ErrorCodeAutoGenerator;
|
||||||
|
import cn.iocoder.dashboard.modules.system.controller.errorcode.dto.ErrorCodeAutoGenerateDTO;
|
||||||
|
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.util.StringUtils;
|
||||||
|
|
||||||
|
import javax.annotation.Resource;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.Arrays;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
@RequiredArgsConstructor
|
||||||
|
@Slf4j
|
||||||
|
public class ErrorCodeAutoGeneratorImpl implements ErrorCodeAutoGenerator {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 应用分组
|
||||||
|
*/
|
||||||
|
private final String group;
|
||||||
|
/**
|
||||||
|
* 错误码枚举类
|
||||||
|
*/
|
||||||
|
private String errorCodeConstantsClass;
|
||||||
|
|
||||||
|
@Resource
|
||||||
|
private ErrorCodeService errorCodeService;
|
||||||
|
|
||||||
|
public ErrorCodeAutoGeneratorImpl setErrorCodeConstantsClass(String errorCodeConstantsClass) {
|
||||||
|
this.errorCodeConstantsClass = errorCodeConstantsClass;
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
@EventListener(ApplicationReadyEvent.class)
|
||||||
|
@Async // 异步,保证项目的启动过程,毕竟非关键流程
|
||||||
|
public void execute() {
|
||||||
|
// 校验 errorCodeConstantsClass 参数
|
||||||
|
if (!StringUtils.hasText(errorCodeConstantsClass)) {
|
||||||
|
log.info("[execute][未配置 yudao.error-code.constants-class 配置项,不进行自动写入到当前服务中]");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
Class errorCodeConstantsClazz;
|
||||||
|
try {
|
||||||
|
errorCodeConstantsClazz = Class.forName(errorCodeConstantsClass);
|
||||||
|
} catch (ClassNotFoundException e) {
|
||||||
|
log.error("[execute][配置的 ({}) 找不到对应的类]", errorCodeConstantsClass);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
// 写入 system-service 服务
|
||||||
|
log.info("[execute][自动将 ({}) 类的错误码,准备写入到当前服务]", errorCodeConstantsClass);
|
||||||
|
List<ErrorCodeAutoGenerateDTO> autoGenerateDTOs = new ArrayList<>();
|
||||||
|
Arrays.stream(errorCodeConstantsClazz.getFields()).forEach(field -> {
|
||||||
|
if (field.getType() != ErrorCode.class) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
try {
|
||||||
|
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 = errorCodeService.autoGenerateErrorCodes1(autoGenerateDTOs);
|
||||||
|
if (autoGenerateErrorCodesResult.isSuccess()) {
|
||||||
|
log.info("[execute][自动将 ({}) 类的错误码,成功写入到当前服务]", errorCodeConstantsClass);
|
||||||
|
} else {
|
||||||
|
log.error("[execute][自动将 ({}) 类的错误码,失败写入到当前服务,原因为 ({}/{})]", errorCodeConstantsClass,
|
||||||
|
autoGenerateErrorCodesResult.getCode(), autoGenerateErrorCodesResult.getMsg());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,71 @@
|
||||||
|
package cn.iocoder.dashboard.modules.system.service.errorcode;
|
||||||
|
|
||||||
|
import cn.hutool.core.collection.CollUtil;
|
||||||
|
import cn.iocoder.dashboard.common.exception.util.ServiceExceptionUtil;
|
||||||
|
import cn.iocoder.dashboard.common.pojo.CommonResult;
|
||||||
|
import cn.iocoder.dashboard.framework.errorcode.core.ErrorCodeRemoteLoader;
|
||||||
|
import cn.iocoder.dashboard.modules.system.controller.errorcode.vo.ErrorCodeVO;
|
||||||
|
import cn.iocoder.dashboard.util.date.DateUtils;
|
||||||
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
import org.springframework.boot.context.event.ApplicationReadyEvent;
|
||||||
|
import org.springframework.context.event.EventListener;
|
||||||
|
import org.springframework.scheduling.annotation.Scheduled;
|
||||||
|
|
||||||
|
import javax.annotation.Resource;
|
||||||
|
import java.util.Date;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
@Slf4j
|
||||||
|
public class ErrorCodeRemoteLoaderImpl implements ErrorCodeRemoteLoader {
|
||||||
|
|
||||||
|
private static final int REFRESH_ERROR_CODE_PERIOD = 60 * 1000;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 应用分组
|
||||||
|
*/
|
||||||
|
private final String group;
|
||||||
|
|
||||||
|
@Resource
|
||||||
|
private ErrorCodeService errorCodeService;
|
||||||
|
|
||||||
|
private Date maxUpdateTime;
|
||||||
|
|
||||||
|
public ErrorCodeRemoteLoaderImpl(String group) {
|
||||||
|
this.group = group;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
@EventListener(ApplicationReadyEvent.class)
|
||||||
|
public void loadErrorCodes() {
|
||||||
|
// 从 ErrorCodeRpc 全量加载 ErrorCode 错误码
|
||||||
|
CommonResult<List<ErrorCodeVO>> listErrorCodesResult = errorCodeService.listErrorCodes1(group, null);
|
||||||
|
listErrorCodesResult.checkError();
|
||||||
|
log.info("[loadErrorCodes][从 group({}) 全量加载到 {} 个 ErrorCode 错误码]", group, listErrorCodesResult.getData().size());
|
||||||
|
// 写入到 ServiceExceptionUtil 到
|
||||||
|
listErrorCodesResult.getData().forEach(errorCodeVO -> {
|
||||||
|
ServiceExceptionUtil.put(errorCodeVO.getCode(), errorCodeVO.getMessage());
|
||||||
|
// 记录下更新时间,方便增量更新
|
||||||
|
maxUpdateTime = DateUtils.max(maxUpdateTime, errorCodeVO.getUpdateTime());
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
@Scheduled(fixedDelay = REFRESH_ERROR_CODE_PERIOD, initialDelay = REFRESH_ERROR_CODE_PERIOD)
|
||||||
|
public void refreshErrorCodes() {
|
||||||
|
// 从 ErrorCodeRpc 增量加载 ErrorCode 错误码
|
||||||
|
// TODO 优化点:假设删除错误码的配置,会存在问题;
|
||||||
|
CommonResult<List<ErrorCodeVO>> listErrorCodesResult = errorCodeService.listErrorCodes1(group, maxUpdateTime);
|
||||||
|
listErrorCodesResult.checkError();
|
||||||
|
if (CollUtil.isEmpty(listErrorCodesResult.getData())) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
log.info("[refreshErrorCodes][从 group({}) 增量加载到 {} 个 ErrorCode 错误码]", group, listErrorCodesResult.getData().size());
|
||||||
|
// 写入到 ServiceExceptionUtil 到
|
||||||
|
listErrorCodesResult.getData().forEach(errorCodeVO -> {
|
||||||
|
ServiceExceptionUtil.put(errorCodeVO.getCode(), errorCodeVO.getMessage());
|
||||||
|
// 记录下更新时间,方便增量更新
|
||||||
|
maxUpdateTime = DateUtils.max(maxUpdateTime, errorCodeVO.getUpdateTime());
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
Loading…
Reference in New Issue