错误码的 Starter 的初始化,完成
This commit is contained in:
@@ -1,12 +1,15 @@
|
||||
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
|
||||
@@ -15,4 +18,9 @@ public class ErrorCodeAutoConfiguration {
|
||||
.setErrorCodeConstantsClass(errorCodeProperties.getConstantsClass());
|
||||
}
|
||||
|
||||
@Bean
|
||||
public ErrorCodeRemoteLoader errorCodeRemoteLoader(ErrorCodeProperties errorCodeProperties) {
|
||||
return new ErrorCodeRemoteLoader(errorCodeProperties.getGroup());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -1,13 +1,18 @@
|
||||
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;
|
||||
/**
|
||||
* 错误码枚举类
|
||||
|
||||
@@ -2,18 +2,19 @@ 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.ErrorCodeRpc;
|
||||
import cn.iocoder.mall.systemservice.rpc.errorcode.dto.ErrorCodeAutoGenerateDTO;
|
||||
import org.apache.dubbo.config.annotation.Reference;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.boot.context.event.ApplicationReadyEvent;
|
||||
import org.springframework.context.event.EventListener;
|
||||
import org.springframework.scheduling.annotation.Async;
|
||||
|
||||
import java.lang.reflect.Field;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.function.Consumer;
|
||||
|
||||
public class ErrorCodeAutoGenerator {
|
||||
|
||||
@@ -28,6 +29,9 @@ public class ErrorCodeAutoGenerator {
|
||||
*/
|
||||
private String errorCodeConstantsClass;
|
||||
|
||||
@Reference(version = "${dubbo.consumer.ErrorCodeRpc.version}")
|
||||
private ErrorCodeRpc errorCodeRpc;
|
||||
|
||||
public ErrorCodeAutoGenerator(String group) {
|
||||
this.group = group;
|
||||
}
|
||||
@@ -54,23 +58,26 @@ public class ErrorCodeAutoGenerator {
|
||||
}
|
||||
// 写入 system-service 服务
|
||||
logger.info("[execute][自动将 ({}) 类的错误码,准备写入到 system-service 服务]", errorCodeConstantsClass);
|
||||
List<ErrorCodeAutoGenerateDTO> autoGenerateDTO = new ArrayList<>();
|
||||
Arrays.stream(errorCodeConstantsClazz.getFields()).forEach(new Consumer<Field>() {
|
||||
@Override
|
||||
public void accept(Field field) {
|
||||
if (field.getType() != ErrorCode.class) {
|
||||
return;
|
||||
}
|
||||
try {
|
||||
ErrorCode errorCode = (ErrorCode) field.get(errorCodeConstantsClazz);
|
||||
autoGenerateDTO.add(new ErrorCodeAutoGenerateDTO().setGroup(group)
|
||||
.setCode(errorCode.getCode()).setMessage(errorCode.getMessage()));
|
||||
} catch (IllegalAccessException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
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);
|
||||
}
|
||||
});
|
||||
logger.info("[execute][自动将 ({}) 类的错误码,完成写入到 system-service 服务]", errorCodeConstantsClass);
|
||||
CommonResult<Boolean> autoGenerateErrorCodesResult = errorCodeRpc.autoGenerateErrorCodes(autoGenerateDTOs);
|
||||
if (autoGenerateErrorCodesResult.isSuccess()) {
|
||||
logger.info("[execute][自动将 ({}) 类的错误码,成功写入到 system-service 服务]", errorCodeConstantsClass);
|
||||
} else {
|
||||
logger.error("[execute][自动将 ({}) 类的错误码,失败写入到 system-service 服务,原因为 ({}/{}/{})]", errorCodeConstantsClass,
|
||||
autoGenerateErrorCodesResult.getCode(), autoGenerateErrorCodesResult.getMessage(), autoGenerateErrorCodesResult.getDetailMessage());
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -1,5 +1,7 @@
|
||||
package cn.iocoder.mall.system.errorcode.core;
|
||||
|
||||
import cn.iocoder.common.framework.util.CollectionUtils;
|
||||
import cn.iocoder.common.framework.util.DateUtil;
|
||||
import cn.iocoder.common.framework.util.ServiceExceptionUtil;
|
||||
import cn.iocoder.common.framework.vo.CommonResult;
|
||||
import cn.iocoder.mall.systemservice.rpc.errorcode.ErrorCodeRpc;
|
||||
@@ -44,32 +46,25 @@ public class ErrorCodeRemoteLoader {
|
||||
listErrorCodesResult.getData().forEach(errorCodeVO -> {
|
||||
ServiceExceptionUtil.put(errorCodeVO.getCode(), errorCodeVO.getMessage());
|
||||
// 记录下更新时间,方便增量更新
|
||||
maxUpdateTime = max(maxUpdateTime, errorCodeVO.getUpdateTime());
|
||||
maxUpdateTime = DateUtil.max(maxUpdateTime, errorCodeVO.getUpdateTime());
|
||||
});
|
||||
}
|
||||
|
||||
@Scheduled(fixedDelay = REFRESH_ERROR_CODE_PERIOD)
|
||||
@Scheduled(fixedDelay = REFRESH_ERROR_CODE_PERIOD, initialDelay = REFRESH_ERROR_CODE_PERIOD)
|
||||
public void refreshErrorCodes() {
|
||||
// 从 ErrorCodeRpc 加载 ErrorCode 错误码
|
||||
CommonResult<List<ErrorCodeVO>> listErrorCodesResult = errorCodeRpc.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 = max(maxUpdateTime, errorCodeVO.getUpdateTime());
|
||||
maxUpdateTime = DateUtil.max(maxUpdateTime, errorCodeVO.getUpdateTime());
|
||||
});
|
||||
}
|
||||
|
||||
private static Date max(Date a, Date b) {
|
||||
if (a == null) {
|
||||
return b;
|
||||
}
|
||||
if (b == null) {
|
||||
return a;
|
||||
}
|
||||
return a.compareTo(b) > 0 ? a : b;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user