Meh*_*lik 1 java spring mocking microservices
如果MS2是否正在运行,我的MS1一个微服务[ MS2]在启动时会检查另一个微服务[ ]。如果MS2正在运行,则MS1将启动,否则将无法启动。
但是目前,我正在MS2本地计算机上运行,这需要大量的RAMans来降低计算机的速度。
有什么机制可以使当启动MS1并查找MS2时,MS2似乎在运行,而没有实际运行实际的MS2?
更新:
假设MS2正在运行localhost:1234,现在MS1将使用REST连接到它。
您可以看一下WireMock哪个是HTTP API的模拟器,因此它适合于本地开发。使用它时,您将能够模仿微服务,就像它在给定的主机和端口上作为独立微服务运行时一样。
您既可以将其作为独立进程运行,也可以作为spring应用程序的一部分运行。
选项1-配置独立的Wiremock服务器:
1234:java -jar wiremock-standalone-2.24.0.jar --port 1234
Run Code Online (Sandbox Code Playgroud)
localhost:1234用作您的MS2主机。curl -X POST --data '{ "request": { "url": "/yourendpoint", "method": "GET" }, "response": { "status": 200, "body": "Response" }}' http://localhost:1234/__admin/mappings/new
Run Code Online (Sandbox Code Playgroud)
在这里,我们进行了模拟,当您/yourendpoint使用HTTP GET 在模拟服务器上运行时,您将收到文本Response作为响应。
localhost:1234/yourendpoint您会得到模拟的响应:curl http://localhost:1234/yourendpoint
Response
Run Code Online (Sandbox Code Playgroud)
完整的例子可以在下面找到 Wiremock Standalone docs
选项2-在Spring应用程序中配置WireMock服务器:
WireMock dependency到您的项目中(注意,不仅要将其添加到测试范围)@Component
public class CustomMicroserviceMock {
private WireMockServer wireMockServer;
public CustomMicroserviceMock() {
wireMockServer = new WireMockServer(options().port(1234));
wireMockServer.stubFor(get(urlEqualTo("/yourendpoint"))
.willReturn(aResponse()
.withHeader("Content-Type", "text/plain")
.withBody("Response")));
wireMockServer.start();
}
@PreDestroy
void preDestroy() {
wireMockServer.stop();
}
}
Run Code Online (Sandbox Code Playgroud)
localhost:1234/yourendpoint we get response : ResponseThis is just a POC how it could look like but it works in both cases.
| 归档时间: |
|
| 查看次数: |
123 次 |
| 最近记录: |