Make writing a habit together! This is the fourth day of my participation in the “Gold Digging Day New Plan · April More text Challenge”. Click here for more details.

A catalog of articles in the microservices series

  • Deep into microservices -SpringBoot automatic assembly principle
  • Deep microservices -SpringCloud invokes the component Feign
  • Deep microservices – the foundation for service registration and discovery of SpringCloud Eureka
  • Deep microservices – service registration and discovery of SpringCloud Eureka’s high availability and core principles

preface

This series takes you deep into the underlying principles of the various frameworks of the Microservices Spring architecture. The last article introduced SpringBoot autoloader. This section will take you through Feign, the invocation component of the SpringCloud architecture


What is Feign

  • Feign is a declarative Web services client. It makes it easier to write Web service clients
  • It has pluggable annotation support, including Feign annotations and JAX-RS annotations
  • Feign also supports pluggable encoders and decoders
  • Support for Spring MVC annotations and support for useHttpMessageConvertersAnnotations used by default in Spring Web
  • Spring Cloud integrates Eureka, Spring Cloud CircuitBreaker, and Spring Cloud LoadBalancer to provide an HTTP client for load balancing when using Feign

Integrate SpringCloud Feign

Add POM dependencies

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

Enable the Feign client

@SpringBootApplication
@EnableFeignClients
public class Application {

    public static void main(String[] args) { SpringApplication.run(Application.class, args); }}Copy the code

Create a Spring Cloud LoadBalancer client

@FeignClient("demo")
public interface DemoFeignClient {
    @RequestMapping(method = RequestMethod.GET, value = "/getDemo")
    DemoDTO getDemo(a);
}
Copy the code

RPC calls the client

@RestController
@RequestMapping("/remote")
public class RemoteService {

    @Autowired
    DemoFeignClient demoFeignClient;

    @RequestMapping(value = "/getRemoteDemo")
    public DemoDTO   getRemoteDemo(a) {
        DemoDTO  result = demoFeignClient.getDemo();
        returnresult; }}Copy the code

Feign core configuration

The timeout configuration

Configuration description:

ConnectTimeout: Indicates the timeout period for establishing a connection

ReadTimeout: The time from the time the connection is established to the time the response is returned

1) Configure for FEIGN

feign:
    client:
        config:
            feignName:
                connectTimeout: 5000
                readTimeout: 5000
Copy the code

2) General configuration

feign:
    client:
        config:
            default:
                connectTimeout: 5000
                readTimeout: 5000
Copy the code

Interceptor configuration

Feign’s interceptor function can be used to implement interface authentication and authentication

1) the Basic authentication

@Configuration
public class DemoFeignConfiguration {
    @Bean
    public BasicAuthRequestInterceptor basicAuthRequestInterceptor(a) {
        return new BasicAuthRequestInterceptor("user"."password"); }}Copy the code

2) The custom interceptor implementation needs to implement the interface RequestInterceptor

public interface RequestInterceptor {

  /**
   * Called for every request. Add data using methods on the supplied {@link RequestTemplate}.
   */
  void apply(RequestTemplate template);
}
Copy the code

2.1) Put TOKEN authentication information in the request header

public class UserInfoRequestInterceptor implements RequestInterceptor {

    public void apply(RequestTemplate template) {
        template.header("USER-INFO"."TOKEN"); }}Copy the code

2.2) Add the configuration to Feign

feign:
    client:
        config:
            feignName:
                connectTimeout: 5000
                readTimeout: 5000
                requestInterceptors:
                    - com.example.UserInfoRequestInterceptor
Copy the code

The log configuration

The Feign uses logs to implement the Logger function of Fegin. You can print HTTP call links for function debugging.

1) Logger. The Level of Level:

  • NONE, no logging (default)
  • BASIC, which records only the request method and URL along with the response status code and execution time.
  • HEADERS, which records basic information and request and response HEADERS
  • FULL, which records the headers, body, and metadata of the request and response

2) Configuration 2.1) YAML configuration

logging:
  level:
    com.example.feign.DemoFeign: debug
Copy the code

2.2) the Config configuration

@Configuration
public class FeignConfiguration {
    @Bean
    Logger.Level feignLoggerLevel(a) {
        returnLogger.Level.FULL; }}Copy the code

@feignClient Common attributes

  • Name: Specifies the name of FeignClient
  • Url: Specifies the address of the @feignClient call, which can be used for online debugging
  • Configuration: Feign configuration class
  • Fallback: Handler class that requests error callbacks
  • Path: Specifies the unified prefix of FeignClient