基于外部仓库的消费者驱动合同

在这个流程中,我们进行消费者驱动的合同测试。合同定义如下 存储在一个独立的仓库中。spring-doc.cadn.net.cn

前提条件

要使用基于消费者的合同和外部仓库中持有的合同,你需要搭建一个git仓库,满足以下功能:spring-doc.cadn.net.cn

更多信息请参见“作指南”部分, 其中我们描述了如何建立这样的仓库。 关于此类项目的示例,请参见此示例spring-doc.cadn.net.cn

你还需要消费者代码,并且已经设置了 Spring Cloud Contract Stub Runner。 关于此类项目的示例,请参见此示例。 你还需要制作人代码,里面有Spring Cloud Contract,并配有插件。 关于此类项目的示例,请参见此示例。 存根存储是Nexus或Artifactory。spring-doc.cadn.net.cn

在高层次,流动如下:spring-doc.cadn.net.cn

  1. 消费者则从独立仓库中作合同定义。spring-doc.cadn.net.cn

  2. 一旦消费者完成工作,就会在消费者身上创建带有工作代码的分支 Side,并且会向存放合同定义的独立仓库发送拉取请求。spring-doc.cadn.net.cn

  3. 生产者接管了拉取请求到带有合同的独立仓库 定义并安装所有合同的JAR。spring-doc.cadn.net.cn

  4. 生产者从本地存储的 JAR 生成测试并写入缺失的 实现以使测试通过。spring-doc.cadn.net.cn

  5. 生产者的工作完成后,调用存储库的 合同定义被合并。spring-doc.cadn.net.cn

  6. CI 工具构建包含合同定义的仓库,JAR 则用 合同定义上传到 Nexus 或 Artifactory,生产者可以合并其分支。spring-doc.cadn.net.cn

  7. 最后,消费者可以转为在线工作,从以下平台获取生产者的存根 远程位置,分支可以合并到主节点。spring-doc.cadn.net.cn

消费者流

  1. 写一个测试,发送请求给制作人。spring-doc.cadn.net.cn

    由于没有服务器存在,测试失败了。spring-doc.cadn.net.cn

  2. 克隆存放合同定义的仓库。spring-doc.cadn.net.cn

  3. 将需求设置为文件夹下的合同,消费者名称作为生产者的子文件夹。spring-doc.cadn.net.cn

    例如,对于一个名为制作人以及一位名为消费者合同将存储在SRC/主/资源/合约/生产者/消费者/)spring-doc.cadn.net.cn

  4. 一旦合同定义,就会将生产者存根安装到本地存储,如下示例所示:spring-doc.cadn.net.cn

    $ cd src/main/resource/contracts/producer
    $ ./mvnw clean install
  5. 在消费者测试中设置春季云合约(SCC)存根运行器,目的是:spring-doc.cadn.net.cn

下图显示了消费者的流程:spring-doc.cadn.net.cn

flow-overview-consumer-cdc-external-consumer

制作流程

  1. 它接管了带合同定义的仓库拉取请求。你可以的 从命令行中,具体如下spring-doc.cadn.net.cn

    $ git checkout -b the_branch_with_pull_request master
    git pull https://github.com/user_id/project_name.git the_branch_with_pull_request
  2. 安装合同定义,具体如下spring-doc.cadn.net.cn

    $ ./mvnw clean install
  3. 设置插件从JAR获取合同定义,而不是从那里获取SRC/测试/资源/合同如下:spring-doc.cadn.net.cn

    梅文
    <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
    }
  4. 运行构建以生成测试和存根,具体如下:spring-doc.cadn.net.cn

    梅文
    ./mvnw clean install
    格拉德勒
    ./gradlew clean build
  5. 写出缺失的实现,使测试通过。spring-doc.cadn.net.cn

  6. 将拉取请求与合同定义合并到仓库,具体如下:spring-doc.cadn.net.cn

    $ 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 master

    CI系统会根据合同定义构建项目,并上传JAR,然后通过 合同定义为Nexus或Artifactory。spring-doc.cadn.net.cn

  7. 转为远程工作。spring-doc.cadn.net.cn

  8. 设置插件后,合同定义不再来自本地 但存储方式来自远程,具体如下:spring-doc.cadn.net.cn

    梅文
    <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
    }
  9. 将生产者代码与新实现合并。spring-doc.cadn.net.cn

  10. CI系统:spring-doc.cadn.net.cn

下图展示了生产者过程:spring-doc.cadn.net.cn

flow-overview-consumer-cdc-external-producer