I What is Feign
Feign is a declarative pseudo Http client provided by Spring Cloud. It makes calling remote services as simple as calling local services. You only need to create an interface and add an annotation.
Feign integrates Ribbon by default, so using feign by default under Nacos achieves the effect of load balancing.
II Use of Feign
1. Add dependency
<!--fegin assembly--> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-openfeign</artifactId> </dependency>
2. Add Fegin annotation on the main class
@EnableFeignClients
@SpringBootApplication @EnableDiscoveryClient //Open Fegin @EnableFeignClients public class Application {}
3. Create a service and use Fegin to implement micro service invocation
//Declare the name of the calling provider (service product) @FeignClient("service-product") public interface ProductService { //Specifies which method of the provider to call //@FeignClient+@GetMapping is a complete request path http://service-product/product/{pid} @GetMapping(value = "/product/{pid}") Product findByPid(@PathVariable("pid") Integer pid); }
4. Injection use
// It is injected and used like a normal service @Autowired private ProductService productService; //Call commodity micro service through fegin Product product = productService.findByPid(pid);
III Service call Feign advanced
1. Configuration
feign: client: config: feignName: #Defines the name of the FeginClient connectTimeout: 5000 # Equivalent to request Options readTimeout: 5000 # Equivalent to request Options # Configure the log level of Feign, which is equivalent to the Logger in the code configuration mode loggerLevel: full # Feign's error decoder is equivalent to the error decoder in the code configuration mode errorDecoder: com.example.SimpleErrorDecoder # Configure retry, which is equivalent to Retryer in code configuration mode retryer: com.example.SimpleRetryer # Configuring the interceptor is equivalent to the RequestInterceptor in the code configuration mode requestInterceptors: - com.example.FooRequestInterceptor - com.example.BarRequestInterceptor decode404: false
- feignName: the name of the FeginClient (service name, for example: service product). Change the name of the invoked microservice to default to configure it as global
- connectTimeout: the timeout length for establishing the link
- readTimeout: read timeout duration
- readTimeout: read timeout duration
- Loggerlevel: log level of fegin
- errorDecoder: Feign's error decoder
- retryer: configure retry
- requestInterceptors: add request interceptors
- decode404: configuration does not handle 404 exceptions
2. Request compression
Spring Cloud Feign supports GZIP compression of requests and responses to reduce performance loss during communication. The compression function of request and response can be enabled through the following parameters:
feign: compression: request: enabled: true # Turn on request compression response: enabled: true # Turn on response compression
At the same time, we can also set the requested data type and the lower limit of the size to trigger compression:
feign: compression: request: enabled: true # Turn on request compression mime-types: text/html,application/xml,application/json # Set compressed data type min-request-size: 2048 # Sets the lower limit of the size that triggers compression
Note: the above data type and the lower limit of compression size are the default values.
3. Log level
In the development or running phase, you often want to see the log record of Feign request process. By default, Feign's log is not turned on.
To achieve the logging effect by using the attribute configuration method, just click application Add the following content to YML:
feign: client: config: shop-service-product: loggerLevel: FULL logging: level: cn.itcast.order.fegin.ProductFeginClient: debug
- logging. level. XX: debug: feign log will only respond to the log level of debug
- feign.client.config.shop-service-product.loggerLevel: there are four types of logs for configuring Feign
Log level:- NONE [best performance, suitable for production]: no log is recorded (default value)
- BASIC [applicable to production environment tracking problem]: only record the request method, URL, response status code and execution time
- HEADERS: record the header s of requests and responses based on the BASIC level.
- FULL [more applicable to development and test environment positioning]: record the header, body and metadata of requests and responses.