SpringCloud 生产者
2023-12-07 16:49 更新
对于生产者,要使用Pact Broker中的Pact文件,我们可以重复使用与外部合同相同的机制。我们通过包含pact://
协议的URL路由Spring Cloud Contract以使用Pact实现。只需将URL传递给Pact Broker。可以在此处找到此类设置的示例。
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>pact://http://localhost:8085</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> <!-- When + is passed, a latest tag will be applied when fetching pacts --> <version>+</version> </contractDependency> <!-- The contracts mode can't be classpath --> <contractsMode>REMOTE</contractsMode> </configuration> <!-- Don't forget to add spring-cloud-contract-pact to the classpath! --> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-contract-pact</artifactId> <version>${spring-cloud-contract.version}</version> </dependency> </dependencies> </plugin>
Gradle.
buildscript { repositories { //... } dependencies { // ... // Don't forget to add spring-cloud-contract-pact to the classpath! classpath "org.springframework.cloud:spring-cloud-contract-pact:${contractVersion}" } } contracts { // When + is passed, a latest tag will be applied when fetching pacts contractDependency { stringNotation = "${project.group}:${project.name}:+" } contractRepository { repositoryUrl = "pact://http://localhost:8085" } // The mode can't be classpath contractsMode = "REMOTE" // Base class mappings etc. }
通过这样的设置:
- 契约文件将从契约代理下载
- Spring Cloud Contract将把Pact文件转换为测试和存根
- 与存根一样的JAR会像往常一样自动创建
以上内容是否对您有帮助:
更多建议: