短跑者核心
存根运行核心为服务协作者运行存根。将存根视为 服务功能让你用存根跑者作为消费者驱动合同的实现。
Stub Runner 允许你自动下载提供的依赖(或 从classpath中选择这些服务器),为它们启动WireMock服务器,并用适当的方式供给它们 简短定义。对于消息传递,定义了特殊的存根路由。
获取小作品
您可以从以下几种方式中选择获取存根:
-
基于Aether的解决方案,可以从Artifactory或Nexus下载带有存根的JAR。
-
类路径扫描解决方案,通过模式搜索类路径以检索存根
-
编写你自己的实现
org.springframework.cloud.contract.stubrunner.StubDownloaderBuilder实现完全自定义
后一个例子在自定义存根跑道部分有描述。
下载小作品
你可以用以下方式控制存根的下载小作品模式开关。它从中选择价值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)
类路径扫描
如果你设置小作品模式属性到StubRunnerProperties.StubsMode.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, 制片人小作品。 |
为了实现合适的存根包装,生产商会按以下方式制定合同:
└── src
└── test
└── resources
└── contracts
└── com.example
└── beer-api-producer-restdocs
└── nested
└── contract3.groovy
通过使用梅文集会插件或者Gradle Jar任务,你需要创建以下内容
你的存根罐中的结构:
└── META-INF
└── com.example
└── beer-api-producer-restdocs
└── 2.0.0
├── contracts
│ └── nested
│ └── contract2.groovy
└── mappings
└── mapping.json
通过保持这种结构,类路径会被扫描,你可以从消息传递中获益。 无需下载伪影即可获得 HTTP 存根。
配置HTTP服务器存根
Stub Runner 有一个概念HttpServerStub抽象化了底层
HTTP服务器的具体实现(例如,WireMock就是其中一种实现)。
有时,你需要对存根服务器进行额外的调优(这对具体实现来说是具体的)。
为此,Stub Runner 给了你
这httpServerStubConfigurer注释中可用的属性
JUnit规则,可以通过系统属性访问,你可以提供
你对org.springframework.cloud.contract.stubrunner.HttpServerStubConfigurer接口。实现方式可能会有所变化
给定HTTP服务器存根的配置文件。
Spring Cloud 合同存根运行器自带一个实现,你可以
可以扩展到WireMock:org.springframework.cloud.contract.stubrunner.provider.wiremock.WireMockHttpServerStubConfigurer.
在配置方法
你可以为给出的存根提供自定义配置。用途
这种情况可能是在HTTPS端口上启动给定工件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"
}
}
}
查看注册地图
每个 stubbed 协作者都会在__/管理/端点。
你也可以使用映射输出文件夹属性,用于将映射转储到文件。
对于基于注释的方法,类似以下示例:
@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")
然后,如果你查看目标/输出映射文件夹中,你会看到以下结构;
.
├── fraudDetectionServer_13705
└── loanIssuance_12255
这意味着有两个存根登记。fraudDetectionServer在端口注册13705和贷款发行端口12255.如果我们查看其中一个文件,会发现(对于WireMock来说)
给定服务器可用的映射:
[{
"id" : "f9152eb9-bf77-4c38-8289-90be7d10d0d7",
"request" : {
"url" : "/name",
"method" : "GET"
},
"response" : {
"status" : 200,
"body" : "fraudDetectionServer"
},
"uuid" : "f9152eb9-bf77-4c38-8289-90be7d10d0d7"
},
...
]