基于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的方式。

原来,我们追求的是

  • 模块化
  • 重用
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容