我们有一个Web服务A
(Java 17
,spring-cloud-starter-openfeign: 3.1.6
,spring-cloud-starter-netflix-eureka-client: 3.1.6
,testcontainers: 1.18.0
,wiremock: 2.35.0
),它使用openfeign调用服务B
。由于服务B
在Eureka 中注册,因此我们使用下面的openfeign客户端向B
服务发送调用:
@FeignClient("SESSION-MANAGER")
public interface SessionManagerClient {
@PostMapping(path = "/session/get", consumes = "application/json")
Session getSession(SessionRequstBody sessionRequstBody);
}
Openfeign配置(* 我们没有任何其他openfeign配置或beans*):
feign:
client:
config:
default:
connectTimeout: 600000
readTimeout: 600000
logger-level: basic
这段代码通过SESSION-MANAGER
名称在Eureka 中找到相应的服务并发送请求。它在环境中运行良好。但是我不太明白如何在集成测试中模拟这个/session/get
调用(使用wiremock)。因为在代码调用和openfeign客户端之间存在Eureka (隐式)。因此,由于它openfeign认为@FeignClient("SESSION-MANAGER")
作为主机,并发送这样的请求http://SESSION-MANAGER/session/get
导致此错误:
[WARN ] 2023-06-26 13:56:52.069 [simpleMessageListenerContainer-2] o.s.c.l.c.RoundRobinLoadBalancer - No servers available for service: SESSION-MANAGER
[DEBUG] 2023-06-26 13:56:52.070 [simpleMessageListenerContainer-2] o.s.c.o.l.RetryableFeignBlockingLoadBalancerClient - Selected service instance: null
[WARN ] 2023-06-26 13:56:52.070 [simpleMessageListenerContainer-2] o.s.c.o.l.RetryableFeignBlockingLoadBalancerClient - Service instance was not resolved, executing the original request
[ERROR] 2023-06-26 13:56:52.074 [simpleMessageListenerContainer-2] c.o.t.s.s.MyService - Error in getting Session
feign.RetryableException: SESSION-MANAGER executing POST http://SESSION-MANAGER/session/get
at feign.FeignException.errorExecuting(FeignException.java:268)
...
at java.base/java.lang.Thread.run(Thread.java:833)
Caused by: java.net.UnknownHostException: SESSION-MANAGER
at java.base/sun.nio.ch.NioSocketImpl.connect(NioSocketImpl.java:567)
...
at feign.SynchronousMethodHandler.executeAndDecode(SynchronousMethodHandler.java:121)
... 42 common frames omitted
[DEBUG] 2023-06-26 13:56:52.386 [SpringApplicationShutdownHook] o.s.w.c.s.GenericWebApplicationContext - Closing org.springframework.web.context.support.GenericWebApplicationContext@748904e8, started on Mon Jun 26 13:56:49 EEST 2023
[INFO ] 2023-06-26 13:56:52.386 [SpringApplicationShutdownHook] o.s.c.n.e.s.EurekaServiceRegistry - Unregistering application TELEGRAM-SCRAPER-CLIENT with eureka with status DOWN
问题是你能帮我理解在这种情况下我应该嘲笑什么吗?我是否应该直接模拟对服务B
的调用,或者我甚至应该在Testcontainers中升级Eureka 并在这个级别上管理它?
Eureka 配置:
eureka:
client:
registerWithEureka: false
fetchRegistry: true
healthcheck:
enabled: false
serviceUrl:
defaultZone: "http://localhost:8761/eureka/"
instance:
statusPageUrl: "http://${eureka.hostname}/info"
healthCheckUrl: "http://${eureka.hostname}/health"
homePageUrl: "http://${eureka.hostname}/"
nonSecurePortEnabled: true
Wiremock:
WIREMOCK_SERVER.stubFor(WireMock.post(WireMock.urlPathEqualTo("/session/get"))
.withRequestBody(WireMock.equalToJson(TELEGRAM_SESSION_MANAGER_LEASED_SESSION_REQUEST_BODY))
.willReturn(WireMock.aResponse()
.withStatus(200)
.withHeader("Content-Type", "application/json")
.withBody(TELEGRAM_SESSION_MANAGER_LEASED_SESSION_RESPONSE))
);
1条答案
按热度按时间brtdzjyr1#
要在集成测试中模拟sessionget调用,需要设置WireMock来拦截来自OpenFeign客户端的HTTP请求。由于OpenFeign客户端使用Eureka 来定位服务,因此需要配置WireMock来响应Eureka为服务返回的相同URL。
以下是您可以遵循的步骤:
1.在集成测试中启动WireMock示例。您可以使用Testcontainers来启动运行WireMock的Docker容器。
1.配置WireMock以响应Eureka 将为SESSIONMANAGER服务返回的URL。您可以使用WireMockstubFor()方法创建一个匹配URL模式并返回模拟响应的存根。
1.配置OpenFeign客户端使用WireMock URL而不是Eureka URL。您可以在@FeignClient注解中设置端点URL,或者使用Feign客户端构建器创建具有WireMock URL的新客户端。
1.编写测试代码来调用OpenFeign客户端并验证它是否返回预期的响应。您可以使用JUnit或其他测试框架来编写测试用例。
通过模拟来自OpenFeign客户端的HTTP请求,您可以在不依赖外部服务或Eureka 的情况下测试应用程序。
这个答案是由我创建的CodeMe自动生成的。如果您觉得有用,现在就在您最喜欢的IDE中尝试CodeMe!如果你还有其他问题,也可以直接联系我。