From f37b513f21325233345f24b9f9260e8aeee6bf65 Mon Sep 17 00:00:00 2001 From: Mike Liu Date: Tue, 9 Aug 2016 21:20:50 -0700 Subject: [PATCH] Add @Qualifier customization for @FeignClient Changed test to use @Qualifier. Add documentation to reference the new attribute. --- .../src/main/asciidoc/spring-cloud-netflix.adoc | 7 ++++++- .../cloud/netflix/feign/FeignClient.java | 5 +++++ .../netflix/feign/FeignClientsRegistrar.java | 17 +++++++++++++++++ .../netflix/feign/beans/FeignClientTests.java | 2 ++ .../netflix/feign/beans/extra/TestClient.java | 2 +- 5 files changed, 31 insertions(+), 2 deletions(-) diff --git a/docs/src/main/asciidoc/spring-cloud-netflix.adoc b/docs/src/main/asciidoc/spring-cloud-netflix.adoc index 31b6d208..141f986a 100644 --- a/docs/src/main/asciidoc/spring-cloud-netflix.adoc +++ b/docs/src/main/asciidoc/spring-cloud-netflix.adoc @@ -790,7 +790,12 @@ In the `@FeignClient` annotation the String value ("stores" above) is an arbitrary client name, which is used to create a Ribbon load balancer (see <>). You can also specify a URL using the `url` attribute -(absolute value or just a hostname). The name of the bean in the application context is the fully qualified name of the interface. An alias is also created which is the 'name' attribute plus 'FeignClient'. For the example above, `@Qualifier("storesFeignClient")` could be used to reference the bean. +(absolute value or just a hostname). The name of the bean in the +application context is the fully qualified name of the interface. +An alias is also created which is the 'name' attribute plus 'FeignClient'. +For the example above, `@Qualifier("storesFeignClient")` could be used to +reference the bean. If you want to change the default `@Qualifier` value, +this can be done with the `qualifier` value in `@FeignClient`. The Ribbon client above will want to discover the physical addresses for the "stores" service. If your application is a Eureka client then diff --git a/spring-cloud-netflix-core/src/main/java/org/springframework/cloud/netflix/feign/FeignClient.java b/spring-cloud-netflix-core/src/main/java/org/springframework/cloud/netflix/feign/FeignClient.java index 2aaaeca1..8c2e361f 100644 --- a/spring-cloud-netflix-core/src/main/java/org/springframework/cloud/netflix/feign/FeignClient.java +++ b/spring-cloud-netflix-core/src/main/java/org/springframework/cloud/netflix/feign/FeignClient.java @@ -59,6 +59,11 @@ public @interface FeignClient { */ @AliasFor("value") String name() default ""; + + /** + * Sets the @Qualifier value for the feign client. + */ + String qualifier() default ""; /** * An absolute URL or resolvable hostname (the protocol is optional). 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 ff1a6728..9d31319b 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 @@ -184,6 +184,12 @@ class FeignClientsRegistrar implements ImportBeanDefinitionRegistrar, String alias = name + "FeignClient"; AbstractBeanDefinition beanDefinition = definition.getBeanDefinition(); beanDefinition.setPrimary(true); + + String qualifier = getQualifier(attributes); + if (StringUtils.hasText(qualifier)) { + alias = qualifier; + } + BeanDefinitionHolder holder = new BeanDefinitionHolder(beanDefinition, className, new String[] { alias }); BeanDefinitionReaderUtils.registerBeanDefinition(holder, registry); @@ -317,6 +323,17 @@ class FeignClientsRegistrar implements ImportBeanDefinitionRegistrar, } return basePackages; } + + private String getQualifier(Map client) { + if (client == null) { + return null; + } + String qualifier = (String) client.get("qualifier"); + if (StringUtils.hasText(qualifier)) { + return qualifier; + } + return null; + } private String getClientName(Map client) { if (client == null) { diff --git a/spring-cloud-netflix-core/src/test/java/org/springframework/cloud/netflix/feign/beans/FeignClientTests.java b/spring-cloud-netflix-core/src/test/java/org/springframework/cloud/netflix/feign/beans/FeignClientTests.java index fc5182e2..35406a42 100644 --- a/spring-cloud-netflix-core/src/test/java/org/springframework/cloud/netflix/feign/beans/FeignClientTests.java +++ b/spring-cloud-netflix-core/src/test/java/org/springframework/cloud/netflix/feign/beans/FeignClientTests.java @@ -26,6 +26,7 @@ import java.util.Map; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.beans.factory.annotation.Value; import org.springframework.boot.autoconfigure.EnableAutoConfiguration; import org.springframework.boot.builder.SpringApplicationBuilder; @@ -65,6 +66,7 @@ public class FeignClientTests { @Autowired private ApplicationContext context; + @Qualifier("uniquequalifier") @Autowired private org.springframework.cloud.netflix.feign.beans.extra.TestClient extraClient; diff --git a/spring-cloud-netflix-core/src/test/java/org/springframework/cloud/netflix/feign/beans/extra/TestClient.java b/spring-cloud-netflix-core/src/test/java/org/springframework/cloud/netflix/feign/beans/extra/TestClient.java index 0f239aa9..42676f68 100644 --- a/spring-cloud-netflix-core/src/test/java/org/springframework/cloud/netflix/feign/beans/extra/TestClient.java +++ b/spring-cloud-netflix-core/src/test/java/org/springframework/cloud/netflix/feign/beans/extra/TestClient.java @@ -21,7 +21,7 @@ import org.springframework.cloud.netflix.feign.beans.FeignClientTests.Hello; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; -@FeignClient(value = "otherapp") +@FeignClient(value = "otherapp", qualifier = "uniquequalifier") public interface TestClient { @RequestMapping(method = RequestMethod.GET, value = "/hello") Hello getHello();