Skip to content

Commit

Permalink
cola 2.0
Browse files Browse the repository at this point in the history
  • Loading branch information
fulan.zjf committed Aug 26, 2019
1 parent ea84312 commit 9d59107
Show file tree
Hide file tree
Showing 295 changed files with 3,469 additions and 1,928 deletions.
81 changes: 54 additions & 27 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,19 +1,23 @@
![image.png](http://ata2-img.cn-hangzhou.img-pub.aliyun-inc.com/9e7048ef11db23b0579a439676dec4c9.png)

# COLA架构
# COLA 2.0架构
<strong>COLA是Clean Object-Oriented and Layered Architecture的缩写,代表“整洁面向对象分层架构”,也叫“可乐”架构。</strong>
>注意本架构曾用名SOFA、COPA,以后统一使用COLA这个新名称,带来不便,表示抱歉
![image.png](http://ata2-img.cn-hangzhou.img-pub.aliyun-inc.com/9e7048ef11db23b0579a439676dec4c9.png)

>需要访问COLA 1.0,请使用TAG : https://github.com/alibaba/COLA/tree/COLA1.0
更多关于COLA架构的信息:
- COLA架构的设计思想和原理:[请点击](https://blog.csdn.net/significantfrank/article/details/85785565)
- 领域建模的方法论:[请点击](http://blog.csdn.net/significantfrank/article/details/79614915)
目前COLA已经发展到2.0,关于COLA 2.0的详细内容请查看:[请点击](https://blog.csdn.net/significantfrank/article/details/100074716)

需要进一步交流的,可以微信咨询: josico-nie
**需要进一步交流的,可以微信咨询:fsxiaodao**

# 项目说明
COLA框架包括两个Project,一个是cola-framework里面是框架的核心代码,另一个是cola-archetype是用来生成新应用的Maven Archetype源码。
## cola-framework Project
该Project包含3个Module,cola-core, cola-common, cola-test
**COLA既是框架,也是架构。创建COLA的主要目的是为应用架构提供一套简单的可以复制、可以理解、可以落地、可以控制复杂性的”指导和约束"。**
- 框架部分主要是以二方库的形式被应用依赖和使用。
- 架构部分主要是提供了创建符合COLA要求的应用Archetype。

## COLA框架
COLA作为框架主要是提供应用和架构需要的通用组件支撑,比如对CQRS和扩展点功能的支持。COLA框架主要由cola-framework这个项目来实现。
在这个项目里面包含4个Module:cola-core, cola-common, cola-test和cola-extension

### cola-core
该Module是整个框架的核心,里面的主要功能和Package如下:
```
Expand All @@ -25,42 +29,65 @@ com
├── command \\提供Command标准
├── common
├── context \\提供框架执行所需要的上下文
├── convertor \\提供Convertor标准
├── domain \\提供Domain Entity标准
├── event
├── exception \\提供Exception标准
├── extension \\负责扩展机制中的重要概念-扩展(Extension)的定义和执行
├── extensionpoint \\负责扩展机制中的重要概念-扩展点(Extension Point)的定义
├── logger \\提供DIP的日志接口
├── repository \\提供仓储(Repository)的标准
├── rule \\提供业务规则或者叫策略(Rule)的标准和执行
│   └── ruleengine
└── validator \\提供Validator标准和执行
```
### cola-common
该Module提供了框架中Client Object, Entity Object和Data Object的定义,二方库会依赖该Module。

### cola-test
该Module主要是提供一些开发测试的工具,可以使用TDD来进行开发。

## cola-archetype Project
为了提升应用创建的效率,以及保证架构风格和规范被准守。我们开发了创建应用的Maven Archetype,这样只要执行一行命令就可以生成一个基于COLA框架的应用骨架来。
### cola-extension
该Module提供了一些扩展通用功能支持,比如乐观锁、缓存、mybatis的模板等等。

## COLA架构
COLA作为架构,组要是制定了一套指导和约束,并将这套规范沉淀成Archetype。以便通过Archetype可以快速的生成符合COLA规范的应用。满足COLA的应用是一个有清晰的依赖关系的分层架构,如下图所示:

可以按照以下步骤使用COLA Archetype:
![image.png](https://ata2-img.cn-hangzhou.oss-pub.aliyun-inc.com/a33b80bcac5ec73d0d1358d6b49a119c.png)

1. 下载Archetype
我们提供了两个Archetype,分别是cola-archetype-service和cola-archetype-web

2. 安装Archetype到本地:运行 mvn install
### cola-archetype-service
用来生成纯后端应用(没有Controller),生成应用的命令为:
```
mvn archetype:generate -DgroupId=com.alibaba.demo -DartifactId=demo -Dversion=1.0.0-SNAPSHOT -Dpackage=com.alibaba.demo -DarchetypeArtifactId=cola-framework-archetype-service -DarchetypeGroupId=com.alibaba.cola -DarchetypeVersion=2.0.1
```

3. 生成应用,比如,要生成一个名为demo应用,只要执行下面的maven 命令:
### cola-archetype-web
用来生成Web后端应用(有Controller),生成应用的命令为:
```
mvn archetype:generate -DgroupId=com.alibaba.sample -DartifactId=demo -Dversion=1.0.0-SNAPSHOT -Dpackage=com.alibaba.sample -DarchetypeArtifactId=cola-framework-archetype -DarchetypeGroupId=com.alibaba.cola -DarchetypeVersion=1.0.0-SNAPSHOT
mvn archetype:generate -DgroupId=com.alibaba.demo -DartifactId=demo -Dversion=1.0.0-SNAPSHOT -Dpackage=com.alibaba.demo -DarchetypeArtifactId=cola-framework-archetype-web -DarchetypeGroupId=com.alibaba.cola -DarchetypeVersion=2.0.1
```

4. 启动SpringBoot,首先在demo目录下运行mvn install(如果不想运行测试,可以加上-DskipTests参数)。然后进入start目录,执行mvn spring-boot:run
运行成功的话,可以看到SpringBoot启动成功的界面
# 如何使用COLA

## 第一步:生成COLA应用
**1、使用Archetype生成应用:**

直接运行上面提供的Archetype命令就可以生成应用,如果你的Remote Maven Repository里面没有Archetype的Jar包,也可以自己下载Archetype到本地,然后本地运行 mvn install安装。

**2、 检查应用里的模块和组件:**

如果命令执行成功的话,我们可以看到如下的代码结构,**它们就是COLA应用架构。**
![image.png](https://ata2-img.cn-hangzhou.oss-pub.aliyun-inc.com/27569bf9d656f89a32e18d9ef15c85c6.png)

## 第二步:运行Demo
**1、进入在第一步中生成的应用目录。**

**2、启动SpringBoot:**

首先在demo目录下运行mvn install(如果不想运行测试,可以加上-DskipTests参数)。然后进入start目录,执行mvn spring-boot:run
运行成功的话,可以看到SpringBoot启动成功的界面。

**3、 执行测试:**

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

6. 请求执行成功的话,可以在浏览器中的返回值中看到:"customerName":"Hello, World"。同时观察启动SpringBoot的控制台,可以看到LoggerInterceptor打印出来的日志。
**4、查看运行日志:**

查看生成的demo应用,我们可以看到生成了5个module,分别是demo-controller、demo-client、demo-app、demo-domain、demo-infrastructure。其中demo-client是用来存放RPC调用中的DTO(Data Transfer Object)类,其它四个module分别对应控制层、应用层、领域层和基础实施层
请求执行成功的话,可以在浏览器中的返回值中看到:"customerName":"Hello, World"。同时观察启动SpringBoot的控制台,可以看到LoggerInterceptor打印出来的日志
File renamed without changes.
38 changes: 38 additions & 0 deletions cola-archetype-service/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.alibaba.cola</groupId>
<artifactId>cola-framework-archetype-service</artifactId>
<version>2.0.0</version>
<packaging>maven-archetype</packaging>

<name>cola framework archetype</name>

<build>
<extensions>
<extension>
<groupId>org.apache.maven.archetype</groupId>
<artifactId>archetype-packaging</artifactId>
<version>3.0.1</version>
</extension>
</extensions>

<pluginManagement>
<plugins>
<plugin>
<artifactId>maven-archetype-plugin</artifactId>
<version>3.0.1</version>
</plugin>
</plugins>
</pluginManagement>
</build>

<!-- already in parent
<distributionManagement>
<snapshotRepository>
<id>snapshots</id>
<url>http://repo.alibaba-inc.com/mvn/snapshots</url>
</snapshotRepository>
</distributionManagement>
-->
</project>
Original file line number Diff line number Diff line change
@@ -1,24 +1,64 @@
<?xml version="1.0" encoding="UTF-8"?>
<archetype-descriptor xsi:schemaLocation="http://maven.apache.org/plugins/maven-archetype-plugin/archetype-descriptor/1.0.0 http://maven.apache.org/xsd/archetype-descriptor-1.0.0.xsd" name="all"
<archetype-descriptor xsi:schemaLocation="http://maven.apache.org/plugins/maven-archetype-plugin/archetype-descriptor/1.0.0 http://maven.apache.org/xsd/archetype-descriptor-1.0.0.xsd" name="demo"
xmlns="http://maven.apache.org/plugins/maven-archetype-plugin/archetype-descriptor/1.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<fileSets>
<fileSet filtered="true" encoding="UTF-8">
<directory>.idea/libraries</directory>
<includes>
<include>**/*.xml</include>
</includes>
</fileSet>
<fileSet filtered="true" encoding="UTF-8">
<directory>.idea/inspectionProfiles</directory>
<includes>
<include>**/*.xml</include>
</includes>
</fileSet>
<fileSet filtered="true" encoding="UTF-8">
<directory>.idea/codeStyles</directory>
<includes>
<include>**/*.xml</include>
</includes>
</fileSet>
<fileSet filtered="true" encoding="UTF-8">
<directory>.idea</directory>
<includes>
<include>**/*.xml</include>
</includes>
</fileSet>
<fileSet filtered="true" encoding="UTF-8">
<directory>.idea/dictionaries</directory>
<includes>
<include>**/*.xml</include>
</includes>
</fileSet>
<fileSet encoding="UTF-8">
<directory>.idea</directory>
<includes>
<include>**/*.name</include>
</includes>
</fileSet>
<fileSet encoding="UTF-8">
<directory></directory>
<includes>
<include>demo.iml</include>
</includes>
</fileSet>
</fileSets>
<modules>
<module id="${rootArtifactId}-controller" dir="__rootArtifactId__-controller" name="${rootArtifactId}-controller">
<module id="${rootArtifactId}-client" dir="__rootArtifactId__-client" name="${rootArtifactId}-client">
<fileSets>
<fileSet filtered="true" packaged="true" encoding="UTF-8">
<directory>src/main/java</directory>
<includes>
<include>**/*.java</include>
</includes>
</fileSet>
</fileSets>
</module>
<module id="${rootArtifactId}-client" dir="__rootArtifactId__-client" name="${rootArtifactId}-client">
<fileSets>
<fileSet filtered="true" packaged="true" encoding="UTF-8">
<directory>src/main/java</directory>
<fileSet encoding="UTF-8">
<directory></directory>
<includes>
<include>**/*.java</include>
<include>demo-client.iml</include>
</includes>
</fileSet>
</fileSets>
Expand All @@ -31,24 +71,16 @@
<include>**/*.java</include>
</includes>
</fileSet>
<fileSet filtered="true" encoding="UTF-8">
<directory>src/main/resources</directory>
<includes>
<include>**/*.xml</include>
<include>**/*.properties</include>
</includes>
</fileSet>
<fileSet filtered="true" packaged="true" encoding="UTF-8">
<directory>src/test/java</directory>
<includes>
<include>**/*.java</include>
</includes>
</fileSet>
<fileSet filtered="true" encoding="UTF-8">
<directory>src/test/resources</directory>
<fileSet encoding="UTF-8">
<directory></directory>
<includes>
<include>**/*.xml</include>
<include>**/*.properties</include>
<include>demo-app.iml</include>
</includes>
</fileSet>
</fileSets>
Expand All @@ -61,22 +93,16 @@
<include>**/*.java</include>
</includes>
</fileSet>
<fileSet filtered="true" encoding="UTF-8">
<directory>src/main/resources</directory>
<includes>
<include>**/*.properties</include>
</includes>
</fileSet>
<fileSet filtered="true" packaged="true" encoding="UTF-8">
<directory>src/test/java</directory>
<includes>
<include>**/*.java</include>
</includes>
</fileSet>
<fileSet filtered="true" encoding="UTF-8">
<directory>src/test/resources</directory>
<fileSet encoding="UTF-8">
<directory></directory>
<includes>
<include>**/*.properties</include>
<include>demo-domain.iml</include>
</includes>
</fileSet>
</fileSets>
Expand All @@ -93,7 +119,6 @@
<directory>src/main/resources</directory>
<includes>
<include>**/*.xml</include>
<include>**/*.properties</include>
</includes>
</fileSet>
<fileSet filtered="true" packaged="true" encoding="UTF-8">
Expand All @@ -105,10 +130,15 @@
<fileSet filtered="true" encoding="UTF-8">
<directory>src/test/resources</directory>
<includes>
<include>**/*.xml</include>
<include>**/*.properties</include>
</includes>
</fileSet>
<fileSet encoding="UTF-8">
<directory></directory>
<includes>
<include>demo-infrastructure.iml</include>
</includes>
</fileSet>
</fileSets>
</module>
<module id="start" dir="start" name="start">
Expand Down Expand Up @@ -139,6 +169,12 @@
<include>**/*.properties</include>
</includes>
</fileSet>
<fileSet encoding="UTF-8">
<directory></directory>
<includes>
<include>start.iml</include>
</includes>
</fileSet>
</fileSets>
</module>
</modules>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>${groupId}</groupId>
<artifactId>${rootArtifactId}.all</artifactId>
<artifactId>${rootArtifactId}</artifactId>
<version>${version}</version>
<relativePath>../pom.xml</relativePath>
</parent>
Expand All @@ -17,7 +17,7 @@
</dependency>
<dependency>
<groupId>${groupId}</groupId>
<artifactId>${rootArtifactId}-domain</artifactId>
<artifactId>${rootArtifactId}-infrastructure</artifactId>
</dependency>
<!-- JSR 303 Validation -->
<dependency>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
#set( $symbol_pound = '#' )
#set( $symbol_dollar = '$' )
#set( $symbol_escape = '\' )
package ${package}.event.handler;
package ${package}.eventhandler;

import com.alibaba.cola.dto.Response;
import com.alibaba.cola.event.EventHandler;
import com.alibaba.cola.event.EventHandlerI;
import com.alibaba.cola.logger.Logger;
Expand All @@ -16,13 +17,14 @@
* @date 2019/04/09
*/
@EventHandler
public class CustomerCreatedHandler implements EventHandlerI<CustomerCreatedEvent> {
public class CustomerCreatedHandler implements EventHandlerI<Response, CustomerCreatedEvent> {

private Logger logger = LoggerFactory.getLogger(CustomerCreatedHandler.class);

@Override
public void execute(CustomerCreatedEvent event) {
public Response execute(CustomerCreatedEvent event) {
logger.debug("Sync new customer to Search");
logger.debug("Logging system operation for new customer");
return Response.buildSuccess();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#set( $symbol_pound = '#' )
#set( $symbol_dollar = '$' )
#set( $symbol_escape = '\' )
package ${package}.executor;


import com.alibaba.cola.command.Command;
import com.alibaba.cola.command.CommandExecutorI;
import com.alibaba.cola.domain.DomainEventServiceI;
import com.alibaba.cola.dto.Response;
import com.alibaba.cola.exception.BizException;
import ${package}.dto.CustomerAddCmd;
import ${package}.dto.domainmodel.ErrorCode;
import org.springframework.beans.factory.annotation.Autowired;

@Command
public class CustomerAddCmdExe implements CommandExecutorI<Response, CustomerAddCmd>{

@Override
public Response execute(CustomerAddCmd cmd) {
//The flow of usecase is defined here.
//The core ablility should be implemented in Domain. or sink to Domian gradually
if(cmd.getCustomer().getCompanyName().equals("ConflictCompanyName")){
throw new BizException(ErrorCode.B_CUSTOMER_companyNameConflict, "公司名冲突");
}
return Response.buildSuccess();
}

}
Loading

0 comments on commit 9d59107

Please sign in to comment.