From e3341a19cddaf004d3451a969b8a4f277ccb0bac Mon Sep 17 00:00:00 2001 From: Spencer Gibb Date: Wed, 15 Feb 2017 17:46:12 -0700 Subject: [PATCH] Add @FeignClient(primary) Adds the ability to turn off Spring Cloud Netflix making the feign instances as primary. fixes gh-1016 --- .../main/asciidoc/spring-cloud-netflix.adoc | 12 ++ .../cloud/netflix/feign/FeignClient.java | 5 + .../netflix/feign/FeignClientsRegistrar.java | 5 +- .../valid/FeignClientNotPrimaryTests.java | 165 ++++++++++++++++++ 4 files changed, 186 insertions(+), 1 deletion(-) create mode 100644 spring-cloud-netflix-core/src/test/java/org/springframework/cloud/netflix/feign/valid/FeignClientNotPrimaryTests.java diff --git a/docs/src/main/asciidoc/spring-cloud-netflix.adoc b/docs/src/main/asciidoc/spring-cloud-netflix.adoc index 400c3bec..3283faef 100644 --- a/docs/src/main/asciidoc/spring-cloud-netflix.adoc +++ b/docs/src/main/asciidoc/spring-cloud-netflix.adoc @@ -1127,6 +1127,18 @@ static class HystrixClientFallbackFactory implements FallbackFactory testClients; + + @FeignClient(name = "localapp", primary = false) + protected interface TestClient { + @RequestMapping(method = RequestMethod.GET, path = "/hello") + Hello getHello(); + + } + + @Configuration + @EnableAutoConfiguration + @RestController + @EnableFeignClients(clients = { TestClient.class} , + defaultConfiguration = TestDefaultFeignConfig.class) + @RibbonClient(name = "localapp", configuration = LocalRibbonClientConfiguration.class) + protected static class Application { + + @Bean + @Primary + public PrimaryTestClient primaryTestClient() { + return new PrimaryTestClient(); + } + + @RequestMapping(method = RequestMethod.GET, path = "/hello") + public Hello getHello() { + return new Hello(HELLO_WORLD_1); + } + + public static void main(String[] args) { + new SpringApplicationBuilder(Application.class) + .properties("spring.application.name=feignclienttest", + "management.contextPath=/admin") + .run(args); + } + } + + @Test + public void testClientType() { + assertThat(this.testClient).as("testClient was of wrong type").isInstanceOf(PrimaryTestClient.class); + } + + @Test + public void testClientCount() { + assertThat(this.testClients).as("testClients was wrong").hasSize(2); + } + + @Test + public void testSimpleType() { + Hello hello = this.testClient.getHello(); + assertNull("hello was null", hello); + } + + protected static class PrimaryTestClient implements TestClient { + @Override + public Hello getHello() { + return null; + } + } + + @Data + @AllArgsConstructor + @NoArgsConstructor + public static class Hello { + private String message; + } + + @Configuration + public static class TestDefaultFeignConfig { + @Bean + Logger.Level feignLoggerLevel() { + return Logger.Level.FULL; + } + } + + // Load balancer with fixed server list for "local" pointing to localhost + @Configuration + public static class LocalRibbonClientConfiguration { + + @Value("${local.server.port}") + private int port = 0; + + @Bean + public ServerList ribbonServerList() { + return new StaticServerList<>(new Server("localhost", this.port)); + } + + } +}