Springcloud -- use and explanation of openfeign / feign

1. Introduction to openfeign

Feign is a declarative Web Service client. Its appearance makes it easy to develop Web Service clients. To use feign, you only need to create an interface and add corresponding annotations, such as FeignClient annotation. Feign has pluggable annotations, including feign annotations and JAX-RS annotations.
Feign also supports encoders and decoders. Spring Cloud Open Feign enhances feign, supports Spring MVC annotations, and can use HttpMessageConverters like Spring Web.
Feign is a declarative and templated HTTP client. Using feign in Spring Cloud, you can use HTTP requests to access remote services, just like calling local methods. Developers are completely unaware that they are calling remote methods, let alone accessing HTTP requests.

1.1 Feign

Feign is a lightweight RESTful HTTP service client in the Spring Cloud component. Feign has a built-in Ribbon for client load balancing and calling services in the service registry. Feign can be used in the following ways: use feign's annotation to define the interface and call the interface to call the service of the service registry.

  • Feign's dependency:

1.2 OpenFeign

OpenFeign is a Spring Cloud that supports spring MVC annotations based on Feign, such as @ RequestMapping. OpenFeign's @ FeignClient can parse the interface under the @ RequestMapping annotation of spring MVC and generate implementation classes through dynamic proxy.

  • OpenFeign's dependencies:

1.3 description

SpringCloud F and above, and SpringBoot 2.0 and above basically use OpenFeign. If the framework structure of OpenFeign is the version that appears after Feign stops working in 2019, it can also be said that most new projects use OpenFeign, and projects before 2018 use Feign.

2. Functions of openfeign

  1. Pluggable annotation support, including Feign annotation and JAX-RS annotation.
  2. Support pluggable HTTP encoder and decoder (Gson, Jackson, Sax, JAXB, JAX-RS, SOAP).
  3. Supports Hystrix and its Fallback.
  4. Support Ribbon load balancing.
  5. Supports compression of HTTP requests and responses.
  6. Flexible configuration: configuration based on name granularity
  7. Support multiple clients: JDK URLConnection, apache httpclient, okhttp, ribbon)
  8. Support log
  9. Error retry is not supported
  10. url support placeholder
  11. It can run independently without relying on the registry

3. Use of openfeign

Effect: Feign = RestTemplate+Ribbon+Hystrix

3.1 adding dependencies


3.2 start Feign

Start the class to add Feign to the annotation @ EnableFeignClients

@EnableDiscoveryClient // Turn on service discovery
@EnableFeignClients	// Open Feign
public class OpenfeignApplication {

	public static void main(String[] args) { 	
		SpringApplication.run(OpenfeignApplication.class, args);


3.3 configuration yml file

In application Configure in YML to register the project with Eureka:

  port: 8021
    context-path: /

    active: dev
    name: openfeign

#Register with Eureka Service Center
      # To register in the cluster, connect multiple EurekaServer addresses with commas; Register to a single instance (non cluster mode), then write one, ok
      defaultZone: http://localhost:8761/eureka

To be continued and updated continuously.

Keywords: Spring Cloud

Added by the-botman on Tue, 08 Mar 2022 02:39:36 +0200