Master “Version H & Alibaba & Link Tracking & Logging & Transaction & Lock”



OpenFeign combat development and parameter tuning

OpenFeign is basically used

OpenFeign profile

OpenFeign is a declarative HTTP client that makes it very easy to write web service clients by creating an interface and adding annotations to the interface. OpenFeign’s predecessor was Feign, which is now defunct. OpenFeign is the annotation that SpringCloud supports Spring MVC based on Feign, and generates implementation classes to do load balancing and call other services through dynamic proxy.

Ribbon+RestTemplate is cumbersome and OpenFeign can simplify development

The basic use

Take the example of a user service invoking a commodity

The user service is configured with OpenFeign

  1. Import dependence

    <dependency>

       <groupId>org.springframework.cloud</groupId>

       <artifactId>spring-cloud-starter-openfeign</artifactId>

      </dependency>

    Copy the code
  2. Annotate the startup class

    1594003407081

Write the called interface in the commodity service (no-parameter interface test)

image-20210106170542955

Write service calls directly using Feign in the user service

  1. Create a plain Java interface

    image-20210106164841421
  2. A client that declares the current interface to be Feign through annotations

    image-20210106164857772
  3. Write the methods of the service invocation in the Feign interface

    image-20210106170626537
  4. Use the Feign interface in the Controller of the user service

    image-20210106170650566
  5. Start all services two goods services one user service registry access user services observe service calls

    Controllers accessing user services can see the effect of load balancing

    image-20210106170751469
    image-20210106170805767
  6. process

    1594004633229

Handling of passing parameters

The parameters are passed in JSON and are actually RestFul requests

  1. /{} Splice parameters

    1. Example of invoked interface [Goods and Services]

      image-20210106171156012
    2. Feign Interface Example [User Service]

      image-20210106171220466
  2. ? The concatenation parameter corresponds to the common request type Get request

    1. Example of invoked interface [Goods and Services]

      image-20210106171304020
    2. Feign Interface Example [User Service]

      image-20210106171415861
  3. The request body pass parameter corresponds to a common request Post request

    1. Example of invoked interface [Goods and Services]

      image-20210106171535072
    2. Feign Interface Example [User Service]

      image-20210106171644918

Open the log

Unlike RestTemplate, Feign encapsulates the details of the request more thoroughly. It does not see the request, the parameters of the request, or the state of the response. To see the details of the request, Feign logs are required

Feign log configuration

1. Configure class @bean

image-20200319112307671
@Bean

public Logger.Level feignConfig(a){

    return Logger.Level.FULL;

}

Copy the code

2. Enable the log function for the package where the Feign interface resides in the configuration file

image-20210106172033726

Restart the project with the above configuration and use the Feign service call again to see the following log:

image-20200421164205211
image-20200421164257236

Feign parameter tuning

1. Replace OKHttp

By default, spring Cloud Feign uses THE JDK’s HttpURLConnection for HTTP components to make calls between sub-services, not thread pools.

There are two thread pools to choose from: HttpClient and OKHttp. OKHttp is recommended because request encapsulation is very easy to use and performance is ok.

Add the dependent

<dependency>

    <groupId>com.squareup.okhttp3</groupId>

    <artifactId>okhttp</artifactId>

</dependency>

Copy the code

Modifying a Configuration File

feign:

  okhttp:

    enabled: true

  httpclient:

    enabled: false

    max-connections: 1000

    max-connections-per-route: 100

Copy the code

Max-connections: indicates the maximum number of connections

Max-connections-per-route: indicates the number of connections per URL

2. Enable Feign request response compression

Compression can effectively save network resources, but increases CPU pressure. Therefore, you are advised to increase the size of the least compressed document

## Enable Feign request response compression

feign.compression.request.enabled=true



feign.compression.response.enabled=true

Configure the compressed document type and the minimum compressed document size

feign.compression.request.mime-types=text/xml,application/xml,application/json



feign.compression.request.min-request-size=2048

Copy the code

If you find this article helpful:

  1. Click “like” to support it, so that more people can see this content.

  2. Share your thoughts with me in the comments section, and record your thought process in the comments section.

  3. If you feel good, you can also follow the personal public account of programming deer to see more articles and explanation videos (thank you for your encouragement and support 🌹🌹🌹)