SpringCloud 非JVM项目中的客户端用法示例

2023-12-11 17:35 更新

我们想使用在“服务器端(nodejs)”步骤中创建的存根假设我们要在端口9876上运行存根。NodeJS代码在这里可用:

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

让我们使用存根运行Stub Runner引导应用程序。

# 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应用程序已启动
  • 它在端口9876上下载了坐标为com.example:bookstore:0.0.1.RELEASE:stubs的存根
  • 它是从Artifactory下载的,运行速度为http://192.168.0.100:8081/artifactory/libs-release-local
  • 过一会儿Stub Runner将在端口8083上运行
  • 并且存根将在端口9876上运行

在服务器端,我们构建了一个有状态的存根。让我们使用curl声明存根已正确设置。

# 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
如果要使用在主机上本地构建的存根,则应传递环境变量-e STUBRUNNER_STUBS_MODE=LOCAL并安装本地m2的卷-v "${HOME}/.m2/:/root/.m2:ro"


以上内容是否对您有帮助:
在线笔记
App下载
App下载

扫描二维码

下载编程狮App

公众号
微信公众号

编程狮公众号