From 8f5e3ad7c0ff9a057ef9cd6e2b8f76ba43d61fd8 Mon Sep 17 00:00:00 2001 From: Spencer Gibb Date: Wed, 15 Feb 2017 14:43:19 -0700 Subject: [PATCH 1/2] polish --- .../cloud/netflix/feign/FeignClientsRegistrar.java | 6 ++---- .../cloud/netflix/feign/FeignClientsRegistrarTests.java | 2 ++ 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/spring-cloud-netflix-core/src/main/java/org/springframework/cloud/netflix/feign/FeignClientsRegistrar.java b/spring-cloud-netflix-core/src/main/java/org/springframework/cloud/netflix/feign/FeignClientsRegistrar.java index 2ed76819..9eb0e315 100644 --- a/spring-cloud-netflix-core/src/main/java/org/springframework/cloud/netflix/feign/FeignClientsRegistrar.java +++ b/spring-cloud-netflix-core/src/main/java/org/springframework/cloud/netflix/feign/FeignClientsRegistrar.java @@ -237,10 +237,8 @@ class FeignClientsRegistrar implements ImportBeanDefinitionRegistrar, } private String resolve(String value) { - if (StringUtils.hasText(value) - && this.resourceLoader instanceof ConfigurableApplicationContext) { - return ((ConfigurableApplicationContext) this.resourceLoader).getEnvironment() - .resolvePlaceholders(value); + if (StringUtils.hasText(value)) { + return this.environment.resolvePlaceholders(value); } return value; } diff --git a/spring-cloud-netflix-core/src/test/java/org/springframework/cloud/netflix/feign/FeignClientsRegistrarTests.java b/spring-cloud-netflix-core/src/test/java/org/springframework/cloud/netflix/feign/FeignClientsRegistrarTests.java index fb349686..5387f601 100644 --- a/spring-cloud-netflix-core/src/test/java/org/springframework/cloud/netflix/feign/FeignClientsRegistrarTests.java +++ b/spring-cloud-netflix-core/src/test/java/org/springframework/cloud/netflix/feign/FeignClientsRegistrarTests.java @@ -20,6 +20,7 @@ package org.springframework.cloud.netflix.feign; import java.util.Collections; import org.junit.Test; +import org.springframework.mock.env.MockEnvironment; import static org.hamcrest.Matchers.is; import static org.junit.Assert.assertThat; @@ -70,6 +71,7 @@ public class FeignClientsRegistrarTests { private String testGetName(String name) { FeignClientsRegistrar registrar = new FeignClientsRegistrar(); + registrar.setEnvironment(new MockEnvironment()); return registrar.getName(Collections.singletonMap("name", name)); } } From e3341a19cddaf004d3451a969b8a4f277ccb0bac Mon Sep 17 00:00:00 2001 From: Spencer Gibb Date: Wed, 15 Feb 2017 17:46:12 -0700 Subject: [PATCH 2/2] 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)); + } + + } +}