From 830ca1e662ec840214ec85f6ad2c219d5b7009c8 Mon Sep 17 00:00:00 2001
From: buildmaster
A central concept in Spring Cloud’s Feign support is that of the named client. Each feign client is part of an ensemble of components that work together to contact a remote server on demand, and the ensemble has a name that you give it as an application developer using the @FeignClient
annotation. Spring Cloud creates a new ensemble as an
-ApplicationContext
on demand for each named client using FeignClientsConfiguration
. This contains (amongst other things) an feign.Decoder
, a feign.Encoder
, and a feign.Contract
.
Spring Cloud lets you take full control of the feign client by declaring additional configuration (on top of the FeignClientsConfiguration
) using @FeignClient
. Example:
@FeignClient(name = "stores", configuration = FooConfiguration.class) +ApplicationContext
on demand for each named client usingFeignClientsConfiguration
. This contains (amongst other things) anfeign.Decoder
, afeign.Encoder
, and afeign.Contract
. +It is possible to override the name of that ensemble by using thecontextId
+attribute of the@FeignClient
annotation.Spring Cloud lets you take full control of the feign client by declaring additional configuration (on top of the
FeignClientsConfiguration
) using@FeignClient
. Example:@FeignClient(name = "stores", configuration = FooConfiguration.class) public interface StoreClient { //.. -}In this case the client is composed from the components already in
FeignClientsConfiguration
together with any inFooConfiguration
(where the latter will override the former).
Note
FooConfiguration
does not need to be annotated with@Configuration
. However, if it is, then take care to exclude it from any@ComponentScan
that would otherwise include this configuration as it will become the default source forfeign.Decoder
,feign.Encoder
,feign.Contract
, etc., when specified. This can be avoided by putting it in a separate, non-overlapping package from any@ComponentScan
or@SpringBootApplication
, or it can be explicitly excluded in@ComponentScan
.
Note The
serviceId
attribute is now deprecated in favor of thename
attribute.
Warning Previously, using the
url
attribute, did not require thename
attribute. Usingname
is now required.Placeholders are supported in the
name
andurl
attributes.@FeignClient(name = "${feign.name}", url = "${feign.url}") +}
In this case the client is composed from the components already in
FeignClientsConfiguration
together with any inFooConfiguration
(where the latter will override the former).
Note
FooConfiguration
does not need to be annotated with@Configuration
. However, if it is, then take care to exclude it from any@ComponentScan
that would otherwise include this configuration as it will become the default source forfeign.Decoder
,feign.Encoder
,feign.Contract
, etc., when specified. This can be avoided by putting it in a separate, non-overlapping package from any@ComponentScan
or@SpringBootApplication
, or it can be explicitly excluded in@ComponentScan
.
Note The
serviceId
attribute is now deprecated in favor of thename
attribute.
Note Using
contextId
attribute of the@FeignClient
annotation in addition to changing the name of +theApplicationContext
ensemble, it will override the alias of the client name +and it will be used as part of the name of the configuration bean created for that client.
Warning Previously, using the
url
attribute, did not require thename
attribute. Usingname
is now required.Placeholders are supported in the
name
andurl
attributes.@FeignClient(name = "${feign.name}", url = "${feign.url}") public interface StoreClient { //.. }Spring Cloud Netflix provides the following beans by default for feign (
BeanType
beanName:ClassName
):
Decoder
feignDecoder:ResponseEntityDecoder
(which wraps aSpringDecoder
)Encoder
feignEncoder:SpringEncoder
Logger
feignLogger:Slf4jLogger
Contract
feignContract:SpringMvcContract
Feign.Builder
feignBuilder:HystrixFeign.Builder
Client
feignClient: if Ribbon is enabled it is aLoadBalancerFeignClient
, otherwise the default feign client is used.The OkHttpClient and ApacheHttpClient feign clients can be used by setting
feign.okhttp.enabled
orfeign.httpclient.enabled
totrue
, respectively, and having them on the classpath. @@ -85,7 +89,16 @@ thread isolation strategy for Hystrix to `SEMAPHORE or disable Hystrix in default: execution: isolation: - strategy: SEMAPHORE
In some cases it might be necessary to customize your Feign Clients in a way that is not + strategy: SEMAPHORE
If we want to create multiple feign clients with the same name or url
+so that they would point to the same server but each with a different custom configuration then
+we have to use contextId
attribute of the @FeignClient
in order to avoid name
+collision of these configuration beans.
@FeignClient(contextId = "fooClient", name = "stores", configuration = FooConfiguration.class) +public interface FooClient { + //.. +}
@FeignClient(contextId = "barClient", name = "stores", configuration = BarConfiguration.class) +public interface BarClient { + //.. +}
In some cases it might be necessary to customize your Feign Clients in a way that is not possible using the methods above. In this case you can create Clients using the Feign Builder API. Below is an example which creates two Feign Clients with the same interface but configures each one with diff --git a/single/spring-cloud-openfeign.html b/single/spring-cloud-openfeign.html index 16208853..af6dde74 100644 --- a/single/spring-cloud-openfeign.html +++ b/single/spring-cloud-openfeign.html @@ -33,10 +33,14 @@ it will resolve the service in the Eureka service registry. If you don’t want to use Eureka, you can simply configure a list of servers in your external configuration (see above for example).
A central concept in Spring Cloud’s Feign support is that of the named client. Each feign client is part of an ensemble of components that work together to contact a remote server on demand, and the ensemble has a name that you give it as an application developer using the @FeignClient
annotation. Spring Cloud creates a new ensemble as an
-ApplicationContext
on demand for each named client using FeignClientsConfiguration
. This contains (amongst other things) an feign.Decoder
, a feign.Encoder
, and a feign.Contract
.
Spring Cloud lets you take full control of the feign client by declaring additional configuration (on top of the FeignClientsConfiguration
) using @FeignClient
. Example:
@FeignClient(name = "stores", configuration = FooConfiguration.class) +ApplicationContext
on demand for each named client usingFeignClientsConfiguration
. This contains (amongst other things) anfeign.Decoder
, afeign.Encoder
, and afeign.Contract
. +It is possible to override the name of that ensemble by using thecontextId
+attribute of the@FeignClient
annotation.Spring Cloud lets you take full control of the feign client by declaring additional configuration (on top of the
FeignClientsConfiguration
) using@FeignClient
. Example:@FeignClient(name = "stores", configuration = FooConfiguration.class) public interface StoreClient { //.. -}In this case the client is composed from the components already in
FeignClientsConfiguration
together with any inFooConfiguration
(where the latter will override the former).
Note
FooConfiguration
does not need to be annotated with@Configuration
. However, if it is, then take care to exclude it from any@ComponentScan
that would otherwise include this configuration as it will become the default source forfeign.Decoder
,feign.Encoder
,feign.Contract
, etc., when specified. This can be avoided by putting it in a separate, non-overlapping package from any@ComponentScan
or@SpringBootApplication
, or it can be explicitly excluded in@ComponentScan
.
Note The
serviceId
attribute is now deprecated in favor of thename
attribute.
Warning Previously, using the
url
attribute, did not require thename
attribute. Usingname
is now required.Placeholders are supported in the
name
andurl
attributes.@FeignClient(name = "${feign.name}", url = "${feign.url}") +}
In this case the client is composed from the components already in
FeignClientsConfiguration
together with any inFooConfiguration
(where the latter will override the former).
Note
FooConfiguration
does not need to be annotated with@Configuration
. However, if it is, then take care to exclude it from any@ComponentScan
that would otherwise include this configuration as it will become the default source forfeign.Decoder
,feign.Encoder
,feign.Contract
, etc., when specified. This can be avoided by putting it in a separate, non-overlapping package from any@ComponentScan
or@SpringBootApplication
, or it can be explicitly excluded in@ComponentScan
.
Note The
serviceId
attribute is now deprecated in favor of thename
attribute.
Note Using
contextId
attribute of the@FeignClient
annotation in addition to changing the name of +theApplicationContext
ensemble, it will override the alias of the client name +and it will be used as part of the name of the configuration bean created for that client.
Warning Previously, using the
url
attribute, did not require thename
attribute. Usingname
is now required.Placeholders are supported in the
name
andurl
attributes.@FeignClient(name = "${feign.name}", url = "${feign.url}") public interface StoreClient { //.. }Spring Cloud Netflix provides the following beans by default for feign (
BeanType
beanName:ClassName
):
Decoder
feignDecoder:ResponseEntityDecoder
(which wraps aSpringDecoder
)Encoder
feignEncoder:SpringEncoder
Logger
feignLogger:Slf4jLogger
Contract
feignContract:SpringMvcContract
Feign.Builder
feignBuilder:HystrixFeign.Builder
Client
feignClient: if Ribbon is enabled it is aLoadBalancerFeignClient
, otherwise the default feign client is used.The OkHttpClient and ApacheHttpClient feign clients can be used by setting
feign.okhttp.enabled
orfeign.httpclient.enabled
totrue
, respectively, and having them on the classpath. @@ -86,7 +90,16 @@ thread isolation strategy for Hystrix to `SEMAPHORE or disable Hystrix in default: execution: isolation: - strategy: SEMAPHORE
In some cases it might be necessary to customize your Feign Clients in a way that is not + strategy: SEMAPHORE
If we want to create multiple feign clients with the same name or url
+so that they would point to the same server but each with a different custom configuration then
+we have to use contextId
attribute of the @FeignClient
in order to avoid name
+collision of these configuration beans.
@FeignClient(contextId = "fooClient", name = "stores", configuration = FooConfiguration.class) +public interface FooClient { + //.. +}
@FeignClient(contextId = "barClient", name = "stores", configuration = BarConfiguration.class) +public interface BarClient { + //.. +}
In some cases it might be necessary to customize your Feign Clients in a way that is not
possible using the methods above. In this case you can create Clients using the
Feign Builder API. Below is an example
which creates two Feign Clients with the same interface but configures each one with
diff --git a/spring-cloud-openfeign.xml b/spring-cloud-openfeign.xml
index ad902676..8cb6e030 100644
--- a/spring-cloud-openfeign.xml
+++ b/spring-cloud-openfeign.xml
@@ -61,7 +61,9 @@ in your external configuration (see