CSDN Topic Challenge Phase 2
Participation Topic: Study Notes
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.
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
Table of contents
1. Feign replaces RestTemplate
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
- Take effect globally
feign:
client:
config:
default:
logger-level: Full
- local effect
feign:
client:
config:
userservice: # 服务名名称
logger-level: Full
3. Method 2: Java code method
- 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; // 日志级别为
}
}
- Global configuration
In the startup class, the heaven is annotated as follows
@EnableFeignClients(defaultConfiguration = DefaultFeignConfiguration.class) //全局生效
- 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)
- import dependencies
<!--HttpClient的依赖-->
<dependency>
<groupId>io.github.openfeign</groupId>
<artifactId>feign-httpclient</artifactId>
</dependency>
- 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
- Create a new module named feign-api.
- Introduce feign dependencies
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
</dependencies>
- Move the clients, config, pojo modules into the feign-api module.
- Import feign-api dependency in order-service
<!--feign-api依赖-->
<dependency>
<groupId>cn.itcast.demo</groupId>
<artifactId>feign-api</artifactId>
<version>1.0</version>
</dependency>
- Modify the import related to the above three modules in order-service to the package in feign-api
- 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.
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)