feat:V3.1.1整合remi-comm到remi-cloud中

This commit is contained in:
dianjiu
2024-09-11 17:41:17 +08:00
parent e111c59a69
commit 28d62f6cf4
245 changed files with 20975 additions and 154 deletions

View File

@@ -0,0 +1,6 @@
```yaml
i18n:
messages:
basename: i18n/messages
encoding: utf-8
```

View File

@@ -0,0 +1,55 @@
<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<artifactId>remi-comm-exception</artifactId>
<name>remi-comm-exception</name>
<description>remi-comm-exception</description>
<parent>
<groupId>ai.remi.top.cloud</groupId>
<artifactId>remi-comm</artifactId>
<version>3.1.1</version>
</parent>
<!-- 打包上传到私服, 需要和配置文件中的server节点配合使用 -->
<distributionManagement>
<repository>
<id>maven-releases</id>
<name>release</name>
<url>https://repository.njydsz.com/repository/maven-releases/</url>
</repository>
<snapshotRepository>
<id>maven-snapshots</id>
<name>snapshot</name>
<url>https://repository.njydsz.com/repository/maven-snapshots/</url>
</snapshotRepository>
</distributionManagement>
<dependencies>
<!--remi-comm-domain-->
<dependency>
<groupId>ai.remi.top.cloud</groupId>
<artifactId>remi-comm-domain</artifactId>
</dependency>
<!--spring-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-aop</artifactId>
</dependency>
<!--slf4j-->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-nop</artifactId>
</dependency>
</dependencies>
</project>

View File

@@ -0,0 +1,40 @@
package ai.remi.comm.exception.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.support.ReloadableResourceBundleMessageSource;
import org.springframework.validation.beanvalidation.LocalValidatorFactoryBean;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
@Configuration
public class I18nConfig implements WebMvcConfigurer {
//@Value("${i18n.messages.basename:}")
//private String messagesBasename;
//@Value("${i18n.messages.encoding:}")
//private String messagesEncoding;
@Bean(name = "messageSource")
public ReloadableResourceBundleMessageSource messageSource() {
ReloadableResourceBundleMessageSource rbms = new ReloadableResourceBundleMessageSource();
//if (!StringUtils.isEmpty(messagesBasename)) {
// //设置path
// rbms.addBasenames(messagesBasename.split(","));
//}
//rbms.setDefaultEncoding(messagesEncoding);
rbms.setBasenames("classpath:i18n/messages");
rbms.setDefaultEncoding("UTF-8");
rbms.setUseCodeAsDefaultMessage(true);
rbms.setFallbackToSystemLocale(false);
return rbms;
}
@Bean
public LocalValidatorFactoryBean getValidator() {
LocalValidatorFactoryBean bean = new LocalValidatorFactoryBean();
bean.setValidationMessageSource(messageSource());
return bean;
}
}

View File

@@ -0,0 +1,89 @@
package ai.remi.comm.exception.custom;
import ai.remi.comm.core.constant.Constants;
import ai.remi.comm.exception.util.MessageUtils;
import lombok.Data;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
/**
* @author dianjiu【公众号 点九开源】
* @email dianjiuxyz@gmail.com
* @desc BusinessException
*/
@Data
@NoArgsConstructor
public class BusinessException extends RuntimeException {
@Getter
@Setter
private String code;
@Getter
@Setter
private String key;
@Getter
@Setter
private Object[] params;
@Getter
@Setter
private String message;
public BusinessException(String key) {
super(MessageUtils.getMessage(key));
this.code = Constants.ERROR;
this.message = MessageUtils.getMessage(key);
}
public BusinessException(String key, Object[] params) {
super(MessageUtils.getMessage(key, params));
this.code = Constants.ERROR;
this.message = MessageUtils.getMessage(key, params);
}
public BusinessException(String code, String key) {
super(MessageUtils.getMessage(key));
this.code = code;
this.message = MessageUtils.getMessage(key);
}
public BusinessException(String code, String key, Object[] params) {
super(MessageUtils.getMessage(key, params));
this.code = code;
this.message = MessageUtils.getMessage(key, params);
}
public BusinessException(Throwable cause) {
super(cause);
}
public BusinessException(String code, Throwable cause) {
super(cause);
this.code = code;
}
public BusinessException(String code, String key, Throwable cause) {
super(MessageUtils.getMessage(key), cause);
this.code = code;
this.message = MessageUtils.getMessage(key);
}
public BusinessException(String code, String key, Object[] params, Throwable cause) {
super(MessageUtils.getMessage(key, params), cause);
this.code = code;
this.message = MessageUtils.getMessage(key, params);
}
public BusinessException(String code, String key, Throwable cause, boolean enableSuppression, boolean writableStackTrace) {
super(MessageUtils.getMessage(key), cause, enableSuppression, writableStackTrace);
this.code = code;
this.message = MessageUtils.getMessage(key);
}
public BusinessException(String code, String key, Object[] params, Throwable cause, boolean enableSuppression, boolean writableStackTrace) {
super(MessageUtils.getMessage(key, params), cause, enableSuppression, writableStackTrace);
this.code = code;
this.message = MessageUtils.getMessage(key, params);
}
}

View File

@@ -0,0 +1,52 @@
package ai.remi.comm.exception.enums;
import lombok.AllArgsConstructor;
import lombok.Getter;
/**
* @author dianjiu【公众号 点九开源】
* @email dianjiuxyz@gmail.com
* @desc 错误码设计描述
* <p>
* 面向日志的错误码定义共十三位(十位有意义,三位连接符),并且应该具有如下分类: 应用标识,表示错误属于哪个应用,三位数字。 功能域标识,表示错误属于应用中的哪个功能模块,三位数字。 错误类型,表示错误属于那种类型,一位字母。
* 错误编码,错误类型下的具体错误,三位数字。
* <p>
* 面向外部传递的错误码共六位,并且有如下分类: 错误类型,表示错误来源,1位字母。 应用标识,表示错误属于哪个应用,2位数字。 错误编码,错误类型下的具体错误,3位数字。
* <p>
* 错误类型: A-表示错误来源于用户 B-表示错误来源于系统 C-表示错误来源于第三方接口
*/
@Getter
@AllArgsConstructor
public enum CommExceptionCode implements ExceptionCode {
/**
* 面向外部传递的错误码共六位,并且有如下分类:
* 错误类型,表示错误来源,1位字母。
* 应用标识,表示错误属于哪个应用,2位数字。
* 错误编码,错误类型下的具体错误,3位数字。
* <p>
* 错误类型: A-表示错误来源于用户 B-表示错误来源于系统 C-表示错误来源于第三方接口
*/
/****************************客户端异常****************************/
BUSINESS_ERROR("A01001", "business.error"),
ILLEGAL_ARGUMENT("A01002", "illegal.argument"),
NOT_LOGGED_IN("A01003", "not.logged.in"),
/****************************系统异常****************************/
SYSTEM_ERROR("B01001", "system.error"),
ASSERT_ERROR("B01002", "assert.error"),
/****************************三方异常****************************/
OTHER_SYSTEM_ERROR("C01001", "other.system.error"),
CALL_FEIGN_ERROR("C01002", "call.feign.error"),
CALL_SQL_ERROR("C01003", "call.sql.error"),
CALL_REDIS_ERROR("C01004", "call.redis.error"),
CALL_MINIO_ERROR("C01005", "call.minio.error"),
CALL_NACOS_ERROR("C01006", "call.nacos.error"),
CALL_JOB_ERROR("C01007", "call.job.error"),
CALL_MQ_ERROR("C01008", "call.mq.error"),
CALL_ES_ERROR("C01009", "call.es.error");
private String code;
private String key;
}

View File

@@ -0,0 +1,17 @@
package ai.remi.comm.exception.enums;
/**
* 异常code接口
*/
public interface ExceptionCode {
/**
* 异常code
* @return
*/
String getCode();
/**
* 异常消息
* @return
*/
String getKey();
}

View File

@@ -0,0 +1,24 @@
package ai.remi.comm.exception.util;
import java.io.PrintWriter;
import java.io.StringWriter;
/**
* @author dianjiu【公众号 点九开源】
* @email dianjiuxyz@gmail.com
* @desc ExceptionUtils
*/
public class ExceptionUtils {
/**
* 获取exception的详细错误信息。
*/
public static String getTrace(Throwable e) {
StringWriter sw = new StringWriter();
e.printStackTrace(new PrintWriter(sw, true));
return sw.toString();
}
}

View File

@@ -0,0 +1,136 @@
package ai.remi.comm.exception.util;
import ai.remi.comm.core.spring.SpringBeans;
import ai.remi.comm.util.auth.AuthInfoUtils;
import ai.remi.comm.util.string.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.MessageSource;
import org.springframework.stereotype.Component;
import java.util.Locale;
/**
* @author dianjiu【公众号 点九开源】
* @email dianjiuxyz@gmail.com
* @desc MessageUtils
*/
@Component
public class MessageUtils {
private static final Logger logger = LoggerFactory.getLogger(MessageUtils.class);
/**
* 获得多语言内容,默认根据当前用户的Local获取
*
* @param key 多语言key
* @return 翻译后的值,如获取不到则返回key
*/
public static String getMessage(String key) {
return getMessage(key, new Object[]{});
}
/**
* 获得多语言内容--带参数,默认根据当前用户的Local获取
*
* @param key 多语言key
* @param params 参数
* @return 翻译后的值,如获取不到则返回key
*/
public static String getMessage(String key, Object[] params) {
Locale locale = getLocale();
if (params == null) {
params = new Object[]{};
}
return getMessage(locale, key, params);
}
/**
* 获取多语言内容--带默认值,默认根据当前用户的Local获取
*
* @param key 多语言key
* @param defaultMsg 默认返回值
* @return 翻译后的值,获取不到返回默认值
*/
public static String getMessage(String key, String defaultMsg) {
return getMessage(key, new Object[]{}, defaultMsg);
}
/**
* 获得多语言内容--带参数--带默认值,默认根据当前用户的Local获取
*
* @param key 多语言key
* @param defaultMsg 默认返回值
* @param params 参数
* @return 翻译后的值,获取不到返回默认值
*/
public static String getMessage(String key, Object[] params, String defaultMsg) {
Locale locale = getLocale();
return getMessage(locale, key, params, defaultMsg);
}
/**
* 不带参数
*
* @param locale 语言信息
* @param key 多语言key
* @return 翻译后的值
*/
public static String getMessage(Locale locale, String key) {
return getMessage(locale, key, new Object[]{});
}
/**
* 获取多语言数据
*
* @param locale 语言信息
* @param key 多语言key
* @param params 参数
* @return 翻译后的值
*/
public static String getMessage(Locale locale, String key, Object[] params) {
try {
MessageSource messageSource = (MessageSource) SpringBeans.getBean("messageSource");
return messageSource.getMessage(key, params, locale);
} catch (Exception e) {
logger.error("get locale msg error", e);
}
return key;
}
/**
* 获取多语言数据
*
* @param key 多语言key
* @param params 参数
* @param defaultMsg 默认返回值
* @param locale 语言信息
* @return 翻译后的值
*/
public static String getMessage(Locale locale, String key, Object[] params, String defaultMsg) {
MessageSource messageSource = (MessageSource) SpringBeans.getBean("messageSource");
return messageSource.getMessage(key, params, defaultMsg, locale);
}
/**
* 获取当前语言
*
* @return
*/
private static Locale getLocale() {
String userLanguage = AuthInfoUtils.getUserLanguage();
if (StringUtils.isBlank(userLanguage)) {
return Locale.CHINA;
}
switch (userLanguage.toLowerCase()) {
case "zh_cn":
case "zh-CN":
case "zh":
return Locale.CHINA;
default:
return Locale.US;
}
}
}

View File

@@ -0,0 +1,2 @@
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
ai.remi.comm.exception.config.I18nConfig