SpringCloud 檢索存根

2023-12-11 17:07 更新

您可以選擇以下獲取存根的選項

  • 基于醚的解決方案,可從Artifactory / Nexus下載帶有存根的JAR
  • 類路徑掃描解決方案,可通過模式搜索類路徑以檢索存根
  • 編寫自己的org.springframework.cloud.contract.stubrunner.StubDownloaderBuilder實現(xiàn)以進行完全自定義

后一個示例在“ 自定義Stub Runner”部分中進行了描述。

存根下載

您可以通過stubsMode開關(guān)控制存根下載。它從StubRunnerProperties.StubsMode枚舉中選擇值。您可以使用以下選項

  • StubRunnerProperties.StubsMode.CLASSPATH(默認值)-將從類路徑中選擇存根
  • StubRunnerProperties.StubsMode.LOCAL-將從本地存儲區(qū)中選擇存根(例如.m2
  • StubRunnerProperties.StubsMode.REMOTE-將從遠程位置選擇存根

例:

@AutoConfigureStubRunner(repositoryRoot="https://foo.bar", ids = "com.example:beer-api-producer:+:stubs:8095", stubsMode = StubRunnerProperties.StubsMode.LOCAL)

類路徑掃描

如果將stubsMode屬性設(shè)置為StubRunnerProperties.StubsMode.CLASSPATH(或由于默認值CLASSPATH而未設(shè)置任何內(nèi)容),則將掃描類路徑。讓我們看下面的例子:

@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/ * / *。

 如您所見,打包生產(chǎn)者存根時必須顯式提供組和工件ID。

生產(chǎn)者將像這樣設(shè)置合同:

└── src
    └── test
        └── resources
            └── contracts
                └── com.example
                    └── beer-api-producer-restdocs
                        └── nested
                            └── contract3.groovy

要實現(xiàn)正確的存根包裝。

或使用Maven assembly插件Gradle Jar任務(wù),您必須在存根jar中創(chuàng)建以下結(jié)構(gòu)。

└── META-INF
    └── com.example
        └── beer-api-producer-restdocs
            └── 2.0.0
                ├── contracts
                │   └── nested
                │       └── contract2.groovy
                └── mappings
                    └── mapping.json

通過維護這種結(jié)構(gòu),可以掃描類路徑,而無需下載工件即可從消息傳遞/ HTTP存根中受益。

配置HTTP服務(wù)器存根

Stub Runner具有HttpServerStub的概念,該概念抽象了HTTP服務(wù)器的底層具體實現(xiàn)(例如,WireMock是實現(xiàn)之一)。有時,您需要對存根服務(wù)器執(zhí)行一些其他調(diào)整,這對于給定的實現(xiàn)而言是具體的。為此,Stub Runner為您提供了httpServerStubConfigurer屬性,該屬性在批注JUnit規(guī)則中可用,并且可以通過系統(tǒng)屬性進行訪問,您可以在其中提供org.springframework.cloud.contract.stubrunner.HttpServerStubConfigurer接口的實現(xiàn)。這些實現(xiàn)可以更改給定HTTP服務(wù)器存根的配置文件。

Spring Cloud Contract Stub Runner帶有一個可以擴展的實現(xiàn),適用于WireMock-org.springframework.cloud.contract.stubrunner.provider.wiremock.WireMockHttpServerStubConfigurer。configure方法中,您可以為給定的存根提供自己的自定義配置。用例可能是在HTTPs端口上為給定的工件ID啟動WireMock。例:

WireMockHttpServerStubConfigurer實現(xiàn)。 

@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端口,它將優(yōu)先于http端口。

以上內(nèi)容是否對您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號
微信公眾號

編程獅公眾號