我們想要的(Fast-Clean-Robust)
- Fast
代碼風(fēng)格需要統(tǒng)一淆衷,方便團(tuán)隊(duì)共同作戰(zhàn)
所有的中間件,配置即可用渤弛。
統(tǒng)一返回結(jié)果數(shù)據(jù)結(jié)構(gòu)祝拯,方便代碼重用和前端邏輯抽象
根據(jù)業(yè)務(wù)設(shè)計(jì)數(shù)據(jù)庫表結(jié)構(gòu)后,能自動生成相應(yīng)的Entity和相應(yīng)的基礎(chǔ)Mapper(單表簡單的DML語句)
- Clean
- 屏蔽各種數(shù)據(jù)傳遞對象的getter和setter方法暮芭,以及Buidler的各種方法
- 緩存幾乎遍布所有項(xiàng)目鹿驼,最好能有配置即可用的緩存Template和注解緩存
- 統(tǒng)一管理和簡化接口所有的邏輯分支
- 能通過注解進(jìn)行業(yè)務(wù)接口的參數(shù)前置校驗(yàn)
- Robust
- 掌握代碼的每個流程分支(未掌控的分支邏輯,其實(shí)就是BUG)
現(xiàn)實(shí)卻是(Not-fast辕宏,But-dirty)
-
舉個例子---擼段代碼
@RequestMapping(value = "/app/userother/orderList")
public Object orderList(@RequestParam("aopsID") String aopsID,
@RequestParam(value = "orderType", required = false) String orderType,
@RequestParam("orderStatus") String orderStatus,
@RequestParam(value = "orderSource", required = false) String orderSource) {
log.info("orderList============aopsID: " + aopsID);
log.info("orderList============orderType: " + orderType);
log.info("orderList============orderStatus: " + orderStatus);
log.info("orderList============orderSource:" + orderSource);
if (StringUtil.isEmpty(aopsID) || StringUtil.isEmpty(orderStatus)) {
log.info("網(wǎng)絡(luò)請求參數(shù)錯誤畜晰,缺少參數(shù)");
ErrorBean result = ResultCode.getErrorBean(ResultCode.ERROR_PARAMETERERROR);
return result;
}
List<OrderDTO> list = null;
HashMap<String, Object> resultStr = new HashMap<String, Object>();
try {
list = orderService.initOrder(aopsID, orderType, orderStatus, orderSource);
/** json list 串null對象處理 **/
List<Object> listObj = JsonNullToStringUtil.JsonToStringList(list);resultStr.put("resultStr", listObj); } catch (Exception e) { log.error("系統(tǒng)異常,方法orderList異常:", e); ErrorBean result = ResultCode.getErrorBean(ResultCode.ERROR_SYSTEM); return result; } catch (Throwable e) { log.error("業(yè)務(wù)異常瑞筐,方法orderList異常:", e); ErrorBean result = ResultCode.getErrorBean(ResultCode.ERROR_BISNUESSEXCEPTION); return result; } return resultStr; }
-
華麗大變身
@GetMapping(value = "/app/userother/orderList")
public ResultVo orderList(@NotBlank String aopsID,
@NotBlank String orderType,
@NotBlank String orderStatus,
String orderSource) throws Exception{List<OrderDTO> list = orderService.initOrder(aopsID, orderType, orderStatus,orderSource); return wrap(list); }
快上車凄鼻,讓micro-template帶你飛
- 如何飛
- 當(dāng)你需要新建項(xiàng)目,你只需要fork一下
- 修改本來屬于你項(xiàng)目特有的東西即可
- 背后的支撐
-
mybatis-spring-boot-starter
- application.yml配置即可用的mybatis插件聚假,讓Mybatis通過注解@MybatisMapper識別業(yè)務(wù)Mapper
mybatis:
datasource:
driverClassName: com.mysql.jdbc.Driver
url: jdbc:mysql://10.20.129.109:5056/aopsms?useUnicode=true&characterEncoding=UTF8&rewriteBatchedStatements=true&connectTimeout=200&autoReconnect=true&socketTimeout=8000
username: deployop
password: paic1234
initialSize: 10
minIdle: 6
maxActive: 10
maxWait: 500
timeBetweenEvictionRunsMillis: 60000
minEvictableIdleTimeMillis: 300000
- application.yml配置即可用的mybatis插件聚假,讓Mybatis通過注解@MybatisMapper識別業(yè)務(wù)Mapper
-
automapper-spring-boot-starter
-
通過BaseMapper接口為業(yè)務(wù)Mapper進(jìn)行賦能(單表的CRUD)
@MybatisMapper
public interface AlertSendKpiMapper extends BaseMapper<AlertSendKpi> {void updateAlertSend(@Param("project") String project); }
-
設(shè)計(jì)數(shù)據(jù)庫表結(jié)構(gòu)后块蚌,能自動生成相應(yīng)的Entity和相應(yīng)的業(yè)務(wù)Mapper
Entity:
@Data
@AllArgsConstructor
@NoArgsConstructor
@Table(name = "alert_send_kpi")
public class AlertSendKpi {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;/** * 告警project名 */ private String project; /** * 告警源 */ @Column(name = "alert_source") private String alertSource; /** * 告警類型(耗時;業(yè)務(wù)成功率;QPS;錯誤率) */ @Column(name = "alert_type") private String alertType; /** * 告警數(shù)值 */ @Column(name = "threshold_value") private Integer thresholdValue; } 業(yè)務(wù)Mapper:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace="com.paic.insurance.root.micro.template.mapper.AlertSendKpiMapper" >
<resultMap id="BaseResultMap" type="com.paic.insurance.root.micro.template.mapper.po.AlertSendKpi" >
<id column="id" property="id" jdbcType="BIGINT" />
<result column="project" property="project" jdbcType="VARCHAR" />
<result column="alert_source" property="alertSource" jdbcType="VARCHAR" />
<result column="alert_type" property="alertType" jdbcType="VARCHAR" />
<result column="threshold_value" property="thresholdValue" jdbcType="INTEGER" />
<result column="wave_value" property="waveValue" jdbcType="INTEGER" />
<result column="create_time" property="createTime" jdbcType="TIMESTAMP" />
<result column="start_period" property="startPeriod" jdbcType="TIMESTAMP" />
<result column="end_period" property="endPeriod" jdbcType="TIMESTAMP" />
<result column="ext_flag1" property="extFlag1" jdbcType="VARCHAR" />
<result column="ext_flag2" property="extFlag2" jdbcType="VARCHAR" />
<result column="value_standard" property="valueStandard" jdbcType="INTEGER" />
<result column="wave_standard" property="waveStandard" jdbcType="INTEGER" />
<result column="hit_alert_type" property="hitAlertType" jdbcType="INTEGER" />
</resultMap><update id="updateAlertSend">
update alert_send_kpi set create_time=now() where project=#{project}
</update>
</mapper>
-
-
cache-spring-boot-starter
- 配置即可用的StringRedisTemplate
- 配置即可用的注解Redis緩存
log-spring-boot-starter
-
frontends-spring-boot-starter
- 前置參數(shù)校驗(yàn)(注解)
- 統(tǒng)一返回?cái)?shù)據(jù)結(jié)構(gòu)
- 通過自定義業(yè)務(wù)異常,統(tǒng)一異常處理膘格,收攏異常分支
- 配置即可啟用CORS
-
背后的干貨
如何開發(fā)spring-boot-starter(配置即可用峭范,spring-boot開發(fā)哲學(xué))
-
解析AOP
- 編碼共識原則
re-use 和 maintain 原則。解決問題是:雜亂代碼的維護(hù)性和擴(kuò)展性差瘪贱。 - OOP(Object Oriented Programming)
- 封裝性:高內(nèi)聚體現(xiàn)者纱控,代碼模塊化,數(shù)據(jù)和行為綁定菜秦,遵循maintain原則甜害。
- 繼承性:代碼復(fù)用者,層層抽象代碼進(jìn)行復(fù)用球昨,遵循re-use原則尔店。
- 多態(tài)性:接口和實(shí)現(xiàn)進(jìn)行分離,用接口組織整個框架邏輯主慰,指定不同實(shí)現(xiàn)適應(yīng)不同情況嚣州,遵循re-use原則。面向接口編程共螺,特別是讀各種框架代碼運(yùn)用十分廣泛避诽。
- AOP(Aspect Oriented Programming)
各種行為之間的公用的一部分邏輯,能不能有個共同的術(shù)語璃谨,那他就是AOP。比如:事務(wù),監(jiān)控佳吞。 - 什么組成了AOP
- Aspect拱雏,模塊化組織者。
- Join Point:程序的執(zhí)行過程的連接點(diǎn)底扳。例如:方法的執(zhí)行铸抑,異常的處理。
- Pointcut:切入點(diǎn)表達(dá)式
- Target object:被切入的對象
- Weaving:將切片和業(yè)務(wù)對象連接起來衷模。從編譯時期到加載時期到運(yùn)行時期都可以進(jìn)行鹊汛。
編譯時期:靜態(tài)代理模式,通過AspectJ編譯器阱冶。
加載時期:load-time刁憋,當(dāng)class文件被ClassLoader加載到JVM后。
運(yùn)行時期:字節(jié)碼運(yùn)行期間木蹬,通過JVM的Proxy模塊或者內(nèi)似于Cglib通過繼承的方式至耻。
- 如寫一個AOP
我們一般通過AOP的幾個注解來進(jìn)行相關(guān)切面的表述。
我們將AOP切面進(jìn)行不同的Weaving的方式镊叁。
- 編碼共識原則
原來尘颓,我們追求的是
- 模塊化
- 重用