Using the Stub Runner Boot Application
Spring Cloud Contract Stub Runner Boot 是一个 Spring Boot 应用程序,它公开 REST 端点以触发消息传递标签并访问 WireMock 服务器。
Spring Cloud Contract Stub Runner Boot is a Spring Boot application that exposes REST endpoints to trigger the messaging labels and to access WireMock servers.
Stub Runner Boot Security
Stub Runner Boot 应用程序在设计上并不安全 - 为其提供安全性需要向所有存根添加安全性,即使它们实际上并不需要。由于这是一种测试实用程序 - 因此服务器*并非打算*用于生产环境。
The Stub Runner Boot application is not secured by design - securing it would require to add security to all stubs even if they don’t actually require it. Since this is a testing utility - the server is not intended to be used in production environments.
希望 only a trusted client 具有对 Stub Runner Boot 服务器的访问权限。您不应将此应用程序作为胖 JAR 或 Docker Image 在不受信任的位置运行。
It is expected that only a trusted client has access to the Stub Runner Boot server. You should not run this application as a Fat Jar or a Docker Image in untrusted locations.
Stub Runner Server
要使用 Stub Runner Server,请添加以下依赖项:
To use the Stub Runner Server, add the following dependency:
compile "org.springframework.cloud:spring-cloud-starter-stub-runner"
然后用 @EnableStubRunnerServer
注释一个类,构建一个 fat jar,它即可工作。
Then annotate a class with @EnableStubRunnerServer
, build a fat jar, and it is ready to work.
关于属性,请参阅 Stub Runner Spring 部分。
For the properties, see the Stub Runner Spring section.
Stub Runner Server Fat Jar
您可以通过运行以下命令从 Maven 下载一个单独的 JAR(例如,对于版本 2.0.1.RELEASE):
You can download a standalone JAR from Maven (for example, for version 2.0.1.RELEASE) by running the following commands:
$ wget -O stub-runner.jar 'https://search.maven.org/remotecontent?filepath=org/springframework/cloud/spring-cloud-contract-stub-runner-boot/2.0.1.RELEASE/spring-cloud-contract-stub-runner-boot-2.0.1.RELEASE.jar'
$ java -jar stub-runner.jar --stubrunner.ids=... --stubrunner.repositoryRoot=...
Spring Cloud CLI
从 1.4.0.RELEASE
版本的 Spring Cloud CLI 项目开始,您可以通过运行 spring cloud stubrunner
来启动 Stub Runner Boot。
Starting from the 1.4.0.RELEASE
version of the Spring Cloud CLI
project, you can start Stub Runner Boot by running spring cloud stubrunner
.
要传递配置,您可以在当前工作目录、名为 config
的子目录或 ~/.spring-cloud
中创建一个 stubrunner.yml
文件。如果本地安装了存根,则该文件可以类似于以下示例:
To pass the configuration, you can create a stubrunner.yml
file in the current working directory,
in a subdirectory called config
, or in ~/.spring-cloud
. The file could resemble the following
example for running stubs installed locally:
stubrunner:
stubsMode: LOCAL
ids:
- com.example:beer-api-producer:+:9876
然后您可以在终端窗口中调用 spring cloud stubrunner
以启动 Stub Runner 服务器。该服务器在端口 8750
可用。
Then you can call spring cloud stubrunner
from your terminal window to start
the Stub Runner server. It is available at port 8750
.
Endpoints
Stub Runner Boot 提供两个端点:
Stub Runner Boot offers two endpoints:
HTTP
对于 HTTP,Stub Runner Boot 提供了以下端点:
For HTTP, Stub Runner Boot makes the following endpoints available:
-
GET
/stubs
: Returns a list of all running stubs inivy:integer
notation -
GET
/stubs/{ivy}
: Returns a port for the givenivy
notation (when calling the endpointivy
can also beartifactId
only)
Messaging
对于消息传递,Stub Runner Boot 提供了以下端点:
For Messaging, Stub Runner Boot makes the following endpoints available:
-
GET
/triggers
: Returns a list of all running labels inivy : [ label1, label2 …]
notation -
POST
/triggers/{label}
: Runs a trigger withlabel
-
POST
/triggers/{ivy}/{label}
: Runs a trigger with alabel
for the givenivy
notation (when calling the endpoint,ivy
can also beartifactId
only)
Example
以下示例显示了 Stub Runner Boot 的典型用法:
The following example shows typical usage of Stub Runner Boot:
Unresolved directive in stub-runner-boot.adoc - include::{stubrunner_core_path}/src/test/groovy/org/springframework/cloud/contract/stubrunner/server/StubRunnerBootSpec.groovy[]
Stub Runner Boot with Service Discovery
使用 Stub Runner Boot 的一种方法是将其用作 “smoke test” 的存根馈送。这是什么意思?假设您不想部署 50 个微服务到测试环境中,以查看您的应用程序是否工作。您已经在构建过程中运行了一套测试,但您还想确保应用程序的打包工作正常。您可以将您的应用程序部署到某个环境、启动它并在其上运行一些测试,以查看它是否正常工作。我们可以将这些测试称为 "`smoke test`”,因为它们的目的是只检查少数测试场景。
One way to use Stub Runner Boot is to use it as a feed of stubs for “smoke tests”. What does that mean? Assume that you do not want to deploy 50 microservices to a test environment in order to see whether your application works. You have already run a suite of tests during the build process, but you would also like to ensure that the packaging of your application works. You can deploy your application to an environment, start it, and run a couple of tests on it to see whether it works. We can call those tests “smoke tests”, because their purpose is to check only a handful of testing scenarios.
这种方法的问题在于,如果您使用微服务,则很可能也会使用服务发现工具。Stub Runner Boot 允许您通过启动所需的存根并将它们注册到服务发现工具来解决此问题。
The problem with this approach is that, if you use microservices, you most likely also use a service discovery tool. Stub Runner Boot lets you solve this issue by starting the required stubs and registering them in a service discovery tool.
现在假设我们想要启动此应用程序,以便自动注册存根。我们可以使用 java -jar ${SYSTEM_PROPS} stub-runner-boot-eureka-example.jar
运行该应用程序来实现此目的,其中 ${SYSTEM_PROPS}
。
Now assume that we want to start this application so that the stubs get automatically registered.
We can do so by running the application with java -jar ${SYSTEM_PROPS} stub-runner-boot-eureka-example.jar
, where
${SYSTEM_PROPS}
.
这样,您的已部署应用程序可以通过服务发现向已启动的 WireMock 服务器发送请求。很可能,第 1 至第 3 点可以在 application.yml
中设置为默认值,因为它们不太可能改变。这样,您可以在启动 Stub Runner Boot 时只提供要下载的存根的列表。
That way, your deployed application can send requests to started WireMock servers through service
discovery. Most likely, points 1 through 3 could be set by default in application.yml
, because they are not
likely to change. That way, you can provide only the list of stubs to download whenever you start
the Stub Runner Boot.