SpringCloud - Feign remote call 2

CSDN Topic Challenge Phase 2
Participation Topic: Study Notes

Please add image description
Personal business card:

blogger: Alcoholics ᝰ.
Personal profile: Indulge in wine, and strive for a future with a drink.
This article is inspirational: Three people walk, there must be my teacher.

Please add image description
This project is based on the dark horse programmer of station B Java "SpringCloud Microservice Technology Stack", SpringCloud+RabbitMQ+Docker+Redis+search+distributed

[SpringCloud+RabbitMQ+Docker+Redis+search+distributed, system detailed springcloud microservice technology stack course|dark horse programmer Java microservice] Click to watch

1. Feign replaces RestTemplate

Click to view

2. Log configuration

1. Log level

NONE: no log
BASIC: request method, URL, response status code and execution time
HEADERS: request method, URL, response status code and execution time and request header and response header
FULL: record header, body and metadata of request and response, almost all information

2. Method 1: Configuration file method

  1. Take effect globally
feign:
  client:
    config:
      default:
        logger-level: Full
  1. local effect
feign:
  client:
    config:
      userservice: # 服务名名称
        logger-level: Full

3. Method 2: Java code method

  1. Create a configuration module config, in which the DefaultFeignConfiguration class is created
package cn.itcast.order.config;

import feign.Logger;
import org.springframework.context.annotation.Bean;

public class DefaultFeignConfiguration  {
    
    
    @Bean
    public Logger.Level feignLogLevel(){
    
    
        return Logger.Level.FULL; // 日志级别为
    }
}


  1. Global configuration

In the startup class, the heaven is annotated as follows

@EnableFeignClients(defaultConfiguration = DefaultFeignConfiguration.class) //全局生效
  1. local configuration

Add the following annotation to the controller class, where value is the service name.

@FeignClient(value = "userservicec" , configuration = DefaultFeignConfiguration.class) // 局部生效

3. Performance optimization

Method : use connection pool (here using Apache HttpClient)

  1. import dependencies
<!--HttpClient的依赖-->
<dependency>
    <groupId>io.github.openfeign</groupId>
    <artifactId>feign-httpclient</artifactId>
</dependency>
  1. Configure the connection pool (application.yml)
feign:
  httpclient:
    enabled: true
    max-connections: 200
    max-connections-per-route: 50

Recommendation: Use basic or none for log level

4. Best Practices

1. Inheritance

For those with high code coupling, define a unified parent interface and inherit this interface.

2. Extraction

1. Steps

Extract FeignCLient as a separate module

  1. Create a new module named feign-api.
  2. Introduce feign dependencies
<dependencies>
  <dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-openfeign</artifactId>
  </dependency>
</dependencies>
  1. Move the clients, config, pojo modules into the feign-api module.

image.png

  1. Import feign-api dependency in order-service
 <!--feign-api依赖-->
<dependency>
  <groupId>cn.itcast.demo</groupId>
  <artifactId>feign-api</artifactId>
  <version>1.0</version>
</dependency>
  1. Modify the import related to the above three modules in order-service to the package in feign-api
  2. restart test

2. Exception

The following exception occurs when restarting:
Field userClient in cn.itcast.order.service.OrderService required a bean of type 'cn.itcast.feign.clients.UserClient' that could not be found.

image.png

Reason:
FeignClients is not in the scope of the startup class @SpringBootApplication to scan the package
Solution:
Method 1: Specify the package where FeignClients is located

@EnableFeignClients(defaultConfiguration = DefaultFeignConfiguration.class, basePackages = "cn.itcast.feign.clients") //全局生效

Method 2: Specify FeignClients bytecode

@EnableFeignClients(defaultConfiguration = DefaultFeignConfiguration.class, clients = UserClient.class)

Guess you like

Origin blog.csdn.net/m0_65144570/article/details/127138958
Recommended