美文网首页
1. Cola2.0框架入门

1. Cola2.0框架入门

作者: 捞月亮的阿汤哥 | 来源:发表于2020-08-13 22:43 被阅读0次

    1. 前言介绍

    COLA既是框架,也是架构。创建COLA的主要目的是为应用架构提供一套简单的可以复制、可以理解、可以落地、可以控制复杂性的”指导和约束"。

    2. 快速启动示例项目 craftsman

    • 在本地新建craftsman的数据库,项目用的mysql driver是8.0的,所以可以使用mysql8
    • 执行resource下的TableCreationDDL.sql,创建项目的表,共两张表
    • 修改application.properties的配置文件的mysql用户名和密码
    • 修改MetricDO类,新增两个属性(我的版本要加你看下你跑案例会不会报错)
    private String creator="tangzihao";
    private String modifier = "tangzihao";
    
    • 需要在表user_profile中插数据
    • 执行Application.java就好了

    3. craftsman项目启动后测试

    3.1 /metrics/ata的post方法测试

    使用postman

    • 方法 post
    • 路径: localhost:8080/metrics/ata
    • 请求体:
    {
        "ataMetricCO": {
            "title":"母猪的产后护理",
            "url":"https://www.google.com/search?q=%E7%A6%85%E4%B8%8E%E6%91%A9%E6%89%98%E8%BD%A6%E7%BB%B4%E4%BF%AE%E7%9A%84%E8%89%BA%E6%9C%AFpdf&oq=%E7%A6%85%E4%B8%8E%E6%91%A9%E6%89%98%E8%BD%A6&aqs=chrome.1.69i57j35i39j0l6.6603j0j4&sourceid=chrome&ie=UTF-8",
            "thumbsUpCount":10,
            "hitCount":1000,
            "commentCount":100,
            "favoriteCount":20,
            "ownerId":"123456"
        },
        "operater": "tangzihao",
        "needsOperator": true
    }
    

    3.2 metrics/ata get方法测试

    • 方法: get
    • 路径: localhost:8080/metrics/ata?ownerId=123456

    4. 模块介绍

    4.1 各个模块介绍

    app模块 client模块 controller模块 domain模块 infrastructure模块 start模块
    command api 和一般的controller一样 领域对象 common 启动类
    converter dto config 需要注入Bootstrap的bean
    event tunnel mapper和rpc
    interceptor
    repository
    service

    4.2 boot启动过程分析

      1. 扫描到bootstrap类型的bean,执行其init方法


        1_bootstrapbean_.png
      1. bootstrap的init方法执行


        2_bootstrapinit.png
    • register类图


      image.png

    4.3 controller的请求调用分析

    image.png
    重点
    • controller的command和commandExecuter是一一对应的
    • 通过command可以找到commandExecuter
      • command位于client包,commandExecuter位于app包
      • command需要继承Command
    • commandExecuter需要实现CommandExecutorI
      • 同时需要添加@Command注解
    • repository在cola中是相当于service的
    • tunnel是dao层和rpc层的抽象
    • tunnel在执行逻辑后可以发布领域事件,可以同步或者异步
    • 领域事件的执行逻辑和CommandHub基本一致找到EventHandler,然后执行

    4.4 对象的转换

    image.png
    • 真正的转换是在commandExecutor进行的
    • CO和command都算dto位于client模块
    • 领域对象在domain层非常重要
    • DO在infrastructure层

    4.5 领域事件

    • controller流转到service
    • service位于client层
    • service通过CommandBus发布事件,但是是同步调用的
    • CommandBus-->CommandHub-->Invocation-->CommandExecutorI-->repository-->tunnel执行逻辑
    • tunnel发布领域事件event
    • EventBus-->EventHub-->EventHandler(注意下eventbus执行同步调用和异步调用)

    4.6 异常处理

    • 会在哪里出现异常
      *CommandInvocation有处理异常
      *EventBus中有处理异常
    • cola框架异常处理的优点
      *异常由框架进行捕获,异常的处理入口收敛了
      *异常处理逻辑
    • 代码示例
    if (exception instanceof BaseException) { ErrorCodeI errCode = ((BaseException) exception).getErrCode(); response.setErrCode(errCode.getErrCode());}else { response.setErrCode(BasicErrorCode.SYS_ERROR.getErrCode());}response.setErrMessage(exception.getMessage());response.setSuccess(false);
    
    • 思路
      将业务异常和系统异常分开,通过errorCode和errorMessage进行问题定位

    相关文章

      网友评论

          本文标题:1. Cola2.0框架入门

          本文链接:https://www.haomeiwen.com/subject/puqidktx.html