如何解决使用 hystrix 作为断路器和 Ribbon 作为负载平衡器的 openfeign 集成测试问题
我使用的是 spring boot 2.4.2 和 spring cloud 2020.0.1。我正在使用带有 hystrix 的 openfeign 作为断路器。我正在使用 Eureka 进行服务发现。该功能按预期工作,但我无法使测试正常工作。它抛出一个异常
[503] during [GET] to [http://simple-helloworld-api/api/v1/greet/Test?error=false] [HelloWorldFeignClient#greeting(String,Boolean)]: [Load balancer does not contain an instance for the service simple-helloworld-api]
build.gradle 中的依赖
dependencies {
implementation 'org.springframework.boot:spring-boot-starter-actuator'
implementation 'org.springframework.boot:spring-boot-starter-web'
implementation 'org.springframework.cloud:spring-cloud-starter-netflix-eureka-client'
implementation 'org.springframework.cloud:spring-cloud-starter-openfeign'
implementation group: 'org.springframework.cloud',name: 'spring-cloud-starter-netflix-hystrix',version: '2.2.7.RELEASE'
implementation group: 'io.github.openfeign',name: 'feign-hystrix',version: '11.0'
implementation group: 'io.github.openfeign',name: 'feign-httpclient',version: '11.0'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
testImplementation "com.github.tomakehurst:wiremock:2.26.3"
testImplementation group: 'io.github.openfeign',name: 'feign-ribbon',version: '11.0'
}
假客户端
@FeignClient(name = "${feign.name.helloWorldApi}",configuration = HelloWorldFeignClientConfiguration.class,fallbackFactory = HelloWorldFeignClientFallbackFactory.class)
public interface HelloWorldFeignClient {
@GetMapping("${helloWorldApi.baseUrl}" + "greet/{name}")
String greeting(@PathVariable("name") String name,@RequestParam(name = "error",required = false) Boolean error);
}
HelloWorldFeignClientFallbackFactory 是从 org.springframework.cloud.openfeign.FallbackFactory
而不是从 feign.hystrix
导入的。
测试类
@SpringBootTest(classes = {HelloWorldFeignClientTest.FeignConfig.class},properties = {"eureka.client.enabled=false","feign.circuitbreaker.enabled=true"},webEnvironment = WebEnvironment.RANDOM_PORT)
class LoadBalancedHelloWorldFeignClientTest {
private static WireMockServer wireMockServer;
@Autowired
private HelloWorldFeignClient helloWorldFeignClient;
@BeforeAll
static void setUp() {
wireMockServer = new WireMockServer(WireMockConfiguration.wireMockConfig().dynamicPort());
wireMockServer.start();
}
@AfterAll
static void tearDown() {
wireMockServer.stop();
wireMockServer = null;
}
@Test
void testHelloWorldFeign_Success_ReturnGreetingMessage() {
//Arrange
wireMockServer.stubFor(get(urlPathEqualTo("/api/v1/greet/Test"))
.willReturn(aResponse()
.withStatus(HttpStatus.OK.value())
.withHeader("Content-Type",MediaType.TEXT_PLAIN_VALUE)
.withBody("Hello!! Test")));
//Act
String message = helloWorldFeignClient.greeting("Test",false);
//Assert
assertThat(message).isNotNull();
}
@Test
void testHelloWorldFeign_Fail_ReturnNull() {
wireMockServer.stubFor(get(urlPathEqualTo("/api/v1/greet/.*"))
.willReturn(aResponse().withStatus(501)));
String message = helloWorldFeignClient.greeting("Test",true);
//Hystrix fallback implementation will return null
assertThat(message).isNull();
}
@EnableFeignClients(clients = HelloWorldFeignClient.class)
@Configuration
@EnableAutoConfiguration
@RibbonClient(name = "simple-helloworld-api",configuration = LoadBalancedHelloWorldFeignClientTest.RibbonConfig.class)
static class FeignConfig {
@Bean
public HelloWorldFeignClientFallbackFactory helloWorldFeignClientFallbackFactory() {
return new HelloWorldFeignClientFallbackFactory();
}
}
@Configuration
static class RibbonConfig {
@Bean
public ServerList<Server> serverList() {
return new StaticServerList<>(new Server("localhost",wireMockServer.port()));
}
}
}
有人可以帮我吗?我已将示例项目推送到 https://github.com/sbhambani/simple-helloworld-api-client。
解决方法
最后,我已经能够通过测试用例解决这个问题。需要在The main bundle lets you access the resources in the same directory as the currently running executable. For a running app,the main bundle offers access to the app’s bundle directory. For code running in a framework,the main bundle offers access to the framework’s bundle directory.
中设置实例信息。
设置实例信息
SimpleDiscoveryProperties
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。