SpringCloud 非JVM項目中的客戶端用法示例

2023-12-11 17:35 更新

我們想使用在“服務(wù)器端(nodejs)”步驟中創(chuàng)建的存根。假設(shè)我們要在端口9876上運行存根。NodeJS代碼在這里可用:

$ git clone https://github.com/spring-cloud-samples/spring-cloud-contract-nodejs
$ cd bookstore

讓我們使用存根運行Stub Runner引導(dǎo)應(yīng)用程序。

# Provide the Spring Cloud Contract Docker version
$ SC_CONTRACT_DOCKER_VERSION="..."
# The IP at which the app is running and Docker container can reach it
$ APP_IP="192.168.0.100"
# Spring Cloud Contract Stub Runner properties
$ STUBRUNNER_PORT="8083"
# Stub coordinates 'groupId:artifactId:version:classifier:port'
$ STUBRUNNER_IDS="com.example:bookstore:0.0.1.RELEASE:stubs:9876"
$ STUBRUNNER_REPOSITORY_ROOT="http://${APP_IP}:8081/artifactory/libs-release-local"
# Run the docker with Stub Runner Boot
$ docker run  --rm -e "STUBRUNNER_IDS=${STUBRUNNER_IDS}" -e "STUBRUNNER_REPOSITORY_ROOT=${STUBRUNNER_REPOSITORY_ROOT}" -e "STUBRUNNER_STUBS_MODE=REMOTE" -p "${STUBRUNNER_PORT}:${STUBRUNNER_PORT}" -p "9876:9876" springcloud/spring-cloud-contract-stub-runner:"${SC_CONTRACT_DOCKER_VERSION}"

這是怎么回事

  • 一個獨立的Stub Runner應(yīng)用程序已啟動
  • 它在端口9876上下載了坐標為com.example:bookstore:0.0.1.RELEASE:stubs的存根
  • 它是從Artifactory下載的,運行速度為http://192.168.0.100:8081/artifactory/libs-release-local
  • 過一會兒Stub Runner將在端口8083上運行
  • 并且存根將在端口9876上運行

在服務(wù)器端,我們構(gòu)建了一個有狀態(tài)的存根。讓我們使用curl聲明存根已正確設(shè)置。

# let's execute the first request (no response is returned)
$ curl -H "Content-Type:application/json" -X POST --data '{ "title" : "Title", "genre" : "Genre", "description" : "Description", "author" : "Author", "publisher" : "Publisher", "pages" : 100, "image_url" : "https://d213dhlpdb53mu.cloudfront.net/assets/pivotal-square-logo-41418bd391196c3022f3cd9f3959b3f6d7764c47873d858583384e759c7db435.svg", "buy_url" : "https://pivotal.io" }' http://localhost:9876/api/books
# Now time for the second request
$ curl -X GET http://localhost:9876/api/books
# You will receive contents of the JSON
如果要使用在主機上本地構(gòu)建的存根,則應(yīng)傳遞環(huán)境變量-e STUBRUNNER_STUBS_MODE=LOCAL并安裝本地m2的卷-v "${HOME}/.m2/:/root/.m2:ro"


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

掃描二維碼

下載編程獅App

公眾號
微信公眾號

編程獅公眾號