一种优秀的分层架构设计-COLA

本文介绍了COLA架构的核心原则,包括分层设计、组件和包结构规范,以及命名规则。同时提供了使用Mavenarchetype快速创建COLA应用的方法,并演示了如何运行和测试基本的REST请求。
摘要由CSDN通过智能技术生成

COLA架构设计

COLA架构核心职责就是定义良好的应用结构,提供最佳应用架构的最佳实践。通过不断探索,我们发现良好的分层结构,良好的包结构定义,可以帮助我们治理混乱不堪的业务应用系统。

COLA 的分层是一种经过改良的三层架构,主要是将传统的业务逻辑层拆分为应用层、领域层和基础设施层。

  • 展示层(Presentation Layer):负责以 Rest 的风格接收 Web 请求,然后将请求路由给 Application 层执行,并返回视图模型(View Model),其载体通常是数据传输对象(Data Transfer Object, DTO);

  • 应用层(Application Layer):主要负责获取输入、组装上下文、做输入校验、调用领域层做业务处理,当需要时发送消息通知。当然,层次是开放的,若有需要,应用层也可以直接访问基础设施层;

  • 领域层(Domain Layer):主要封装了核心业务逻辑, 并通过领域服务(Domaini Service)和领域对象(Entities)的函数对外部提供业务逻辑的计算和处理;

  • 基础设施层(Infrastructure Layer):主要包含数据访问通道(Tunnel)、Config 和 Comon。这里Tunnel这个概念对所有的数据来源进行抽象,数据来源可以是数据库(MySQL/NoSQL)、搜索引擎、文件系统,也可以是SOA服务等;Config 负责应用的配置;Common是通用的工具类;

规范设计

COLA制定了一系列的规范,包括组件(Module)结构、包(Package)结构、命名等。

组件规范

COLA规定一个应用至少要有3个组件:应用层、领域层和基础设施层。如果不是严格的前后端分离,也可以加入展示层的组件,但这是可选的。组件的依赖关系如下:

组件的依赖关系

包规范

应用的包结构:

应用的包结构

领域层的包结构:

领域层的包结构

命名规范:

在COLA架构中,制定了一系列的命名规范,以便通过名称就能知晓该类的作用和职责范围,从而极大的提升代码的可理解性,提升代码审查的效率。

规范

用途

解释

xxxCmd.java

Client Request

Cmd代表Command,表示一个用户请求

xxxCo.java

Client Object

客户端对象,用于传递数据,等同于DTO

xxxServiceI.java

API Service

API接口类

xxxCmdExe.java

Command Executor

命令模式,每一个用户请求对应的一个执行器

xxxInterceptor.java

Command Interceptor

拦截器,用于处理切面逻辑

xxxExtPt.java

Extension Point

扩展点

xxxExt.java

Extension

扩展实现

xxxValidator.java

Validator

校验器,用于校验的类

xxxConvertor.java

Convertor

转化器,实现不同层级对象互转

xxxAssembler.java

Assembler

组装器,组装外部服务调用参数

xxxE.java

Entity

代表领域实体

xxxV.java

Value Ojbect

代码值对象

xxxRepository.java

Repository

仓储接口

xxxDomainService.java

Domain Service

领域服务

xxxDo.java

Data Object

数据对象,用于持久化

xxxTunnel.java

Data Tunnel

数据通道,DAO 是最常见的通道,也可是其他通道

使用COLA架构

1、创建应用

执行以下命令:

 mvn archetype:generate \
    -DgroupId=com.alibaba.cola.demo.web \
    -DartifactId=demo-web \
    -Dversion=1.0.0-SNAPSHOT \
    -Dpackage=com.alibaba.demo \
    -DarchetypeArtifactId=cola-framework-archetype-web \
    -DarchetypeGroupId=com.alibaba.cola \
    -DarchetypeVersion=4.3.1

2、运行应用

在项目目录下运行mvn install(如果不想运行测试,可以加上-DskipTests参数)。

进入start目录,执行mvn spring-boot:run。

运行成功的话,可以看到SpringBoot启动成功的界面。

生成的应用中,已经实现了一个简单的Rest请求,可以在浏览器中输入 http://localhost:8080/helloworld 进行测试。

注意:

如果要生成不是web工程而是service工程也类似,执行的是下面的命令:

 mvn archetype:generate \
    -DgroupId=com.alibaba.cola.demo.service \
    -DartifactId=demo-service \
    -Dversion=1.0.0-SNAPSHOT \
    -Dpackage=com.alibaba.demo \
    -DarchetypeArtifactId=cola-framework-archetype-service \
    -DarchetypeGroupId=com.alibaba.cola \
    -DarchetypeVersion=4.3.1
  • 3
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Building the FP-tree: Transaction ID #1: apple, banana, coca-cola, doughnut ``` root | a | p | p - b | | | c | | | d ``` Transaction ID #2: banana, coca-cola ``` root | a | p - b - c | | | d ``` Transaction ID #3: banana, doughnut ``` root | a | p - b - c | | | | | d | | | d ``` Transaction ID #4: apple, coca-cola ``` root | a - c | | | p - b - c | | | | | d | | | d ``` Transaction ID #5: apple, banana, doughnut ``` root | a - b - d | | | | | c | | | p - b - c | | | d | b - d | c ``` Transaction ID #6: apple, banana, coca-cola ``` root | a - b - c | | | | | d | | | p - b - c | | | d | b - d | c ``` Using the FP-Growth algorithm to discover frequent itemsets: Starting with the most frequent item (d): - d (4) - b-d (3) - c-b-d (2) - a-b-d (2) - a-p-b-d (2) Next, starting with the next most frequent item (b): - b (4) - a-b (3) - p-b (3) - c-b (2) - a-p-b (2) - c-b-d (2) - a-b-d (2) - a-p-b-d (2) Finally, starting with the least frequent item (c): - c (3) - b-c (2) - a-b-c (2) - p-b-c (2) - c-b-d (2) - a-b-d (2) - a-p-b-d (2) All sets of frequent itemsets with minimum support of 2 are: - {d} (4) - {b} (4) - {c} (3) - {a, d} (2) - {b, d} (3) - {p, b, d} (2) - {c, b, d} (2) - {a, b, d} (2) - {a, p, b, d} (2) - {a, b} (3) - {p, b} (3) - {c, b} (2) - {a, p, b} (2) - {c, b, d} (2) - {a, b, d} (2) - {a, p, b, d} (2) - {a, c, b} (2) - {p, c, b} (2) - {a, p, c, b} (2) Using the Apriori algorithm to verify the frequent itemsets with minimum support of 2: Starting with 1-itemsets: - {apple} (3) - {banana} (4) - {coca-cola} (3) - {doughnut} (4) Next, starting with 2-itemsets: - {apple, banana} (2) - {apple, coca-cola} (1) - {apple, doughnut} (2) - {banana, coca-cola} (2) - {banana, doughnut} (2) - {coca-cola, doughnut} (2) Finally, starting with 3-itemsets: - {apple, banana, doughnut} (2) All sets of frequent itemsets with minimum support of 2 are: - {banana} (4) - {doughnut} (4) - {apple} (3) - {coca-cola} (3) - {banana, doughnut} (2) - {apple, doughnut} (2) - {apple, banana} (2) - {banana, coca-cola} (2) - {coca-cola, doughnut} (2) - {apple, banana, doughnut} (2) The Apriori algorithm generates the same set of frequent itemsets with minimum support of 2 as the FP-Growth algorithm. Deriving all association rules with 70% minimum confidence for the frequent itemset {Apple, Banana, Doughnut}: First, find all the subsets of {Apple, Banana, Doughnut}: - {Apple, Banana} - {Apple, Doughnut} - {Banana, Doughnut} - {Apple} - {Banana} - {Doughnut} Next, calculate the confidence for each rule: - {Apple, Banana} -> {Doughnut} (2/2 = 100%) - {Apple, Doughnut} -> {Banana} (2/2 = 100%) - {Banana, Doughnut} -> {Apple} (2/2 = 100%) - {Apple} -> {Banana, Doughnut} (2/3 = 67%) - {Banana} -> {Apple, Doughnut} (2/4 = 50%) - {Doughnut} -> {Apple, Banana} (2/4 = 50%) All association rules with minimum confidence of 70% for the frequent itemset {Apple, Banana, Doughnut} are: - {Apple, Banana} -> {Doughnut} - {Apple, Doughnut} -> {Banana} - {Banana, Doughnut} -> {Apple}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值