一 分层结构
-
web(前端请求层)
-
biz(业务层)
-
dal(数据层)
-
client(外部请求层)
-
common(外部公共层)
-
facade(外观层)
二 分层明细
三 调用关系
-
服务和服务直接可以互相调用;
-
服务可以调用多个域的域能力;
-
域能力是封装好的最小颗粒度的能力,不可互相调用;
-
查询服务直接调用manager,不调用域能力;
四 各层规范
-
定义统一的异常处理切面:处理业务异常和其他运行时异常;
-
内部服务不做异常处理和返回result封装类,异常都抛给web层和facade层处理。
-
查询服务和其他服务区分开,单独放在一个包中;
-
能力唯一对应一个域,且是封装好的最小颗粒度的能力。
-
外部服务要在remote中做好异常处理和封装;
-
业务层中的common类为仅在应用内部使用的公共类;
-
mapper要按不同类型的数据源分开存放,如adb和xdb。
-
common只存放暴露给外部的实体类、常量和枚举;
-
暴露给外部的dto只保留外部必要的字段,其他字段如feature等不可存在。
-
定义统一的异常处理切面:处理业务异常和其他运行时异常;
-
facade层的hsf实现类只做简单的参数校验和转化,不要写业务逻辑。
五 通用代码和工具
-
统一异常处理切面
public class RestExceptionHandler {
public Result system(HttpServletRequest req, Exception e) {
AllLoggers.EXCEPTION.error("RestExceptionHandler.system|servlet:{}|method:{}|code:{}|msg:{}",
req.getServletPath(),req.getMethod(), e.getMessage(), e);
return Result.error(ResultCode.BASE.SYSTEM_ERROR);
}
public Result business(HttpServletRequest req, BusinessException e) {
AllLoggers.EXCEPTION.error("RestExceptionHandler.business|servlet:{}|method:{}|code:{}|msg:{}",
req.getServletPath(),req.getMethod(), e.getMessage(), e);
return Result.error(e.getErrorCode(), e.getErrorMessage());
}
}
-
统一日志打印工具类
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public interface AllLoggers {
/**
* 应用日志
*/
Logger APPLICATION = LoggerFactory.getLogger("APPLICATION");
/**
* 异常日志
*/
Logger EXCEPTION = LoggerFactory.getLogger("EXCEPTION");
/**
* 业务日志
*/
Logger BIZ = LoggerFactory.getLogger("BIZ");
/**
* hsf日志
*/
Logger HSF = LoggerFactory.getLogger("HSF");
/**
* 入口日志
*/
Logger MTOP = LoggerFactory.getLogger("MTOP");
}
<configuration>
<!-- https://github.com/spring-projects/spring-boot/blob/v1.5.13.RELEASE/spring-boot/src/main/resources/org/springframework/boot/logging/logback/defaults.xml -->
<include resource="org/springframework/boot/logging/logback/defaults.xml" />
<property resource="application.properties"></property>
<property name="APP_NAME" value="toms" />
<property name="LOG_PATH" value="${user.home}/${APP_NAME}/logs" />
<property name="LOG_FILE" value="${LOG_PATH}/toms-root.log" />
<appender name="APPLICATION"
class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>${LOG_FILE}/toms-root.log</file>
<encoder>
<pattern><![CDATA[%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] [%level] [traceId:%X{EAGLEEYE_TRACE_ID}] [%class:%line] - %m %n ]]> </pattern>
<charset>UTF-8</charset>
</encoder>
<rollingPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy">
<fileNamePattern>${LOG_PATH}/logs_saved/toms-root.%d{yyyy-MM-dd}.%i.log</fileNamePattern>
<maxHistory>5</maxHistory>
<maxFileSize>1GB</maxFileSize>
<totalSizeCap>20GB</totalSizeCap>
</rollingPolicy>
</appender>
<appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>${CONSOLE_LOG_PATTERN}</pattern>
<charset>utf8</charset>
</encoder>
</appender>
<!--业务日志-->
<appender name="TOMS-BIZ-APPENDER"
class="ch.qos.logback.core.rolling.RollingFileAppender">
<File>${LOG_PATH}/toms-biz.log</File>
<rollingPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy">
<FileNamePattern>${LOG_PATH}/logs_saved/toms-biz.%d{yyyy-MM-dd}.%i.log</FileNamePattern>
<maxHistory>5</maxHistory>
<maxFileSize>2GB</maxFileSize>
<totalSizeCap>20GB</totalSizeCap>
</rollingPolicy>
<encoder>
<pattern><![CDATA[%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] [%level] [traceId:%X{EAGLEEYE_TRACE_ID}] [%class:%line] - %m %n ]]> </pattern>
<charset>UTF-8</charset>
</encoder>
</appender>
<!--hsf日志-->
<appender name="TOMS-HSF-APPENDER"
class="ch.qos.logback.core.rolling.RollingFileAppender">
<File>${LOG_PATH}/toms-hsf.log</File>
<rollingPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy">
<FileNamePattern>${LOG_PATH}/logs_saved/toms-hsf.%d{yyyy-MM-dd}.%i.log</FileNamePattern>
<maxHistory>5</maxHistory>
<maxFileSize>2GB</maxFileSize>
<totalSizeCap>20GB</totalSizeCap>
</rollingPolicy>
<encoder>
<pattern><![CDATA[%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] [%level] [traceId:%X{EAGLEEYE_TRACE_ID}] [%class:%line] - %m %n ]]> </pattern>
<charset>UTF-8</charset>
</encoder>
</appender>
<!-- 通用错误日志 -->
<appender name="TOMS-ERROR-APPENDER"
class="ch.qos.logback.core.rolling.RollingFileAppender">
<File>${LOG_PATH}/toms-error.log</File>
<filter class="ch.qos.logback.classic.filter.LevelFilter">
<level>ERROR</level>
<onMatch>ACCEPT</onMatch>
<onMismatch>DENY</onMismatch>
</filter>
<rollingPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy">
<FileNamePattern>${LOG_PATH}/logs_saved/toms-error.%d{yyyy-MM-dd}.%i.log</FileNamePattern>
<maxHistory>5</maxHistory>
<maxFileSize>2GB</maxFileSize>
<totalSizeCap>10GB</totalSizeCap>
</rollingPolicy>
<encoder>
<pattern><![CDATA[%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] [%level] [traceId:%X{EAGLEEYE_TRACE_ID}] [%class:%line] - %m %n ]]> </pattern>
<charset>UTF-8</charset>
</encoder>
</appender>
<!-- 异常日志 -->
<appender name="TOMS-EXCEPTION-APPENDER"
class="ch.qos.logback.core.rolling.RollingFileAppender">
<File>${LOG_PATH}/toms-exception.log</File>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<FileNamePattern>${LOG_PATH}/logs_saved/toms-exception.%d{yyyy-MM-dd}.log</FileNamePattern>
<maxHistory>5</maxHistory>
</rollingPolicy>
<encoder>
<pattern><![CDATA[%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] [%level] [traceId:%X{EAGLEEYE_TRACE_ID}] [%class:%line] - %m %n ]]> </pattern>
<charset>UTF-8</charset>
</encoder>
</appender>
<logger name="HSF" level="${logback.info.level}" additivity="false">
<appender-ref ref="TOMS-HSF-APPENDER"/>
</logger>
<logger name="BIZ" level="${logback.info.level}" additivity="false">
<appender-ref ref="TOMS-BIZ-APPENDER"/>
<appender-ref ref="TOMS-ERROR-APPENDER"/>
</logger>
<logger name="EXCEPTION" level="${logback.info.level}" additivity="false">
<appender-ref ref="TOMS-EXCEPTION-APPENDER"/>
<appender-ref ref="TOMS-ERROR-APPENDER"/>
</logger>
<root level="INFO">
<appender-ref ref="CONSOLE" />
</root>
</configuration>
-
单位转化工具类
public class UnitConvertUtils {
/**
* 米和千米的进率
*/
public static final double RATE_OF_METRE_AND_KILOMETRE = 1000d;
public static final int INT_RATE_OF_METRE_AND_KILOMETRE = 1000;
/**
* 分和元的进率
*/
public static final double RATE_OF_FEN_AND_YUAN = 100d;
/**
* 立方厘米和立方米的进率
*/
public static final double INT_RATE_OF_CM3_AND_M3 = 1000000d;
/**
* 米转千米
*
* @param toConvert
* @return 异常返回null
*/
public static Double convertMetre2Kilometre(Long toConvert) {
if (toConvert == null) {
return null;
}
return toConvert / RATE_OF_METRE_AND_KILOMETRE;
}
/**
* 千米转米
*
* @param toConvert
* @return 异常返回null
*/
public static Long convertKilometre2Metre(Double toConvert) {
if (toConvert == null) {
return null;
}
BigDecimal bigDecimal = BigDecimal.valueOf(toConvert);
BigDecimal factorBigDecimal = BigDecimal.valueOf(RATE_OF_METRE_AND_KILOMETRE);
return bigDecimal.multiply(factorBigDecimal).longValue();
}
/**
* 元转分
*
* @param toConvert
* @return 异常返回null
*/
public static Long convertYuan2Fen(Double toConvert) {
if (toConvert == null) {
return null;
}
BigDecimal bigDecimal = BigDecimal.valueOf(toConvert);
BigDecimal factorBigDecimal = BigDecimal.valueOf(RATE_OF_FEN_AND_YUAN);
return bigDecimal.multiply(factorBigDecimal).longValue();
}
/**
* 元转分
*
* @param toConvert
* @return 异常返回null
*/
public static Long convertYuan2Fen(String toConvert) {
if (toConvert == null) {
return null;
}
BigDecimal bigDecimal = BigDecimal.valueOf(ConvertUtils.convertString2Double(toConvert));
BigDecimal factorBigDecimal = BigDecimal.valueOf(RATE_OF_FEN_AND_YUAN);
return bigDecimal.multiply(factorBigDecimal).longValue();
}
/**
* 分转元
*
* @param price
* @return
*/
public static String convertFen2Yuan(Long price) {
if (price == null) {
return null;
}
return BigDecimal.valueOf(price).divide(new BigDecimal(RATE_OF_FEN_AND_YUAN)).toString();
}
/**
* 里程米转换为千米
*
* @param distance
* @return
*/
public static Double meter2Kilometer(Long distance) {
if (distance == null) {
return null;
}
BigDecimal meter = BigDecimal.valueOf(distance);
BigDecimal kilometer = meter.divide(new BigDecimal(INT_RATE_OF_METRE_AND_KILOMETRE));
return kilometer.doubleValue();
}
/**
* 立方厘米转立方米
*
* @param volume
* @return
*/
public static String convertCm32M3(Long volume) {
if (volume == null) {
return null;
}
return BigDecimal.valueOf(volume).divide(new BigDecimal(INT_RATE_OF_CM3_AND_M3)).toString();
}
}
开发者评测局特别节目暨无影评测大赛颁奖典礼 重磅来袭!
阿里云开发者社区重磅评测栏目《开发者评测局》暨无影评测大赛颁奖典礼重磅开播。CSDN TOP 1 博主“处女座程序猿”、清华大学教授卓晴,苏宁消金安全运维总经理顾黄亮等来自开发者、高校、企业的参赛代表嘉宾与无影内部团队展开了深度圆桌论坛,共话“云时代云办公”。点击阅读原文查看完整视频!