当前位置:网站首页>[cloud native] record of feign custom configuration of microservices
[cloud native] record of feign custom configuration of microservices
2022-07-05 05:38:00 【Xiao Wei wants to learn from Zhu Lao】
Preface :
Recently, I am learning about micro Services , Read the relevant courses of dark horse , Will relate Feign The knowledge of has summed up some , I hope I can help you guys and deepen your impression
If there is anything to be improved in the article, please give me more advice
Xiao Wei first thanked everyone for their support
List of articles
🥤Feign Custom configuration for
Feign There are two ways to customize the configuration :java Code configuration and Attribute configuration , These two configurations are described below .
Here we start with Feign Log configuration is an example ,Feign No logs are printed by default , When an exception occurs in the remote call , Subsequent debugging will want to see the details of the entire request , You can configure Feign Log requests .
Feign Log level of
1.NONE( The default value is ): Don't log anything
2.BASIC: Only the request method is recorded 、URL、 Response status code and execution time
3.HEADERS: Record BASIC Level based , Recording requests and responses header
4.FULL: Recording requests and responses header、body And metadata
Of course, there are other things besides logs , The following is presented in the form of a table :
type | effect | explain |
---|---|---|
feign.Logger.Level | Modify log level | Contains four different levels :NONE、BASIC、HEADERS、FULL |
feign.codec.Decoder | The parser that responds to the result | http Analyze the result of remote call , For example, parsing json String is java object |
feign.codec.Encoder | Request parameter encoding | Encode the request parameters , Easy to pass http Request to send |
feign. Contract | Supported annotation formats | The default is SpringMVC Annotations |
feign. Retryer | Failure retry mechanism | Retry mechanism for request failure , The default is No , However, it will be used Ribbon Retry |
In general , The default value can meet our use , If you want to customize , Just create a custom @Bean Override default Bean that will do .
The configuration file
Modify based on the configuration file feign The log level can be for a single service :
feign:
client:
config:
userservice: # Configuration for a microservice
loggerLevel: FULL # The level of logging
It can also be for all services :
feign:
client:
config:
default: # Here we use default Global configuration , If you write the service name , It is the configuration for a micro service
loggerLevel: FULL # The level of logging
There are four levels of logs :
- NONE: No log information is recorded , This is the default .
- BASIC: Record only the requested method ,URL And the response status code and execution time
- HEADERS: stay BASIC On the basis of , Additionally, the request and response header information is recorded
- FULL: Record details of all requests and responses , Including header information 、 Request body 、 Metadata .
🧁Java Code configuration
It can also be based on Java Code to modify the log level , Foretell a class , Then declare a Logger.Level The object of :
public class DefaultFeignConfiguration {
@Bean
public Logger.Level feignLogLevel(){
return Logger.Level.BASIC; // Log level is BASIC
}
}
If you want to Global effect , Put it in the... Of the startup class @EnableFeignClients In this note :
@EnableFeignClients(defaultConfiguration = DefaultFeignConfiguration .class)
If it is Partial effect , Then put it in the corresponding @FeignClient In this note :
@FeignClient(value = "userservice", configuration = DefaultFeignConfiguration .class)
Feign Tuning of
Feign Performance tuning book ⾝ Very good performance ⾼, But you can still optimize .
feign The underlying client implementation :
URLConnection: Default implementation , Connection pooling is not supported
Apche HttpClient: Support connection pool
OKHttp: Support connection pool
So optimize feign The performance of
Use the connection pool instead of the default connection pool URLConnection
The level of logging : meanwhile ⽇ The level of ambition is different , The performance consumed is also different , Use as much as possible Basic Level or none
You can make ⽤Apche HttpClient or OKHttp Into the ⾏ Optimize
Feign Bottom layer initiation http request , Rely on other frameworks . Its underlying client implementation includes :
•URLConnection: Default implementation , Connection pooling is not supported
•Apache HttpClient : Support connection pool
•OKHttp: Support connection pool
So improve Feign The main means of performance is to use Connection pool Instead of default URLConnection.
Here we use Apache Of HttpClient To demonstrate .
First step Introduce dependencies
stay order-service Of pom Introduce in the file Apache Of HttpClient rely on :
<!--httpClient Dependence -->
<dependency>
<groupId>io.github.openfeign</groupId>
<artifactId>feign-httpclient</artifactId>
</dependency>
2) Configure connection pool
stay order-service Of application.yml Add configuration in :
feign:
client:
config:
default: # default Global configuration
loggerLevel: BASIC # The level of logging ,BASIC Is the basic request and response information
httpclient:
enabled: true # Turn on feign Yes HttpClient Support for
max-connections: 200 # Maximum number of connections
max-connections-per-route: 50 # Maximum number of connections per path
stay FeignClientFactoryBean Medium loadBalance Break in the method , and Debug Mode start order-service service , The bottom is Apache HttpClient.
1. Try to use the log level basic
2. Use HttpClient or OKHttp Instead of URLConnection
① introduce feign-httpClient rely on
② Profile on httpClient function , Set connection pool parameters
This is the end of the article , If you have any questions, please point out
Thank you again for your support
边栏推荐
- Haut OJ 1352: string of choice
- 利用HashMap实现简单缓存
- Analysis of backdoor vulnerability in remote code execution penetration test / / phpstudy of national game title of national secondary vocational network security B module
- Sword finger offer 09 Implementing queues with two stacks
- kubeadm系列-02-kubelet的配置和启动
- 每日一题-无重复字符的最长子串
- Little known skills of Task Manager
- Scope of inline symbol
- Remote upgrade afraid of cutting beard? Explain FOTA safety upgrade in detail
- sync. Interpretation of mutex source code
猜你喜欢
YOLOv5添加注意力机制
[jailhouse article] performance measurements for hypervisors on embedded ARM processors
Sword finger offer 53 - ii Missing numbers from 0 to n-1
lxml. etree. XMLSyntaxError: Opening and ending tag mismatch: meta line 6 and head, line 8, column 8
Implement a fixed capacity stack
Using HashMap to realize simple cache
Solution to the palindrome string (Luogu p5041 haoi2009)
全排列的代码 (递归写法)
剑指 Offer 53 - I. 在排序数组中查找数字 I
On-off and on-off of quality system construction
随机推荐
SAP-修改系统表数据的方法
【实战技能】如何做好技术培训?
读者写者模型
Graduation project of game mall
API related to TCP connection
Warning using room database: schema export directory is not provided to the annotation processor so we cannot export
Codeforces Round #716 (Div. 2) D. Cut and Stick
智慧工地“水电能耗在线监测系统”
剑指 Offer 53 - I. 在排序数组中查找数字 I
搭建完数据库和网站后.打开app测试时候显示服务器正在维护.
Introduction to convolutional neural network
每日一题-搜索二维矩阵ps二维数组的查找
Pointnet++ learning
Sword finger offer 53 - ii Missing numbers from 0 to n-1
YOLOv5添加注意力機制
shared_ Repeated release heap object of PTR hidden danger
【Jailhouse 文章】Performance measurements for hypervisors on embedded ARM processors
On-off and on-off of quality system construction
剑指 Offer 58 - II. 左旋转字符串
A misunderstanding about the console window