|
该版本仍在开发中,尚未被视为稳定。对于最新的稳定版本,请使用 spring-cloud-contract 5.0.0! |
基于外部仓库的消费者驱动合同
在这个流程中,我们进行消费者驱动的合同测试。合同定义如下 存储在一个独立的仓库中。
前提条件
要使用基于消费者的合同和外部仓库中持有的合同,你需要搭建一个git仓库,满足以下功能:
-
包含每个生产者的所有合同定义。
-
可以把合同定义打包在 JAR 里。
-
对于每个契约生产者,包含一种方式(例如,
pom.xml)以安装存根 本地通过 Spring Cloud Contract 插件(SCC 插件)实现。
你还需要消费者代码,并且已经设置了 Spring Cloud Contract Stub Runner。 关于此类项目的示例,请参见此示例。 你还需要制作人代码,里面有Spring Cloud Contract,并配有插件。 关于此类项目的示例,请参见此示例。 存根存储是Nexus或Artifactory。
在高层次,流动如下:
-
消费者则从独立仓库中作合同定义。
-
一旦消费者完成工作,就会在消费者身上创建带有工作代码的分支 Side,并且会向存放合同定义的独立仓库发送拉取请求。
-
生产者接管了拉取请求到带有合同的独立仓库 定义并安装所有合同的JAR。
-
生产者从本地存储的 JAR 生成测试并写入缺失的 实现以使测试通过。
-
生产者的工作完成后,调用存储库的 合同定义被合并。
-
CI 工具构建包含合同定义的仓库,JAR 则用 合同定义上传到 Nexus 或 Artifactory,生产者可以合并其分支。
-
最后,消费者可以转为在线工作,从以下平台获取生产者的存根 远程位置,分支可以合并到主节点。
消费者流
消费者:
-
写一个测试,发送请求给制作人。
由于没有服务器存在,测试失败了。
-
克隆存放合同定义的仓库。
-
将需求设置为文件夹下的合同,消费者名称作为生产者的子文件夹。
例如,对于一个名为
制作人以及一位名为消费者合同将存储在SRC/主/资源/合约/生产者/消费者/) -
一旦合同定义,就会将生产者存根安装到本地存储,如下示例所示:
$ cd src/main/resource/contracts/producer $ ./mvnw clean install -
在消费者测试中设置春季云合约(SCC)存根运行器,目的是:
-
从本地存储中获取生产者的存根。
-
在每个消费者的存根模式下工作(这支持了消费者驱动的合约模式)。
SCC短篇跑者:
-
拿制片人的存根。
-
运行内存内的 HTTP 服务器存根,包含生产者存根。 现在你的测试与HTTP服务器存根通信,测试通过。
-
向仓库创建拉取请求,定义合同,并包含生产者的新合同。
-
它分支你的消费者代码,直到制作团队合并他们的代码。
-
下图显示了消费者的流程:
制作流程
制片人:
-
它接管了带合同定义的仓库拉取请求。你可以的 从命令行中,具体如下
$ git checkout -b the_branch_with_pull_request master git pull https://github.com/user_id/project_name.git the_branch_with_pull_request -
安装合同定义,具体如下
$ ./mvnw clean install -
设置插件从JAR获取合同定义,而不是从那里获取
SRC/测试/资源/合同如下:- 梅文
-
<plugin> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-contract-maven-plugin</artifactId> <version>${spring-cloud-contract.version}</version> <extensions>true</extensions> <configuration> <!-- We want to use the JAR with contracts with the following coordinates --> <contractDependency> <groupId>com.example</groupId> <artifactId>beer-contracts</artifactId> </contractDependency> <!-- The JAR with contracts should be taken from Maven local --> <contractsMode>LOCAL</contractsMode> <!-- ... additional configuration --> </configuration> </plugin> - 格拉德勒
-
contracts { // We want to use the JAR with contracts with the following coordinates // group id `com.example`, artifact id `beer-contracts`, LATEST version and NO classifier contractDependency { stringNotation = 'com.example:beer-contracts:+:' } // The JAR with contracts should be taken from Maven local contractsMode = "LOCAL" // Additional configuration }
-
运行构建以生成测试和存根,具体如下:
- 梅文
-
./mvnw clean install - 格拉德勒
-
./gradlew clean build
-
写出缺失的实现,使测试通过。
-
将拉取请求与合同定义合并到仓库,具体如下:
$ git commit -am "Finished the implementation to make the contract tests pass" $ git checkout master $ git merge --no-ff the_branch_with_pull_request $ git push origin masterCI系统会根据合同定义构建项目,并上传JAR,然后通过 合同定义为Nexus或Artifactory。
-
转为远程工作。
-
设置插件后,合同定义不再来自本地 但存储方式来自远程,具体如下:
- 梅文
-
<plugin> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-contract-maven-plugin</artifactId> <version>${spring-cloud-contract.version}</version> <extensions>true</extensions> <configuration> <!-- We want to use the JAR with contracts with the following coordinates --> <contractDependency> <groupId>com.example</groupId> <artifactId>beer-contracts</artifactId> </contractDependency> <!-- The JAR with contracts should be taken from a remote location --> <contractsMode>REMOTE</contractsMode> <!-- ... additional configuration --> </configuration> </plugin> - 格拉德勒
-
contracts { // We want to use the JAR with contracts with the following coordinates // group id `com.example`, artifact id `beer-contracts`, LATEST version and NO classifier contractDependency { stringNotation = 'com.example:beer-contracts:+:' } // The JAR with contracts should be taken from a remote location contractsMode = "REMOTE" // Additional configuration }
-
将生产者代码与新实现合并。
-
CI系统:
-
构建项目。
-
生成测试、存根和存根JAR。
-
将应用和存根一起上传到Nexus或Artifactory。
-
下图展示了生产者过程: