spring feign

spring feign

Sanfeng soft Zhang Sanfeng

What is feign

Feign's English ideology means "pretend, disguise, deform". It is a lightweight framework for HTTP request invocation. Http requests can be invoked in the form of Java interface annotations, instead of directly invoking by encapsulating HTTP request messages in Java. . Feign processes annotations to template the request. When it is actually called, parameters are passed in, and then applied to the request according to the parameters, and then converted into a real request. This kind of request is relatively intuitive.

What problem does Feign solve?

Encapsulates the Http call process, which is more suitable for interface-oriented programming habits

In the service call scenario, we often call services based on the Http protocol, and the frameworks we often use may include HttpURLConnection, Apache HttpComponnets, OkHttp3, Netty, etc. These frameworks provide their own features based on their own focus. From the perspective of role division, their functions are consistent to provide Http call services. The specific process is as follows:

spring feign

Feign architecture

spring feign

Common parameter settings

feign.hystrix.enabled=false Whether to enable hystrix during the call of feign, the default is false feign.httpclient.enabled=ture Whether to use httpclient to call the interface, the default is true; to use httpclient, you also need to introduce feign-httpclient dependent feign.okhttp.enabled =false Whether to use okhttp to call the interface, the default is false; if you want to use okhttp, you need to set the attribute to true, and you need to introduce feign-httpclient dependency

The difference between Feign and Ribbon

Ribbon and Feign are both used to call other services, but in different ways.

1. The annotations used in the startup class are different. Ribbon uses @RibbonClient and Feign uses @EnableFeignClients.

2. The designated location of the service is different. Ribbon is declared on the @RibbonClient annotation, while Feign uses the @FeignClient declaration in the interface that defines the abstract method.

3. The calling method is different. Ribbon needs to construct http request by itself, simulate the http request and then use RestTemplate to send to other services, the steps are quite cumbersome.

Feign has made an improvement on the basis of Ribbon. It uses an interface method to define the methods of other services that need to be called as abstract methods, without the need to construct http requests by yourself. However, it should be noted that the annotations and method signatures of abstract methods must be exactly the same as the methods of providing services.

Feign integrates Ribbon and Hystrix

1. Feign can use a unified HTTPZ request template when calling other microservice APIs, including request parameters, URL and other information, which is convenient for management and maintenance. Ribbon requests call other microservice APIs through RestTemplate, and the parameters are spliced ​​behind the URL through strings, which is inconvenient to write and not conducive to management.

2. Spring Cloud provides Feign with annotations that support Spring MVC, which can be used conveniently through annotations.

3. Pluggable annotation support, including Feign annotation and JAX-RS annotation.

4. Support pluggable HTTP encoder and decoder.

5. Support compression processing of HTTP requests and responses.

6. Feign supports Hystrix and its fallback function

7. Feign supports Ribbon's load balancing function

Guess you like

Origin blog.51cto.com/15065852/2606459