Feign is a declarative Web service client. This makes the writing of the Web service client more convenient to use feign to create an interface and annotate it. It has pluggable annotation support, including feign annotations and jax-rs annotations. feign also supports pluggable encoders and decoders. Spring Cloud adds support for spring MVC annotations and is used by default in the Spring Web HttpMessageConverters
. Spring Cloud integrates the Ribbon and Eureka to provide load-balanced HTTP clients when using feign.
How to join feign
to include feign in your project, use org.springframework.cloud
the initiator for group and artifact IDs spring-cloud-starter-feign
. for More information about using the current Spring Cloud Publishing list settings to build your system, see The Spring Cloud project page .
Example Spring Boot app
@Configuration @componentscan@enableautoconfiguration@enableeurekaclient@enablefeignclientspublic Class application {public static void main (string[] args) {Springapplication.run (application.class, args); }}
Storeclient.java
@FeignClient ("stores") public interface Storeclient {@RequestMapping (method = requestmethod.get, value = "/stores") List<store> getstores (); @RequestMapping (method = requestmethod.post, value = "/stores/{storeid}", consumes = "Application/json") Store Update (@ Pathvariable ("StoreId") Long StoreId, store store);
in the @FeignClient
comment, the string value (above "storage") is an arbitrary client name used to create the Ribbon load balancer ( For more information about ribbon support, see below )). You can also specify a URL by using the url
attribute (absolute or just the host name). the name of the Bean in the context of the application is the fully qualified name of the interface. to specify your own alias value, you can use @FeignClient
the value of the comment qualifier
.
The central concept of Spring Cloud's feign support is the specified client. Each pretending client is part of a composite component that works together to contact a remote server as needed, and that collection has a name that you use as a @FeignClient
comment for application developers. Spring Cloud uses the FeignClientsConfiguration
creation of a new collection for each named client, as needed ApplicationContext
. This contains (among other things) feign.Decoder
, feign.Encoder
and feign.Contract
.
Spring Cloud can @FeignClient
fully control the fake client by using the claim additional configuration ( FeignClientsConfiguration
). Example:
@FeignClient (name = "Stores", configuration = fooconfiguration.class) public interface Storeclient {/.}
In this case, the client consists of the FeignClientsConfiguration
components in and FooConfiguration
any components in it (the latter overrides the former).
name
and url
attributes support placeholders.
@FeignClient (name = "${feign.name}", url = "${feign.url}") public interface Storeclient {/.}
Spring Cloud Netflix defaults to feign ( BeanType
beanname: ClassName
) to provide the following beans:
Decoder
Feigndecoder: ResponseEntityDecoder
(which contains SpringDecoder
)
Encoder
Feignencoder:SpringEncoder
Logger
Feignlogger:Slf4jLogger
Contract
Feigncontract:SpringMvcContract
Feign.Builder
Feignbuilder:HystrixFeign.Builder
Client
Feignclient: If the Ribbon is enabled, LoadBalancerFeignClient
otherwise the default feign client will be used.
You can feign.okhttp.enabled
use the feign.httpclient.enabled
true
okhttpclient and apachehttpclient feign clients by putting or setting them and placing them on the classpath.
Spring Cloud Netflix By default No provide the following beans, but still look for these types of beans from the application context to create fake clients:
creating a type of bean and placing it in a @FeignClient
configuration (for example, above FooConfiguration
) allows you to overwrite each bean described. Example:
@Configurationpublic class Fooconfiguration {@Bean public contract feigncontract () {return new feign. Contract.default (); } @Bean Public Basicauthrequestinterceptor basicauthrequestinterceptor () {return new Basicauthrequestinterce Ptor ("User", "password"); }}
This is SpringMvcContract
replaced by feign.Contract.Default
, and will be RequestInterceptor
added to, RequestInterceptor
the collection.
You can @EnableFeignClients
specify the default configuration in a property in a defaultConfiguration
manner similar to the above. The difference is that this configuration will apply to all fake clients.
Source Source
Spring Cloud's feign client