Commit 5bd631636310789779e2409e45937fb242fef055

Authored by dylan
1 parent e973a436

refactor(error-code): 修改为框架定义接口,modules实现

src/main/java/cn/iocoder/dashboard/framework/errorcode/config/ErrorCodeAutoConfiguration.java
1 1 package cn.iocoder.dashboard.framework.errorcode.config;
2 2  
3   -import cn.iocoder.dashboard.framework.errorcode.core.ErrorCodeRemoteLoader;
  3 +import cn.iocoder.dashboard.modules.system.service.errorcode.ErrorCodeRemoteLoaderImpl;
  4 +import cn.iocoder.dashboard.modules.system.service.errorcode.ErrorCodeAutoGeneratorImpl;
4 5 import org.springframework.boot.context.properties.EnableConfigurationProperties;
5 6 import org.springframework.context.annotation.Bean;
6 7 import org.springframework.context.annotation.Configuration;
... ... @@ -12,14 +13,14 @@ import org.springframework.scheduling.annotation.EnableScheduling;
12 13 public class ErrorCodeAutoConfiguration {
13 14  
14 15 @Bean
15   - public cn.iocoder.dashboard.framework.errorcode.core.ErrorCodeAutoGenerator errorCodeAutoGenerator(ErrorCodeProperties errorCodeProperties) {
16   - return new cn.iocoder.dashboard.framework.errorcode.core.ErrorCodeAutoGenerator(errorCodeProperties.getGroup())
  16 + public ErrorCodeAutoGeneratorImpl errorCodeAutoGenerator(ErrorCodeProperties errorCodeProperties) {
  17 + return new ErrorCodeAutoGeneratorImpl(errorCodeProperties.getGroup())
17 18 .setErrorCodeConstantsClass(errorCodeProperties.getConstantsClass());
18 19 }
19 20  
20 21 @Bean
21   - public ErrorCodeRemoteLoader errorCodeRemoteLoader(ErrorCodeProperties errorCodeProperties) {
22   - return new ErrorCodeRemoteLoader(errorCodeProperties.getGroup());
  22 + public ErrorCodeRemoteLoaderImpl errorCodeRemoteLoader(ErrorCodeProperties errorCodeProperties) {
  23 + return new ErrorCodeRemoteLoaderImpl(errorCodeProperties.getGroup());
23 24 }
24 25  
25 26 }
26 27 \ No newline at end of file
... ...
src/main/java/cn/iocoder/dashboard/framework/errorcode/core/ErrorCodeAutoGenerator.java
1 1 package cn.iocoder.dashboard.framework.errorcode.core;
2 2  
3   -import cn.iocoder.dashboard.common.exception.ErrorCode;
4   -import cn.iocoder.dashboard.common.pojo.CommonResult;
5   -import cn.iocoder.dashboard.modules.system.controller.errorcode.dto.ErrorCodeAutoGenerateDTO;
6   -import cn.iocoder.dashboard.modules.system.service.errorcode.ErrorCodeService;
7   -import lombok.extern.slf4j.Slf4j;
8   -import org.springframework.boot.context.event.ApplicationReadyEvent;
9   -import org.springframework.context.event.EventListener;
10   -import org.springframework.scheduling.annotation.Async;
11   -import org.springframework.util.StringUtils;
12   -
13   -import javax.annotation.Resource;
14   -import java.util.ArrayList;
15   -import java.util.Arrays;
16   -import java.util.List;
17   -
18   -@Slf4j
19   -public class ErrorCodeAutoGenerator {
  3 +/**
  4 + * @author dylan
  5 + */
  6 +public interface ErrorCodeAutoGenerator {
20 7  
21 8 /**
22   - * 应用分组
  9 + * 将配置类到错误码写入数据库
23 10 */
24   - private final String group;
25   - /**
26   - * 错误码枚举类
27   - */
28   - private String errorCodeConstantsClass;
29   -
30   - @Resource
31   - private ErrorCodeService errorCodeService;
32   -
33   - public ErrorCodeAutoGenerator(String group) {
34   - this.group = group;
35   - }
36   -
37   - public ErrorCodeAutoGenerator setErrorCodeConstantsClass(String errorCodeConstantsClass) {
38   - this.errorCodeConstantsClass = errorCodeConstantsClass;
39   - return this;
40   - }
41   -
42   - @EventListener(ApplicationReadyEvent.class)
43   - @Async // 异步,保证项目的启动过程,毕竟非关键流程
44   - public void execute() {
45   - // 校验 errorCodeConstantsClass 参数
46   - if (!StringUtils.hasText(errorCodeConstantsClass)) {
47   - log.info("[execute][未配置 yudao.error-code.constants-class 配置项,不进行自动写入到当前服务中]");
48   - return;
49   - }
50   - Class errorCodeConstantsClazz;
51   - try {
52   - errorCodeConstantsClazz = Class.forName(errorCodeConstantsClass);
53   - } catch (ClassNotFoundException e) {
54   - log.error("[execute][配置的 ({}) 找不到对应的类]", errorCodeConstantsClass);
55   - return;
56   - }
57   - // 写入 system-service 服务
58   - log.info("[execute][自动将 ({}) 类的错误码,准备写入到当前服务]", errorCodeConstantsClass);
59   - List<ErrorCodeAutoGenerateDTO> autoGenerateDTOs = new ArrayList<>();
60   - Arrays.stream(errorCodeConstantsClazz.getFields()).forEach(field -> {
61   - if (field.getType() != ErrorCode.class) {
62   - return;
63   - }
64   - try {
65   - // TODO 芋艿:校验是否重复了;
66   - ErrorCode errorCode = (ErrorCode) field.get(errorCodeConstantsClazz);
67   - autoGenerateDTOs.add(new ErrorCodeAutoGenerateDTO().setGroup(group)
68   - .setCode(errorCode.getCode()).setMessage(errorCode.getMessage()));
69   - } catch (IllegalAccessException e) {
70   - throw new RuntimeException(e);
71   - }
72   - });
73   - CommonResult<Boolean> autoGenerateErrorCodesResult = errorCodeService.autoGenerateErrorCodes1(autoGenerateDTOs);
74   - if (autoGenerateErrorCodesResult.isSuccess()) {
75   - log.info("[execute][自动将 ({}) 类的错误码,成功写入到当前服务]", errorCodeConstantsClass);
76   - } else {
77   - log.error("[execute][自动将 ({}) 类的错误码,失败写入到当前服务,原因为 ({}/{})]", errorCodeConstantsClass,
78   - autoGenerateErrorCodesResult.getCode(), autoGenerateErrorCodesResult.getMsg());
79   - }
80   - }
81   -
  11 + void execute();
82 12 }
... ...
src/main/java/cn/iocoder/dashboard/framework/errorcode/core/ErrorCodeRemoteLoader.java
1 1 package cn.iocoder.dashboard.framework.errorcode.core;
2 2  
3   -import cn.hutool.core.collection.CollUtil;
4   -import cn.iocoder.dashboard.common.exception.util.ServiceExceptionUtil;
5   -import cn.iocoder.dashboard.common.pojo.CommonResult;
6   -import cn.iocoder.dashboard.modules.system.controller.errorcode.vo.ErrorCodeVO;
7   -import cn.iocoder.dashboard.modules.system.service.errorcode.ErrorCodeService;
8   -import cn.iocoder.dashboard.util.date.DateUtils;
9   -import lombok.extern.slf4j.Slf4j;
10   -import org.springframework.boot.context.event.ApplicationReadyEvent;
11   -import org.springframework.context.event.EventListener;
12   -import org.springframework.scheduling.annotation.Scheduled;
13   -
14   -import javax.annotation.Resource;
15   -import java.util.Date;
16   -import java.util.List;
17   -
18   -@Slf4j
19   -public class ErrorCodeRemoteLoader {
20   -
21   - private static final int REFRESH_ERROR_CODE_PERIOD = 60 * 1000;
  3 +public interface ErrorCodeRemoteLoader {
22 4  
23 5 /**
24   - * 应用分组
  6 + * 全量加载 ErrorCode 错误码
25 7 */
26   - private final String group;
27   -
28   - @Resource
29   - private ErrorCodeService errorCodeService;
30   -
31   - private Date maxUpdateTime;
32   -
33   - public ErrorCodeRemoteLoader(String group) {
34   - this.group = group;
35   - }
36   -
37   - @EventListener(ApplicationReadyEvent.class)
38   - public void loadErrorCodes() {
39   - // 从 ErrorCodeRpc 全量加载 ErrorCode 错误码
40   - CommonResult<List<ErrorCodeVO>> listErrorCodesResult = errorCodeService.listErrorCodes1(group, null);
41   - listErrorCodesResult.checkError();
42   - log.info("[loadErrorCodes][从 group({}) 全量加载到 {} 个 ErrorCode 错误码]", group, listErrorCodesResult.getData().size());
43   - // 写入到 ServiceExceptionUtil 到
44   - listErrorCodesResult.getData().forEach(errorCodeVO -> {
45   - ServiceExceptionUtil.put(errorCodeVO.getCode(), errorCodeVO.getMessage());
46   - // 记录下更新时间,方便增量更新
47   - maxUpdateTime = DateUtils.max(maxUpdateTime, errorCodeVO.getUpdateTime());
48   - });
49   - }
50   -
51   - @Scheduled(fixedDelay = REFRESH_ERROR_CODE_PERIOD, initialDelay = REFRESH_ERROR_CODE_PERIOD)
52   - public void refreshErrorCodes() {
53   - // 从 ErrorCodeRpc 增量加载 ErrorCode 错误码
54   - // TODO 优化点:假设删除错误码的配置,会存在问题;
55   - CommonResult<List<ErrorCodeVO>> listErrorCodesResult = errorCodeService.listErrorCodes1(group, maxUpdateTime);
56   - listErrorCodesResult.checkError();
57   - if (CollUtil.isEmpty(listErrorCodesResult.getData())) {
58   - return;
59   - }
60   - log.info("[refreshErrorCodes][从 group({}) 增量加载到 {} 个 ErrorCode 错误码]", group, listErrorCodesResult.getData().size());
61   - // 写入到 ServiceExceptionUtil 到
62   - listErrorCodesResult.getData().forEach(errorCodeVO -> {
63   - ServiceExceptionUtil.put(errorCodeVO.getCode(), errorCodeVO.getMessage());
64   - // 记录下更新时间,方便增量更新
65   - maxUpdateTime = DateUtils.max(maxUpdateTime, errorCodeVO.getUpdateTime());
66   - });
67   - }
  8 + void loadErrorCodes();
68 9  
  10 + /**
  11 + * 增量加载 ErrorCode 错误码
  12 + */
  13 + void refreshErrorCodes();
69 14 }
... ...
src/main/java/cn/iocoder/dashboard/modules/system/service/errorcode/ErrorCodeAutoGeneratorImpl.java 0 → 100644
  1 +package cn.iocoder.dashboard.modules.system.service.errorcode;
  2 +
  3 +import cn.iocoder.dashboard.common.exception.ErrorCode;
  4 +import cn.iocoder.dashboard.common.pojo.CommonResult;
  5 +import cn.iocoder.dashboard.framework.errorcode.core.ErrorCodeAutoGenerator;
  6 +import cn.iocoder.dashboard.modules.system.controller.errorcode.dto.ErrorCodeAutoGenerateDTO;
  7 +import lombok.RequiredArgsConstructor;
  8 +import lombok.extern.slf4j.Slf4j;
  9 +import org.springframework.boot.context.event.ApplicationReadyEvent;
  10 +import org.springframework.context.event.EventListener;
  11 +import org.springframework.scheduling.annotation.Async;
  12 +import org.springframework.util.StringUtils;
  13 +
  14 +import javax.annotation.Resource;
  15 +import java.util.ArrayList;
  16 +import java.util.Arrays;
  17 +import java.util.List;
  18 +
  19 +@RequiredArgsConstructor
  20 +@Slf4j
  21 +public class ErrorCodeAutoGeneratorImpl implements ErrorCodeAutoGenerator {
  22 +
  23 + /**
  24 + * 应用分组
  25 + */
  26 + private final String group;
  27 + /**
  28 + * 错误码枚举类
  29 + */
  30 + private String errorCodeConstantsClass;
  31 +
  32 + @Resource
  33 + private ErrorCodeService errorCodeService;
  34 +
  35 + public ErrorCodeAutoGeneratorImpl setErrorCodeConstantsClass(String errorCodeConstantsClass) {
  36 + this.errorCodeConstantsClass = errorCodeConstantsClass;
  37 + return this;
  38 + }
  39 +
  40 + @Override
  41 + @EventListener(ApplicationReadyEvent.class)
  42 + @Async // 异步,保证项目的启动过程,毕竟非关键流程
  43 + public void execute() {
  44 + // 校验 errorCodeConstantsClass 参数
  45 + if (!StringUtils.hasText(errorCodeConstantsClass)) {
  46 + log.info("[execute][未配置 yudao.error-code.constants-class 配置项,不进行自动写入到当前服务中]");
  47 + return;
  48 + }
  49 + Class errorCodeConstantsClazz;
  50 + try {
  51 + errorCodeConstantsClazz = Class.forName(errorCodeConstantsClass);
  52 + } catch (ClassNotFoundException e) {
  53 + log.error("[execute][配置的 ({}) 找不到对应的类]", errorCodeConstantsClass);
  54 + return;
  55 + }
  56 + // 写入 system-service 服务
  57 + log.info("[execute][自动将 ({}) 类的错误码,准备写入到当前服务]", errorCodeConstantsClass);
  58 + List<ErrorCodeAutoGenerateDTO> autoGenerateDTOs = new ArrayList<>();
  59 + Arrays.stream(errorCodeConstantsClazz.getFields()).forEach(field -> {
  60 + if (field.getType() != ErrorCode.class) {
  61 + return;
  62 + }
  63 + try {
  64 + ErrorCode errorCode = (ErrorCode) field.get(errorCodeConstantsClazz);
  65 + autoGenerateDTOs.add(new ErrorCodeAutoGenerateDTO().setGroup(group)
  66 + .setCode(errorCode.getCode()).setMessage(errorCode.getMessage()));
  67 + } catch (IllegalAccessException e) {
  68 + throw new RuntimeException(e);
  69 + }
  70 + });
  71 + CommonResult<Boolean> autoGenerateErrorCodesResult = errorCodeService.autoGenerateErrorCodes1(autoGenerateDTOs);
  72 + if (autoGenerateErrorCodesResult.isSuccess()) {
  73 + log.info("[execute][自动将 ({}) 类的错误码,成功写入到当前服务]", errorCodeConstantsClass);
  74 + } else {
  75 + log.error("[execute][自动将 ({}) 类的错误码,失败写入到当前服务,原因为 ({}/{})]", errorCodeConstantsClass,
  76 + autoGenerateErrorCodesResult.getCode(), autoGenerateErrorCodesResult.getMsg());
  77 + }
  78 + }
  79 +
  80 +}
... ...
src/main/java/cn/iocoder/dashboard/modules/system/service/errorcode/ErrorCodeRemoteLoaderImpl.java 0 → 100644
  1 +package cn.iocoder.dashboard.modules.system.service.errorcode;
  2 +
  3 +import cn.hutool.core.collection.CollUtil;
  4 +import cn.iocoder.dashboard.common.exception.util.ServiceExceptionUtil;
  5 +import cn.iocoder.dashboard.common.pojo.CommonResult;
  6 +import cn.iocoder.dashboard.framework.errorcode.core.ErrorCodeRemoteLoader;
  7 +import cn.iocoder.dashboard.modules.system.controller.errorcode.vo.ErrorCodeVO;
  8 +import cn.iocoder.dashboard.util.date.DateUtils;
  9 +import lombok.extern.slf4j.Slf4j;
  10 +import org.springframework.boot.context.event.ApplicationReadyEvent;
  11 +import org.springframework.context.event.EventListener;
  12 +import org.springframework.scheduling.annotation.Scheduled;
  13 +
  14 +import javax.annotation.Resource;
  15 +import java.util.Date;
  16 +import java.util.List;
  17 +
  18 +@Slf4j
  19 +public class ErrorCodeRemoteLoaderImpl implements ErrorCodeRemoteLoader {
  20 +
  21 + private static final int REFRESH_ERROR_CODE_PERIOD = 60 * 1000;
  22 +
  23 + /**
  24 + * 应用分组
  25 + */
  26 + private final String group;
  27 +
  28 + @Resource
  29 + private ErrorCodeService errorCodeService;
  30 +
  31 + private Date maxUpdateTime;
  32 +
  33 + public ErrorCodeRemoteLoaderImpl(String group) {
  34 + this.group = group;
  35 + }
  36 +
  37 + @Override
  38 + @EventListener(ApplicationReadyEvent.class)
  39 + public void loadErrorCodes() {
  40 + // 从 ErrorCodeRpc 全量加载 ErrorCode 错误码
  41 + CommonResult<List<ErrorCodeVO>> listErrorCodesResult = errorCodeService.listErrorCodes1(group, null);
  42 + listErrorCodesResult.checkError();
  43 + log.info("[loadErrorCodes][从 group({}) 全量加载到 {} 个 ErrorCode 错误码]", group, listErrorCodesResult.getData().size());
  44 + // 写入到 ServiceExceptionUtil 到
  45 + listErrorCodesResult.getData().forEach(errorCodeVO -> {
  46 + ServiceExceptionUtil.put(errorCodeVO.getCode(), errorCodeVO.getMessage());
  47 + // 记录下更新时间,方便增量更新
  48 + maxUpdateTime = DateUtils.max(maxUpdateTime, errorCodeVO.getUpdateTime());
  49 + });
  50 + }
  51 +
  52 + @Override
  53 + @Scheduled(fixedDelay = REFRESH_ERROR_CODE_PERIOD, initialDelay = REFRESH_ERROR_CODE_PERIOD)
  54 + public void refreshErrorCodes() {
  55 + // 从 ErrorCodeRpc 增量加载 ErrorCode 错误码
  56 + // TODO 优化点:假设删除错误码的配置,会存在问题;
  57 + CommonResult<List<ErrorCodeVO>> listErrorCodesResult = errorCodeService.listErrorCodes1(group, maxUpdateTime);
  58 + listErrorCodesResult.checkError();
  59 + if (CollUtil.isEmpty(listErrorCodesResult.getData())) {
  60 + return;
  61 + }
  62 + log.info("[refreshErrorCodes][从 group({}) 增量加载到 {} 个 ErrorCode 错误码]", group, listErrorCodesResult.getData().size());
  63 + // 写入到 ServiceExceptionUtil 到
  64 + listErrorCodesResult.getData().forEach(errorCodeVO -> {
  65 + ServiceExceptionUtil.put(errorCodeVO.getCode(), errorCodeVO.getMessage());
  66 + // 记录下更新时间,方便增量更新
  67 + maxUpdateTime = DateUtils.max(maxUpdateTime, errorCodeVO.getUpdateTime());
  68 + });
  69 + }
  70 +
  71 +}
... ...