austin Rukawa Kaede is uploading…Reupload Cancel
August 21, 2022 15:13 Read 4924
Frontier
Feign
was written by Netflix. It is a lightweight RESTful HTTP service client in SpringCloud components and the first generation load balancing client in SpringCloud.OpenFeign
is a declarative service call and load balancing component officially launched by Spring. It appears to replace Feign (Netflix Feign), which has entered a maintenance state. Services play an overarching role.
To understand the principle of OpenFeign
remote invocation, you can refer to the previous article: Spring Cloud OpenFeign – Remote Invocation
This article is mainly to share some tips for optimizing several aspects of OpenFeign
, which are mainly divided into the following points:
- request communication connection optimization
- timeout optimization
- load balancing
- data compression
- Log level optimization
1. Request communication connection optimization
The underlying communication components of OpenFeign
use the JDK’s built-in URLConnection
object to make HTTP
requests by default. Because the connection pool is not used, the performance is not very good. We can manually replace the communication components of OpenFeign
with dedicated communication components like Apache HttpClient
or OKHttp
, these dedicated communication components The built-in connection pool can better reuse and manage HTTP connection objects, and can also greatly improve the efficiency of HTTP requests. Next, I will take Apache HttpClient
as an example to demonstrate the replacement of dedicated communication components.
1.1 Add pom dependency
<!-- Add openfeign framework dependencies --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-openfeign</artifactId> </dependency> <!-- Add httpclient framework dependency --> <dependency> <groupId>io.github.openfeign</groupId> <artifactId>feign-httpclient</artifactId> </dependency> Copy Code
1.2 Open Apache HttpClient to use
Start Apache HttpClient
, and add the following configuration in the project configuration file application.yml
:
spring: cloud: Feign: client: httpclient: # By default, Feign uses the URLConnection that comes with JDK for Http requests, and manually replaces it with the communication component dedicated to Apache HttpClient enabled: true Copy Code
Verify whether the Apache HttpClient
configuration is effective, through debug debugging, set a breakpoint on the feign.SynchronousMethodHandler#executeAndDecode()
method, and initiate a remote call request without configuration Before replacement, it looks like this:
After the configuration is replaced, you can see that the default client has been replaced with Apache HttpClient:
2. Ribbon timeout optimization
Both OpenFeign
and Feign
have built-in Ribbon
load balancing components at the bottom layer, so there is no need to import after importing
depends. OpenFeign
dependencies >RibbonOpenFeign
therefore uses Ribbon
‘s request connection timeout and request processing timeout as its timeout, while Ribbon
default request connection timeout and request processing timeout are both 1s, as shown in the following source code:
So when we use OpenFeign to remotely call the microservice interface and the response time exceeds 1s, the following error will be reported:
2.1 Set OpenFeign call timeout
Add configuration in the project configuration file application.yml:
# remote call optimization Feign: client: httpclient: # By default, Feign uses the URLConnection that comes with JDK for Http requests, and manually replaces it with the communication component dedicated to Apache HttpClient enabled: true config: default: # Set the global timeout time connectTimeout: 3000 # Timeout time for request connection readTimeout: 5000 # Timeout for request processing Copy Code
2.2 Set Ribbon timeout
ribbon: ConnectionTimeout: 3000 # The timeout for requesting a connection ReadTimeout: 5000 # Timeout for request processing Copy Code
Use debug to verify whether the timeout period set below is valid. It is obvious that the configuration has taken effect. It mainly depends on the org.springframework.cloud.openfeign.FeignClientFactoryBean#configureUsingProperties()
method. The source code is shown in the following figure:
Call again and find that the request timeout exception will no longer be reported!
?♂?It is recommended to use this method to set the timeout period of OpenFeign, because the semantics of this configuration are clearer.
3. Data compression
OpenFeign
does not enable data compression by default, but we can manually enable its Gzip
compression function, which can greatly improve bandwidth utilization and accelerate data transmission speed , add the following configuration in the project configuration file application.yml
:
feign: compression: request: enabled: true # Turn on the compression function of the request data mime-types: text/xml, application/xml, application/json # compression type min-request-size: 1024 # The minimum compression value standard, when the data is greater than 1024, it will be compressed response: enabled: true # Turn on the response data compression function Copy Code
Tip Reminder: If the CPU resources of the service consumer are relatively tight, it is not recommended to enable the data compression function, because data compression and decompression both consume CPU resources, which will instead Adding an extra burden to the CPU will also lead to a decrease in system performance.
4. Load balancing optimization
The bottom layer of OpenFeign
uses Ribbon
for load balancing. Looking at the source code, we can see that its default load balancing strategy is polling strategy. The source code location: com. netflix.loadbalancer.BaseLoadBalancer
, as shown below:
However, in addition to the polling strategy, we have other 6 built-in load balancing strategies to choose from, and these load balancing strategies are as follows:
Load Balancing Strategy | Description |
---|---|
Weight Strategy< /strong> | WeightedResponseTimeRule , assign a weight according to the response time of each service provider, the longer the response time, the smaller the weight, and the lower the possibility of being selected . Its implementation principle is to start using the polling strategy and start a timer, collect the average response time of all service providers every once in a while, and then attach a weight to each service provider, the higher the weight is selected The probability is also greater. |
Minimum connection strategy | BestAvailableRule , also called minimum concurrent strategy, it is Traverse the list of service providers and select the service instance with the smallest number of connections. If there are the same minimum number of connections, the polling strategy will be called for selection. |
Zone-sensitive policy | ZoneAvoidanceRule , according to the performance and The availability of the service is used to select the service instance. In an environment without regions, this strategy is similar to the round robin strategy. |
Available sensitivity policy | AvailabilityFilteringRule , first filter out unhealthy service instances, Then select a service instance with a smaller number of connections. |
Random Strategy | RandomRule , randomly select a service from the list of service providers instance. |
Retry strategy | RetryRule , according to the polling strategy to obtain services, if obtained If the service instance is null or has expired, it will continue to retry to obtain the service within the specified time, and return null if the service instance is still not obtained after the specified time. |
Tip Reminder: For performance considerations, we can choose to use
weight strategy
orregion-sensitive strategy
instead ofpolling strategy
, because this is the most efficient execution.
5. OpenFeign call log level optimization
OpenFeign provides log enhancements, and its log levels are as follows:
- NONE [best performance, suitable for production]: By default, no logs are displayed.
- BASIC [applicable to tracking problems in production environment]: Only record request method, URL, response status code and execution time.
- HEADERS【applicable to need to view request header information】: In addition to the information defined in
BASIC
, there are request and response header information. - FULL【Comparatively suitable for development-level test environment positioning problems】: In addition to the information defined in
HEADERS
, there are also request and response body and metadata.
You can set the log level through the configuration file, and the configuration information is as follows:
logging: level: com.jacklin.mamba.contentcenter.post.feignClient.UserCenterFeignClient: debug # feign's custom interface Copy Code
6. Summary
OpenFeign
is a declarative service call and load balancing component officially launched by Spring
. In the production environment, we can optimize OpenFeign
through the following configuration to run:
- Modify the timeout period of
OpenFeign
so thatOpenFeign
can handle business correctly; - By configuring a dedicated communication component
Apache HttpClient
orOKHttp
,OpenFeign
can better supportHTTP
through the thread pool Connection objects are reused and managed to improve their performance; - Turn on the data compression function, which can improve broadband utilization and speed up data transmission speed;
- Use an appropriate load balancing strategy to replace the default round-robin load balancing strategy to achieve better execution efficiency;
- Check the log level of
OpenFeign
in the build environment, select an appropriate log output level, and prevent invalid log output.