基于spring-boot的快速業務開發框架

我們想要的(Fast-Clean-Robust)

  • Fast
    代碼風格需要統一,方便團隊共同作戰
    所有的中間件,配置即可用。
    統一返回結果數據結構,方便代碼重用和前端邏輯抽象
    根據業務設計數據庫表結構后,能自動生成相應的Entity和相應的基礎Mapper(單表簡單的DML語句)
  • Clean
    • 屏蔽各種數據傳遞對象的getter和setter方法,以及Buidler的各種方法
    • 緩存幾乎遍布所有項目,最好能有配置即可用的緩存Template和注解緩存
    • 統一管理和簡化接口所有的邏輯分支
    • 能通過注解進行業務接口的參數前置校驗
  • Robust
    • 掌握代碼的每個流程分支(未掌控的分支邏輯,其實就是BUG)

現實卻是(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("網絡請求參數錯誤,缺少參數");
    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("系統異常,方法orderList異常:", e);
          ErrorBean result = ResultCode.getErrorBean(ResultCode.ERROR_SYSTEM);
          return result;
       } catch (Throwable e) {
          log.error("業務異常,方法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帶你飛

  • 如何飛
    • 當你需要新建項目,你只需要fork一下
    • 修改本來屬于你項目特有的東西即可
  • 背后的支撐
    • mybatis-spring-boot-starter

      1. application.yml配置即可用的mybatis插件,讓Mybatis通過注解@MybatisMapper識別業務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
    • automapper-spring-boot-starter

      1. 通過BaseMapper接口為業務Mapper進行賦能(單表的CRUD)
        @MybatisMapper
        public interface AlertSendKpiMapper extends BaseMapper<AlertSendKpi> {

            void updateAlertSend(@Param("project") String project);
        }
        
      2. 設計數據庫表結構后,能自動生成相應的Entity和相應的業務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;
        
            /**
             * 告警類型(耗時;業務成功率;QPS;錯誤率)
             */
            @Column(name = "alert_type")
            private String alertType;
        
            /**
             * 告警數值
             */
            @Column(name = "threshold_value")
            private Integer thresholdValue;
        }
        業務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

      1. 配置即可用的StringRedisTemplate
      2. 配置即可用的注解Redis緩存
    • log-spring-boot-starter

    • frontends-spring-boot-starter

      1. 前置參數校驗(注解)
      2. 統一返回數據結構
      3. 通過自定義業務異常,統一異常處理,收攏異常分支
      4. 配置即可啟用CORS

背后的干貨

  • 如何開發spring-boot-starter(配置即可用,spring-boot開發哲學)

  • 解析AOP

    • 編碼共識原則
      re-use 和 maintain 原則。解決問題是:雜亂代碼的維護性和擴展性差。
    • OOP(Object Oriented Programming)
      1. 封裝性:高內聚體現者,代碼模塊化,數據和行為綁定,遵循maintain原則。
      2. 繼承性:代碼復用者,層層抽象代碼進行復用,遵循re-use原則。
      3. 多態性:接口和實現進行分離,用接口組織整個框架邏輯,指定不同實現適應不同情況,遵循re-use原則。面向接口編程,特別是讀各種框架代碼運用十分廣泛。
    • AOP(Aspect Oriented Programming)
      各種行為之間的公用的一部分邏輯,能不能有個共同的術語,那他就是AOP。比如:事務,監控。
    • 什么組成了AOP
      • Aspect,模塊化組織者。
      • Join Point:程序的執行過程的連接點。例如:方法的執行,異常的處理。
      • Pointcut:切入點表達式
      • Target object:被切入的對象
      • Weaving:將切片和業務對象連接起來。從編譯時期到加載時期到運行時期都可以進行。
        編譯時期:靜態代理模式,通過AspectJ編譯器。
        加載時期:load-time,當class文件被ClassLoader加載到JVM后。
        運行時期:字節碼運行期間,通過JVM的Proxy模塊或者內似于Cglib通過繼承的方式。
    • 如寫一個AOP
      我們一般通過AOP的幾個注解來進行相關切面的表述。
      我們將AOP切面進行不同的Weaving的方式。

原來,我們追求的是

  • 模塊化
  • 重用
最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容