Browse Source

Add @Qualifier customization for @FeignClient

Changed test to use @Qualifier.
Add documentation to reference the new attribute.
pull/6/head
Mike Liu 8 years ago committed by Spencer Gibb
parent
commit
f37b513f21
No known key found for this signature in database
GPG Key ID: 7788A47380690861
  1. 7
      docs/src/main/asciidoc/spring-cloud-netflix.adoc
  2. 5
      spring-cloud-netflix-core/src/main/java/org/springframework/cloud/netflix/feign/FeignClient.java
  3. 17
      spring-cloud-netflix-core/src/main/java/org/springframework/cloud/netflix/feign/FeignClientsRegistrar.java
  4. 2
      spring-cloud-netflix-core/src/test/java/org/springframework/cloud/netflix/feign/beans/FeignClientTests.java
  5. 2
      spring-cloud-netflix-core/src/test/java/org/springframework/cloud/netflix/feign/beans/extra/TestClient.java

7
docs/src/main/asciidoc/spring-cloud-netflix.adoc

@ -790,7 +790,12 @@ In the `@FeignClient` annotation the String value ("stores" above) is @@ -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 <<spring-cloud-ribbon,below for details of Ribbon
support>>). 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

5
spring-cloud-netflix-core/src/main/java/org/springframework/cloud/netflix/feign/FeignClient.java

@ -59,6 +59,11 @@ public @interface FeignClient { @@ -59,6 +59,11 @@ public @interface FeignClient {
*/
@AliasFor("value")
String name() default "";
/**
* Sets the <code>@Qualifier</code> value for the feign client.
*/
String qualifier() default "";
/**
* An absolute URL or resolvable hostname (the protocol is optional).

17
spring-cloud-netflix-core/src/main/java/org/springframework/cloud/netflix/feign/FeignClientsRegistrar.java

@ -184,6 +184,12 @@ class FeignClientsRegistrar implements ImportBeanDefinitionRegistrar, @@ -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, @@ -317,6 +323,17 @@ class FeignClientsRegistrar implements ImportBeanDefinitionRegistrar,
}
return basePackages;
}
private String getQualifier(Map<String, Object> client) {
if (client == null) {
return null;
}
String qualifier = (String) client.get("qualifier");
if (StringUtils.hasText(qualifier)) {
return qualifier;
}
return null;
}
private String getClientName(Map<String, Object> client) {
if (client == null) {

2
spring-cloud-netflix-core/src/test/java/org/springframework/cloud/netflix/feign/beans/FeignClientTests.java

@ -26,6 +26,7 @@ import java.util.Map; @@ -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 { @@ -65,6 +66,7 @@ public class FeignClientTests {
@Autowired
private ApplicationContext context;
@Qualifier("uniquequalifier")
@Autowired
private org.springframework.cloud.netflix.feign.beans.extra.TestClient extraClient;

2
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; @@ -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();

Loading…
Cancel
Save