SpringCloud 检索存根
您可以选择以下获取存根的选项
- 基于醚的解决方案,可从Artifactory / Nexus下载带有存根的JAR
- 类路径扫描解决方案,可通过模式搜索类路径以检索存根
- 编写自己的
org.springframework.cloud.contract.stubrunner.StubDownloaderBuilder实现以进行完全自定义
后一个示例在“ 自定义Stub Runner”部分中进行了描述。
您可以通过stubsMode开关控制存根下载。它从StubRunnerProperties.StubsMode枚举中选择值。您可以使用以下选项
StubRunnerProperties.StubsMode.CLASSPATH(默认值)-将从类路径中选择存根StubRunnerProperties.StubsMode.LOCAL-将从本地存储区中选择存根(例如.m2)StubRunnerProperties.StubsMode.REMOTE-将从远程位置选择存根
例:
@AutoConfigureStubRunner(repositoryRoot="https://foo.bar", ids = "com.example:beer-api-producer:+:stubs:8095", stubsMode = StubRunnerProperties.StubsMode.LOCAL)
如果将stubsMode属性设置为StubRunnerProperties.StubsMode.CLASSPATH(或由于默认值CLASSPATH而未设置任何内容),则将扫描类路径。让我们看下面的例子:
@AutoConfigureStubRunner(ids = {
"com.example:beer-api-producer:+:stubs:8095",
"com.example.foo:bar:1.0.0:superstubs:8096"
})如果您已将依赖项添加到类路径中
Maven.
<dependency> <groupId>com.example</groupId> <artifactId>beer-api-producer-restdocs</artifactId> <classifier>stubs</classifier> <version>0.0.1-SNAPSHOT</version> <scope>test</scope> <exclusions> <exclusion> <groupId>*</groupId> <artifactId>*</artifactId> </exclusion> </exclusions> </dependency> <dependency> <groupId>com.example.foo</groupId> <artifactId>bar</artifactId> <classifier>superstubs</classifier> <version>1.0.0</version> <scope>test</scope> <exclusions> <exclusion> <groupId>*</groupId> <artifactId>*</artifactId> </exclusion> </exclusions> </dependency>
Gradle.
testCompile("com.example:beer-api-producer-restdocs:0.0.1-SNAPSHOT:stubs") { transitive = false } testCompile("com.example.foo:bar:1.0.0:superstubs") { transitive = false }
然后,将扫描您的类路径上的以下位置。对于com.example:beer-api-producer-restdocs
- /META-INF/com.example/beer-api-producer-restdocs/ * / *。
- /contracts/com.example/beer-api-producer-restdocs/ * / *。
- /mappings/com.example/beer-api-producer-restdocs/ * / *。
和com.example.foo:bar
- /META-INF/com.example.foo/bar/ * / *。
- /contracts/com.example.foo/bar/ * / *。
- /mappings/com.example.foo/bar/ * / *。
如您所见,打包生产者存根时必须显式提供组和工件ID。
生产者将像这样设置合同:
└── src
└── test
└── resources
└── contracts
└── com.example
└── beer-api-producer-restdocs
└── nested
└── contract3.groovy要实现正确的存根包装。
或使用Maven assembly插件或
Gradle Jar任务,您必须在存根jar中创建以下结构。
└── META-INF
└── com.example
└── beer-api-producer-restdocs
└── 2.0.0
├── contracts
│ └── nested
│ └── contract2.groovy
└── mappings
└── mapping.json通过维护这种结构,可以扫描类路径,而无需下载工件即可从消息传递/ HTTP存根中受益。
Stub Runner具有HttpServerStub的概念,该概念抽象了HTTP服务器的底层具体实现(例如,WireMock是实现之一)。有时,您需要对存根服务器执行一些其他调整,这对于给定的实现而言是具体的。为此,Stub Runner为您提供了httpServerStubConfigurer属性,该属性在批注JUnit规则中可用,并且可以通过系统属性进行访问,您可以在其中提供org.springframework.cloud.contract.stubrunner.HttpServerStubConfigurer接口的实现。这些实现可以更改给定HTTP服务器存根的配置文件。
Spring Cloud Contract Stub Runner带有一个可以扩展的实现,适用于WireMock-org.springframework.cloud.contract.stubrunner.provider.wiremock.WireMockHttpServerStubConfigurer。在configure方法中,您可以为给定的存根提供自己的自定义配置。用例可能是在HTTPs端口上为给定的工件ID启动WireMock。例:
WireMockHttpServerStubConfigurer实现。
@CompileStatic static class HttpsForFraudDetection extends WireMockHttpServerStubConfigurer { private static final Log log = LogFactory.getLog(HttpsForFraudDetection) @Override WireMockConfiguration configure(WireMockConfiguration httpStubConfiguration, HttpServerStubConfiguration httpServerStubConfiguration) { if (httpServerStubConfiguration.stubConfiguration.artifactId == "fraudDetectionServer") { int httpsPort = SocketUtils.findAvailableTcpPort() log.info("Will set HTTPs port [" + httpsPort + "] for fraud detection server") return httpStubConfiguration .httpsPort(httpsPort) } return httpStubConfiguration } }
然后,您可以通过注释重用它
@AutoConfigureStubRunner(mappingsOutputFolder = "target/outputmappings/",
httpServerStubConfigurer = HttpsForFraudDetection)只要找到一个https端口,它将优先于http端口。