Spring Cloud's feign client

Source: Internet
Author: User

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).

nameand 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:

    • DecoderFeigndecoder: ResponseEntityDecoder (which contains SpringDecoder )

    • EncoderFeignencoder:SpringEncoder

    • LoggerFeignlogger:Slf4jLogger

    • ContractFeigncontract:SpringMvcContract

    • Feign.BuilderFeignbuilder:HystrixFeign.Builder

    • ClientFeignclient: 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:

    • Logger.Level

    • Retryer

    • ErrorDecoder

    • Request.Options

    • Collection<RequestInterceptor>

    • SetterFactory

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.