parent
ff18d2f30a
commit
ca28d791aa
@ -1,4 +1,4 @@
|
||||
package cn.iocoder.yudao.module.system.framework.errorcode.core.dto;
|
||||
package cn.iocoder.yudao.module.system.api.errorcode.dto;
|
||||
|
||||
import lombok.Data;
|
||||
import lombok.experimental.Accessors;
|
||||
@ -1,4 +1,4 @@
|
||||
package cn.iocoder.yudao.module.system.framework.errorcode.core.dto;
|
||||
package cn.iocoder.yudao.module.system.api.errorcode.dto;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
@ -0,0 +1,36 @@
|
||||
package cn.iocoder.yudao.module.system.api.errorcode;
|
||||
|
||||
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
|
||||
import cn.iocoder.yudao.module.system.api.errorcode.dto.ErrorCodeAutoGenerateReqDTO;
|
||||
import cn.iocoder.yudao.module.system.api.errorcode.dto.ErrorCodeRespDTO;
|
||||
import cn.iocoder.yudao.module.system.service.errorcode.ErrorCodeService;
|
||||
import org.apache.dubbo.config.annotation.DubboService;
|
||||
import org.springframework.validation.annotation.Validated;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
import java.util.Date;
|
||||
import java.util.List;
|
||||
|
||||
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;
|
||||
import static cn.iocoder.yudao.module.system.enums.ApiConstants.VERSION;
|
||||
|
||||
@RestController // 提供 RESTful API 接口,给 Feign 调用
|
||||
@DubboService(version = VERSION) // 提供 Dubbo RPC 接口,给 Dubbo Consumer 调用
|
||||
@Validated
|
||||
public class ErrorCodeApiImpl implements ErrorCodeApi {
|
||||
|
||||
@Resource
|
||||
private ErrorCodeService errorCodeService;
|
||||
|
||||
@Override
|
||||
public CommonResult<Boolean> autoGenerateErrorCodes(List<ErrorCodeAutoGenerateReqDTO> autoGenerateDTOs) {
|
||||
errorCodeService.autoGenerateErrorCodes(autoGenerateDTOs);
|
||||
return success(true);
|
||||
}
|
||||
|
||||
@Override
|
||||
public CommonResult<List<ErrorCodeRespDTO>> getErrorCodeList(String applicationName, Date minUpdateTime) {
|
||||
return success(errorCodeService.getErrorCodeList(applicationName, minUpdateTime));
|
||||
}
|
||||
}
|
||||
@ -1,33 +0,0 @@
|
||||
package cn.iocoder.common.framework.util;
|
||||
|
||||
import java.util.Random;
|
||||
|
||||
public class MathUtil {
|
||||
|
||||
/**
|
||||
* 随机对象
|
||||
*/
|
||||
private static final Random RANDOM = new Random(); // TODO 后续优化
|
||||
|
||||
/**
|
||||
* 随机[min, max]范围内的数字
|
||||
*
|
||||
* @param min 随机开始
|
||||
* @param max 随机结束
|
||||
* @return 数字
|
||||
*/
|
||||
public static int random(int min, int max) {
|
||||
if (min == max) {
|
||||
return min;
|
||||
}
|
||||
if (min > max) {
|
||||
int temp = min;
|
||||
min = max;
|
||||
max = temp;
|
||||
}
|
||||
// 随即开始
|
||||
int diff = max - min + 1;
|
||||
return RANDOM.nextInt(diff) + min;
|
||||
}
|
||||
|
||||
}
|
||||
@ -1,14 +0,0 @@
|
||||
package cn.iocoder.common.framework.util;
|
||||
|
||||
import cn.hutool.system.SystemUtil;
|
||||
|
||||
/**
|
||||
* 操作系统工具类
|
||||
*/
|
||||
public class OSUtils {
|
||||
|
||||
public static String getHostName() {
|
||||
return SystemUtil.getHostInfo().getName();
|
||||
}
|
||||
|
||||
}
|
||||
@ -1,46 +0,0 @@
|
||||
package cn.iocoder.common.framework.vo;
|
||||
|
||||
import io.swagger.annotations.ApiModel;
|
||||
import io.swagger.annotations.ApiModelProperty;
|
||||
import org.hibernate.validator.constraints.Range;
|
||||
|
||||
import javax.validation.constraints.Min;
|
||||
import javax.validation.constraints.NotNull;
|
||||
import java.io.Serializable;
|
||||
|
||||
@ApiModel("分页参数")
|
||||
public class PageParam implements Serializable {
|
||||
|
||||
@ApiModelProperty(value = "页码,从 1 开始", required = true,example = "1")
|
||||
@NotNull(message = "页码不能为空")
|
||||
@Min(value = 1, message = "页码最小值为 1")
|
||||
private Integer pageNo;
|
||||
|
||||
@ApiModelProperty(value = "每页条数,最大值为 100", required = true, example = "10")
|
||||
@NotNull(message = "每页条数不能为空")
|
||||
@Range(min = 1, max = 100, message = "条数范围为 [1, 100]")
|
||||
private Integer pageSize;
|
||||
|
||||
public Integer getPageNo() {
|
||||
return pageNo;
|
||||
}
|
||||
|
||||
public PageParam setPageNo(Integer pageNo) {
|
||||
this.pageNo = pageNo;
|
||||
return this;
|
||||
}
|
||||
|
||||
public Integer getPageSize() {
|
||||
return pageSize;
|
||||
}
|
||||
|
||||
public PageParam setPageSize(Integer pageSize) {
|
||||
this.pageSize = pageSize;
|
||||
return this;
|
||||
}
|
||||
|
||||
// public final int getOffset() {
|
||||
// return (pageNo - 1) * pageSize;
|
||||
// }
|
||||
|
||||
}
|
||||
@ -1,36 +0,0 @@
|
||||
package cn.iocoder.common.framework.vo;
|
||||
|
||||
import io.swagger.annotations.ApiModel;
|
||||
import io.swagger.annotations.ApiModelProperty;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.util.List;
|
||||
|
||||
@ApiModel("分页结果")
|
||||
public final class PageResult<T> implements Serializable {
|
||||
|
||||
@ApiModelProperty(value = "数据", required = true)
|
||||
private List<T> list;
|
||||
|
||||
@ApiModelProperty(value = "总量", required = true)
|
||||
private Long total;
|
||||
|
||||
public List<T> getList() {
|
||||
return list;
|
||||
}
|
||||
|
||||
public PageResult<T> setList(List<T> list) {
|
||||
this.list = list;
|
||||
return this;
|
||||
}
|
||||
|
||||
public Long getTotal() {
|
||||
return total;
|
||||
}
|
||||
|
||||
public PageResult<T> setTotal(Long total) {
|
||||
this.total = total;
|
||||
return this;
|
||||
}
|
||||
|
||||
}
|
||||
@ -1,46 +0,0 @@
|
||||
<?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>
|
||||
@ -1,26 +0,0 @@
|
||||
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());
|
||||
// }
|
||||
|
||||
}
|
||||
@ -1,39 +0,0 @@
|
||||
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;
|
||||
}
|
||||
}
|
||||
@ -1,84 +0,0 @@
|
||||
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());
|
||||
// }
|
||||
// }
|
||||
|
||||
}
|
||||
@ -1,70 +0,0 @@
|
||||
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());
|
||||
// });
|
||||
// }
|
||||
|
||||
}
|
||||
Loading…
Reference in new issue