如何使用 Git 作为 Contract 和 Stub 的存储?

在多语言世界中,有些语言不使用二进制存储,如 Artifactory 和 Nexus 可以。从 Spring Cloud Contract 版本 2.0.0 开始,我们提供 在 SCM (Source Control Management) 存储库中存储合同和存根的机制。目前, 唯一支持的 SCM 是 Git。spring-doc.cadn.net.cn

存储库必须具有以下设置 (您可以从此处查看):spring-doc.cadn.net.cn

.
└── META-INF
    └── com.example
        └── beer-api-producer-git
            └── 0.0.1-SNAPSHOT
                ├── contracts
                │   └── beer-api-consumer
                │       ├── messaging
                │       │   ├── shouldSendAcceptedVerification.groovy
                │       │   └── shouldSendRejectedVerification.groovy
                │       └── rest
                │           ├── shouldGrantABeerIfOldEnough.groovy
                │           └── shouldRejectABeerIfTooYoung.groovy
                └── mappings
                    └── beer-api-consumer
                        └── rest
                            ├── shouldGrantABeerIfOldEnough.json
                            └── shouldRejectABeerIfTooYoung.json

META-INF文件夹:spring-doc.cadn.net.cn

  • 我们按以下方式对应用程序进行分组groupId(例如com.example).spring-doc.cadn.net.cn

  • 每个应用程序都由其artifactId(例如,beer-api-producer-git).spring-doc.cadn.net.cn

  • 接下来,每个应用程序都按其版本(例如0.0.1-SNAPSHOT).开始 从 Spring Cloud Contract 版本2.1.0中,您可以指定版本,如下所示 (假设您的版本遵循语义版本控制):spring-doc.cadn.net.cn

最后,有两个文件夹:spring-doc.cadn.net.cn

  • contracts:好的做法是存储每个 consumer 中具有 consumer 名称(例如beer-api-consumer).这样,您 可以使用stubs-per-consumer特征。进一步的目录结构是任意的。spring-doc.cadn.net.cn

  • mappings:Maven 或 Gradle Spring Cloud Contract 插件推送 此文件夹中的存根服务器映射。在使用者端,Stub Runner 会扫描此文件夹 以启动具有存根定义的存根服务器。文件夹结构是一个副本 在contracts子文件夹。spring-doc.cadn.net.cn

协议约定

要控制合约来源的类型和位置(无论 binary storage 或 SCM 存储库),您可以在 存储库。Spring Cloud Contract 迭代已注册的协议解析器 并尝试获取 Contract (通过使用插件) 或 stub (从 Stub Runner)。spring-doc.cadn.net.cn

对于 SCM 功能,目前我们支持 Git 存储库。要使用它, 在需要放置存储库 URL 的属性中,您必须为存储库 URL 添加前缀 连接 URL 与git://.下面的清单显示了一些示例:spring-doc.cadn.net.cn

git://file:///foo/bar
git://https://github.com/spring-cloud-samples/spring-cloud-contract-nodejs-contracts-git.git
git://[email protected]:spring-cloud-samples/spring-cloud-contract-nodejs-contracts-git.git

制作人

对于生产者,要使用 SCM(源代码控制管理)方法,我们可以重用 我们用于外部合约的机制相同。我们路由 Spring Cloud Contract 使用以 这git://协议。spring-doc.cadn.net.cn

您必须手动添加pushStubsToScmgoal 或使用 (bind)pushStubsToScmtask 中 Gradle 的 Gradle 中。我们不会将存根推送到origin你的 git 存储 库。

以下清单包括 Maven 和 Gradle 构建文件的相关部分:spring-doc.cadn.net.cn

Maven 系列
<plugin>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-contract-maven-plugin</artifactId>
    <version>${spring-cloud-contract.version}</version>
    <extensions>true</extensions>
    <configuration>
        <!-- Base class mappings etc. -->

        <!-- We want to pick contracts from a Git repository -->
        <contractsRepositoryUrl>git://https://github.com/spring-cloud-samples/spring-cloud-contract-nodejs-contracts-git.git</contractsRepositoryUrl>

        <!-- We reuse the contract dependency section to set up the path
        to the folder that contains the contract definitions. In our case the
        path will be /groupId/artifactId/version/contracts -->
        <contractDependency>
            <groupId>${project.groupId}</groupId>
            <artifactId>${project.artifactId}</artifactId>
            <version>${project.version}</version>
        </contractDependency>

        <!-- The contracts mode can't be classpath -->
        <contractsMode>REMOTE</contractsMode>
    </configuration>
    <executions>
        <execution>
            <phase>package</phase>
            <goals>
                <!-- By default we will not push the stubs back to SCM,
                you have to explicitly add it as a goal -->
                <goal>pushStubsToScm</goal>
            </goals>
        </execution>
    </executions>
</plugin>
Gradle
contracts {
	// We want to pick contracts from a Git repository
	contractDependency {
		stringNotation = "${project.group}:${project.name}:${project.version}"
	}
	/*
	We reuse the contract dependency section to set up the path
	to the folder that contains the contract definitions. In our case the
	path will be /groupId/artifactId/version/contracts
	 */
	contractRepository {
		repositoryUrl = "git://https://github.com/spring-cloud-samples/spring-cloud-contract-nodejs-contracts-git.git"
	}
	// The mode can't be classpath
	contractsMode = "REMOTE"
	// Base class mappings etc.
}

/*
In this scenario we want to publish stubs to SCM whenever
the `publish` task is invoked
*/
publish.dependsOn("publishStubsToScm")

您还可以进一步自定义publishStubsToScmgradle 任务。在以下示例中, 该任务经过自定义,可从本地 Git 存储库中选择 Contract:spring-doc.cadn.net.cn

Gradle
publishStubsToScm {
	// We want to modify the default set up of the plugin when publish stubs to scm is called
	// We want to pick contracts from a Git repository
	contractDependency {
		stringNotation = "${project.group}:${project.name}:${project.version}"
	}
	/*
	We reuse the contract dependency section to set up the path
	to the folder that contains the contract definitions. In our case the
	path will be /groupId/artifactId/version/contracts
	 */
	contractRepository {
		repositoryUrl = "git://file://${new File(project.rootDir, "../target")}/contract_empty_git/"
	}
	// We set the contracts mode to `LOCAL`
	contractsMode = "LOCAL"
	}
重要

2.3.0.RELEASEcustomize{}之前用于publishStubsToScm自定义不再可用。应直接应用设置 在publishStubsToScmclosure 的 Curve 方法,如前面的示例所示。spring-doc.cadn.net.cn

使用这样的设置:spring-doc.cadn.net.cn

本地存储 Contract 的 Producer

使用 SCM 作为存根和 Contract 目标的另一种选择是将 与生产者在本地签订合同,并且仅将合同和存根推送到 SCM。 以下项目显示了使用 Maven 和 Gradle 实现此目的所需的设置。spring-doc.cadn.net.cn

使用这样的设置:spring-doc.cadn.net.cn

将与 Producer 的 Contract 和存根保存在外部存储库中

您还可以将合同保留在创建者存储库中,但将存根保留在外部 git 存储库中。 当您想使用基本消费者-生产者协作流但不能使用时,这最有用 使用构件存储库存储存根。spring-doc.cadn.net.cn

为此,请使用通常的 producer 设置,然后添加pushStubsToScm目标和设置contractsRepositoryUrl添加到要保留存根的存储库中。spring-doc.cadn.net.cn

消费者

在消费者端,当传递repositoryRoot参数 要么来自@AutoConfigureStubRunnerannotation、 JUnit 4 规则、JUnit 5 扩展或属性,则可以传递 SCM 存储库,前缀为git://协议。以下示例显示了如何执行此作:spring-doc.cadn.net.cn

@AutoConfigureStubRunner(
    stubsMode="REMOTE",
    repositoryRoot="git://https://github.com/spring-cloud-samples/spring-cloud-contract-nodejs-contracts-git.git",
    ids="com.example:bookstore:0.0.1.RELEASE"
)

使用这样的设置:spring-doc.cadn.net.cn