Feign performance optimization of SpringCloud

Feign performance optimization of SpringCloud

This article will mainly explain Feign performance optimization issues, such as Gzip compression, HTTP connection pool, request timeout, etc.

1. Gzip compression

1.1 Overview

Introduction to gzip: gzip is a data format that uses the deflate algorithm to compress data; gzip is a popular file compression algorithm that is widely used, especially on Linux platforms.

Gzip capability: When Gzip compresses a plain text file , the effect is very obvious, and the file size can be reduced by more than 70% .

Function of gzip: After network data is compressed, the number of bytes transmitted on the network is actually reduced . The most obvious benefit is that it can speed up the loading of web pages. . The benefit of faster web page loading speed is self-evident. In addition to saving traffic and improving the user's browsing experience, another potential benefit is that Gzip has a better relationship with search engine crawlers.

1.2 Provisions on compressed transmission in the HTTP protocol

Client to the server with the request: Accept-Encoding:gzip, deflatefield indicates to the server compression format supported by the client (gzip or the deflate), without sending the message header, the server by default is not compressed.

After the server receives the request, if the request was found to contain header Accept-Encodingfields, and supports the compression type, it will respond to the message after compression back to the client, and carries the Content-Encoding:gzipmessage header, the message shows the response format is based on the compressed.

After the client receives a request, first determine whether there is Content-Encodinga message header, if there is, according to decompress the packet format. Otherwise, it is processed as a normal message.

1.3 Compression case

Partial configuration

Only configure Consumer's request to Provider through Feign and corresponding Gzip compression.

Service consumer application.yml

# Feign gzip  
      mime-types: text/xml,application/xml,application/json #   MIME TYPE
      min-request-size: 512                                 #   2048
      enabled: true                                         #   gzip  
      enabled: true                                         #   gzip  

Global configuration

The request to the client browser and the request and response from the Consumer to the Provider all implement Gzip compression.

Service consumer application.yml

    enabled: true
    #   MIME TYPE
    mime-types: application/json,application/xml,text/html,text/xml,text/plain

2. HTTP connection pool

We know that the process of establishing an HTTP connection is a very complicated process, involving the exchange of multiple data packets, which is time consuming. For example, when establishing a TCP connection, the overhead of three handshake and four wave of hands is relatively large.

Using HTTP connection pool , can save a lot of 3 times 4-way handshake wave, this can greatly enhance throughput.

Feign HTTP client supports three HttpURLConnectionframeworks: HttpClient, , OkHttp; default HttpURLConnection. You can view the source code org.springframework.cloud.openfeign.ribbon.FeignRibbonClientAutoConfiguration.javato know.

  • The traditional HttpURLConnection comes with the JDK and does not support connection pooling. If you want to implement the connection pool mechanism, you need to manage the connection objects yourself.

  • Compared with the HttpURLConnection that comes with the traditional JDK, HttpClient encapsulates the request headers, parameters, content body, response, etc. for accessing HTTP; it not only makes it easy for the client to send HTTP requests, but also facilitates developers to test the interface (based on HTTP protocol), which not only improves the efficiency of development, but also improves the robustness of the code

    So how to use it?

    First of all, in the corresponding consumer project, add the corresponding dependency, because the Hoxton.SR1 version used in this article has integrated the apache httpclient dependency by default, so you only need to add a dependency.

    <!--   apache httpclient   -->
    <!-- feign apache httpclient   -->

    Then, open it in the configuration file

        enabled: true #   httpclient

    **Note:** If you use HttpClient as Feign's client tool. The notation defined in the interface is to be noted, if the passed parameter is a custom objects (objects in JSON format to mail), configure parameter type, for example: @GetMapping(value = "/single/pojo", consumes = MediaType.APPLICATION_JSON_VALUE). The version of Spring CLoud used in this article no longer needs to be manually configured.

3. Request timed out

Feign's load balancing bottom layer uses Ribbon, so the request timeout configuration here is actually to configure Ribbon.

In the case of high service pressure, the process of processing the service may take a certain amount of time, and the default request timeout configuration is 1s, so we need to adjust the configuration to extend the request timeout time.

Add in the consumer configuration file:

  ConnectTimeout: 5000 #     1  
  ReadTimeout: 5000    #