Stub Runner Core
Stub Runner Core 运行服务协作方的存根。将存根视为服务契约,可以使 Stub Runner 作为 消费者驱动契约 的实现。
Stub Runner 允许您自动下载所提供依赖项的存根(或从类路径中选择),为它们启动 WireMock 服务器,并向它们提供适当的存根定义。对于消息传递,定义了特殊的存根路由。
获取存根
您可以从以下选项中选择获取存根
-
基于 Aether 的解决方案,从 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"
})
您可以将依赖项添加到类路径,如下所示
<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.thing1</groupId>
<artifactId>thing2</artifactId>
<classifier>superstubs</classifier>
<version>1.0.0</version>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>*</groupId>
<artifactId>*</artifactId>
</exclusion>
</exclusions>
</dependency>
testCompile("com.example:beer-api-producer-restdocs:0.0.1-SNAPSHOT:stubs") {
transitive = false
}
testCompile("com.example.thing1:thing2: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.thing1:thing2,将扫描以下位置
-
/META-INF/com.example.thing1/thing2/*/.*
-
/contracts/com.example.thing1/thing2/*/.*
-
/mappings/com.example.thing1/thing2/*/.*
| 打包生产者存根时,您必须明确提供组 ID 和 artifact 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 存根,而无需下载 artifacts。
配置 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 端口上为给定的 artifact ID 启动 WireMock。以下示例展示了如何实现
@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 = TestSocketUtils.findAvailableTcpPort()
log.info("Will set HTTPs port [" + httpsPort + "] for fraud detection server")
return httpStubConfiguration
.httpsPort(httpsPort)
}
return httpStubConfiguration
}
}
然后您可以将其与 @AutoConfigureStubRunner 注解一起重复使用,如下所示
@AutoConfigureStubRunner(mappingsOutputFolder = "target/outputmappings/",
httpServerStubConfigurer = HttpsForFraudDetection)
无论何时发现 HTTPS 端口,它都会优先于 HTTP 端口。
运行存根
本节描述如何运行存根。它包含以下主题
HTTP 存根
存根在 JSON 文档中定义,其语法在 WireMock 文档 中定义。
以下示例在 JSON 中定义了一个存根
{
"request": {
"method": "GET",
"url": "/ping"
},
"response": {
"status": 200,
"body": "pong",
"headers": {
"Content-Type": "text/plain"
}
}
}
查看已注册的映射
每个存根协作方都会在 __/admin/ 端点下公开一组已定义的映射。
您还可以使用 mappingsOutputFolder 属性将映射转储到文件中。对于基于注解的方法,它类似于以下示例
@AutoConfigureStubRunner(ids="a.b.c:loanIssuance,a.b.c:fraudDetectionServer",
mappingsOutputFolder = "target/outputmappings/")
对于 JUnit 方法,它类似于以下示例
@ClassRule @Shared StubRunnerRule rule = new StubRunnerRule()
.repoRoot("https://some_url")
.downloadStub("a.b.c", "loanIssuance")
.downloadStub("a.b.c:fraudDetectionServer")
.withMappingsOutputFolder("target/outputmappings")
然后,如果您查看 target/outputmappings 文件夹,您将看到以下结构;
.
├── fraudDetectionServer_13705
└── loanIssuance_12255
这意味着注册了两个存根。fraudDetectionServer 在端口 13705 上注册,loanIssuance 在端口 12255 上注册。如果我们查看其中一个文件,我们将看到(对于 WireMock)给定服务器可用的映射
[{
"id" : "f9152eb9-bf77-4c38-8289-90be7d10d0d7",
"request" : {
"url" : "/name",
"method" : "GET"
},
"response" : {
"status" : 200,
"body" : "fraudDetectionServer"
},
"uuid" : "f9152eb9-bf77-4c38-8289-90be7d10d0d7"
},
...
]