Unverified Commit f37b513f by Mike Liu Committed by Spencer Gibb

Add @Qualifier customization for @FeignClient

Changed test to use @Qualifier. Add documentation to reference the new attribute.
parent 56c7466e
...@@ -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 an arbitrary client name, which is used to create a Ribbon load
balancer (see <<spring-cloud-ribbon,below for details of Ribbon balancer (see <<spring-cloud-ribbon,below for details of Ribbon
support>>). You can also specify a URL using the `url` attribute 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 The Ribbon client above will want to discover the physical addresses
for the "stores" service. If your application is a Eureka client then for the "stores" service. If your application is a Eureka client then
......
...@@ -61,6 +61,11 @@ public @interface FeignClient { ...@@ -61,6 +61,11 @@ public @interface FeignClient {
String name() default ""; 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). * An absolute URL or resolvable hostname (the protocol is optional).
*/ */
String url() default ""; String url() default "";
......
...@@ -184,6 +184,12 @@ class FeignClientsRegistrar implements ImportBeanDefinitionRegistrar, ...@@ -184,6 +184,12 @@ class FeignClientsRegistrar implements ImportBeanDefinitionRegistrar,
String alias = name + "FeignClient"; String alias = name + "FeignClient";
AbstractBeanDefinition beanDefinition = definition.getBeanDefinition(); AbstractBeanDefinition beanDefinition = definition.getBeanDefinition();
beanDefinition.setPrimary(true); beanDefinition.setPrimary(true);
String qualifier = getQualifier(attributes);
if (StringUtils.hasText(qualifier)) {
alias = qualifier;
}
BeanDefinitionHolder holder = new BeanDefinitionHolder(beanDefinition, className, BeanDefinitionHolder holder = new BeanDefinitionHolder(beanDefinition, className,
new String[] { alias }); new String[] { alias });
BeanDefinitionReaderUtils.registerBeanDefinition(holder, registry); BeanDefinitionReaderUtils.registerBeanDefinition(holder, registry);
...@@ -318,6 +324,17 @@ class FeignClientsRegistrar implements ImportBeanDefinitionRegistrar, ...@@ -318,6 +324,17 @@ class FeignClientsRegistrar implements ImportBeanDefinitionRegistrar,
return basePackages; 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) { private String getClientName(Map<String, Object> client) {
if (client == null) { if (client == null) {
return null; return null;
......
...@@ -26,6 +26,7 @@ import java.util.Map; ...@@ -26,6 +26,7 @@ import java.util.Map;
import org.junit.Test; import org.junit.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value; import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration; import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.builder.SpringApplicationBuilder; import org.springframework.boot.builder.SpringApplicationBuilder;
...@@ -65,6 +66,7 @@ public class FeignClientTests { ...@@ -65,6 +66,7 @@ public class FeignClientTests {
@Autowired @Autowired
private ApplicationContext context; private ApplicationContext context;
@Qualifier("uniquequalifier")
@Autowired @Autowired
private org.springframework.cloud.netflix.feign.beans.extra.TestClient extraClient; private org.springframework.cloud.netflix.feign.beans.extra.TestClient extraClient;
......
...@@ -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.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RequestMethod;
@FeignClient(value = "otherapp") @FeignClient(value = "otherapp", qualifier = "uniquequalifier")
public interface TestClient { public interface TestClient {
@RequestMapping(method = RequestMethod.GET, value = "/hello") @RequestMapping(method = RequestMethod.GET, value = "/hello")
Hello getHello(); Hello getHello();
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment