【问题标题】:Testing Hystrix fallback through Feign API: com.netflix.client.ClientException: Load balancer does not have available server for client通过 Feign API 测试 Hystrix 回退:com.netflix.client.ClientException:负载均衡器没有可用于客户端的服务器
【发布时间】:2018-07-26 16:08:36
【问题描述】:

在测试我的 Feign API 的 Hystrix 回退行为时,我收到一个错误,但我希望它会成功。

Feign接口:

这是外部服务的 api。

@FeignClient(name = "book", fallback = BookAPI.BookAPIFallback.class)
public interface BookAPI {

    @RequestMapping("/")
    Map<String, String> getBook();

    @Component
    class BookAPIFallback implements BookAPI {

        @Override
        @RequestMapping("/")
        public Map<String, String> getBook() {
            Map<String, String> fallbackmap = new HashMap<>();
            fallbackmap.put("book", "fallback book");
            return fallbackmap;
        }
    }

}

测试类

这个测试只是为了验证回退行为:

@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = NONE)
public class BookServiceClientTest {

    @MockBean
    RestTemplate restTemplate;// <---- @LoadBalanced bean

    @Autowired
    private BookServiceClient bookServiceClient;

    @Before
    public void setup() {
        when(restTemplate.getForObject(anyString(), any()))
                .thenThrow(new RuntimeException("created a mock failure"));
    }

    @Test
    public void fallbackTest() {
        assertThat(bookServiceClient.getBook())
                .isEqualTo(new BookAPI.BookAPIFallback().getBook().get("book")); // <--- I thought this should work
    }
}

配置文件

application.yml

这些文件显示了可能相关的配置:

feign:
  hystrix:
    enabled: true

test/application.yml

eureka:
  client:
    enabled: false

问题

运行应用程序时一切正常。
但是在运行此测试时,我收到以下错误。
当然,这是一个测试,所以我还是试图绕过查找。

java.lang.RuntimeException: com.netflix.client.ClientException: Load balancer does not have available server for client: book

at org.springframework.cloud.netflix.feign.ribbon.LoadBalancerFeignClient.execute(LoadBalancerFeignClient.java:71)
at feign.SynchronousMethodHandler.executeAndDecode(SynchronousMethodHandler.java:97)

我错过了什么?

附录

应用类

@SpringBootApplication
@EnableCircuitBreaker
@EnableDiscoveryClient
@EnableFeignClients
public class LibraryApplication {

    public static void main(String[] args) {
        SpringApplication.run(LibraryApplication.class, args);
    }

}

图书馆控制器

@Controller
public class LibraryController {

    private final BookServiceClient bookService;

    public LibraryController(BookServiceClient bookServiceClient) {
        this.bookService = bookServiceClient;
    }

    @GetMapping("/")
    String getLibrary(Model model) {
        model.addAttribute("msg", "Welcome to the Library");
        model.addAttribute("book", bookService.getBook());
        return "library";
    }

}

没有其他类。

【问题讨论】:

  • 你能添加你的主应用程序类吗?
  • @DarrenForsythe 添加了
  • 我无法使用您的代码重新创建骨架项目的问题。如果可能的话,可以看到所有的源代码。这可能是加载其他 bean 的问题,这将在 SpringBootTest 中发生。那是完整的堆栈跟踪吗?如果您想做集成测试,还有其他可用的切片测试等更合适,并且将任何组件扫描分解为 Configurations 并定位需要加载的 bean 可以帮助解决这些问题。
  • @DarrenForsythe 添加了唯一的其他类:LibraryController。 pom.xml 是从 1.5.9.RELEASE Spring Boot initializr 构建的,带有 web、hystrix、eureka、feign starters。

标签: spring-boot netflix-eureka hystrix spring-cloud-feign netflix-ribbon


【解决方案1】:

所以!我能够重新创建问题,感谢添加更多代码,我不得不稍微尝试一下,因为我不确定BookClientService 是什么样子,而且实现 BookAPI 是没有意义的内部呼叫,例如在您的应用程序中,而不是使用 Feign 进行外部 API 调用。

不管怎样,

我推送了您在此处提供的版本。

https://github.com/Flaw101/feign-testing

当我将位于 src/test/resources 文件夹中的第二个 application.yml 重命名为 application-test.yml 时,问题得到解决,这将合并属性。

问题是由于第二个属性源,即测试源,覆盖了初始的application.yml禁用 hystrix,因为 Hystrix 被禁用,没有回退可去,它抛出导致回退的根本原因是缺少可调用Book API 的服务器。将其重命名为 application-test 将始终加载到 spring 测试上下文中。您可以使用内联属性或配置文件来解决它。

我在测试中添加了另一个禁用 feign /w hystrix 的测试,它重新创建了您收到的错误。

【讨论】:

  • 成功了!谢谢:)
  • 我一直希望测试属性与 application.yml 合并,并且不想指定测试配置文件,所以我将 application-test.yml 重命名为 application-default.yml
猜你喜欢
  • 2019-03-23
  • 1970-01-01
  • 2021-07-26
  • 2017-05-15
  • 2017-06-05
  • 2018-06-07
  • 2021-04-20
  • 2019-09-18
  • 2019-02-09
相关资源
最近更新 更多