This is the 19th day of my participation in the Genwen Challenge

When we think of Feign, the functionality we have in mind is the invocation between microservices, which is similar to the Http invocation we used with the RestTemplate

What does Feign have?

Feign, OpenFeIGN and Spring-Cloud-OpenFeign are all FEIGN

Distinction and Connection

Feign and openfeign

The last version (Dependency below) is dated July 2016.

compile group: ‘com.netflix.feign’, name: ‘feign- Core ‘, version:’8.18.0’ // OLD “Open feign” is designed for the new project. It’s the same project, But was moved to a different git repo and got a new group-id. Its versions start at 9.0.0.

Feign was no longer maintained by Netflix, feign was maintained by the community, Feign was renamed OpenFeign, the last version was 8.18.0, and the project migrated to a new repository. Versions later than 9.0.0 only use io.github. Openfeign. This dependency is recommended

Feign is a lightweight RESTful HTTP service client in the Spring Cloud component. Feign has the Ribbon built in, which is used to load balance clients and invoke services in the service registry. OpenFeign is a Spring Cloud annotation that supports SpringMVC on top of Feign, such as @requesMapping, etc.

What is the relationship between Spring-Cloud-OpenFeign and OpenFeign?

Spring-cloud-openfeign is a component that is packaged based on OpenFeign and integrates annotations of SpringMVC to facilitate SpringBoot project development.

Why Feign?

Feign applies to situations where an interface is called in multiple places, and wraps client classes for each microservice to wrap calls to these dependent services. Feign further encapsulates this by helping us define and implement the definition of the dependent service interface. Feign implements interface binding for service providers by creating an interface and configuring it with annotations. Feign integrates with the Ribbon, so using Feign simplifies the development of automatic encapsulation of service invocation clients when using the Spring Cloud Ribbon

The use of openfeign

Create a Feign module

Importing dependent dependencies

 <dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-OpenFopenfeign</artifactId>
</dependency>
Copy the code

Add the @feignClient annotation to the business layer interface

Such as:

@Component
@FeignClient(value = "CLOUD-User-SERVICE")
public interface UserFeignService
{
    @GetMapping(value = "/user/get/{id}")
    public CommonResult<User> getUserById(@PathVariable("id") Long id);

}
Copy the code

Common attributes of the @feignClient tag are as follows:

  • Name: Specifies the name of FeignClient. If the project uses the Ribbon, the name property will be used as the name of the microservice for service discovery
  • Url: URL is used for debugging. You can manually specify the address of the @FeignClient call
  • Decode404: When an HTTP 404 error occurs, decoder will be called if the field is true, otherwise FeignException will be thrown
  • Configuration: Feign configuration class, you can customize Feign Encoder, Decoder, LogLevel, Contract
  • Fallback: Defines a fault-tolerant processing class. When the remote interface fails to be called or times out, the fault-tolerant logic of the corresponding interface will be called. The class specified by Fallback must implement the interface tagged with @FeignClient
  • FallbackFactory: Factory class used to generate examples of fallback classes. With this property we can implement fault-tolerant logic common to each interface and reduce repetitive code
  • Path: defines the unified prefix of the current FeignClient

OpenFeign log enhancement

Log Printing function

Feign provides log printing, which allows you to configure the severity level to see the details of Http requests in the Feign interface.

The level of logging

  • NONE: By default, no logs are displayed.
  • BASIC: only request method, URL, response status code and execution time are recorded;
  • HEADERS: Request and response HEADERS in addition to the information defined in BASIC;
  • FULL: In addition to the information defined in HEADERS, there is the body and metadata of the request and response.

Today, I took a look at what OpenFeign does and how to use it