对于最新的稳定版本,请使用 spring-cloud-contract 4.3.0spring-doc.cadn.net.cn

具有外部存储库中的合同的消费者驱动合同

在此流程中,我们执行消费者驱动的合约测试。合约定义为 存储在单独的存储库中。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. 生产者将拉取请求接管到带有 contract 的单独存储库 定义并在本地安装带有所有协定的 JAR。spring-doc.cadn.net.cn

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

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

  6. CI 工具使用合约定义构建存储库并使用 合约定义上传到 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

    例如,对于名为producer和一个名为consumer,则合约将存储在src/main/resources/contracts/producer/consumer/)spring-doc.cadn.net.cn

  4. 定义合约后,将生产者存根安装到本地存储,如以下示例所示:spring-doc.cadn.net.cn

    $ cd src/main/resource/contracts/producer
    $ ./mvnw clean install
  5. 在消费者测试中设置 Spring Cloud Contract (SCC) Stub Runner,以:spring-doc.cadn.net.cn

以下 UML 图显示了使用者流:spring-doc.cadn.net.cn

流概述-消费者-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 而不是从 JAR 获取合约定义src/test/resources/contracts如下: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>
    Gradle
    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
    Gradle
    ./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 系统使用合约定义构建项目,并使用 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>
    Gradle
    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

以下 UML 图显示了生产者进程:spring-doc.cadn.net.cn

流程概述-消费者-cdc-外部生产者