Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

The composition and configuration method of Spring Cloud Feign

2025-01-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

This article introduces the relevant knowledge of "the composition and configuration of Spring Cloud Feign". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

The component interface function of Feign defaults to the entry of Feign.BuilderFeign what is used by the bottom layer of Feign.BuilderClientFeign to request cooperation with Ribbon: LoadBalancerFeignClient

When not cooperating with Ribbon: Fgien.Client.DefaultContract contract, annotations support SpringMVCContractEncoder decoder for converting exclusive to HTTP request message body SpringEncoderDecoder encoder, and corresponding message body to object ResponseEntityDecoderLogger log manager Slf4jLoggerRequestInterceptor is used to add generic logic to each request (interceptor, example: want to bring heared to each request) No Feign journal level log level print content NONE (default) does not record any log BASIC only records request method, URL On the basis of the response status code and the execution time (suitable for production environment) HEADERS records the BASIC level, the headerFULL record request chord ineader,body and metadata for recording requests and responses first how to integrate Feign

Follow the first step of SpringBoot's three axes: add dependence

Org.springframework.cloud spring-cloud-starter-openfeign

Step 2: write notes

@ EnableFeignClients / / add to the startup class

Step 3: write the configuration

How to add log level fine granularity to Feign

Method 1: code implementation step 1: add a Feign configuration class, which can be added under the main class, but do not add @ Configuration. If @ Configuration is added and placed under the main class, all Feign client instances will be shared, with the same parent-child context loading conflict as the Ribbon configuration class; if @ Configuration must be added, it will be placed in a package other than the main class load. It is recommended not to add @ Configuration.

Public class FeignConfig {@ Bean public Logger.Level Logger () {return Logger.Level.FULL;}}

Step 2: add a configuration class to @ FeignClient

/ / @ FeignClient configuration = GoodsFeignConfig.class fine-grained configuration, specifying the configuration class @ FeignClient (name = "goods", configuration = FeignConfig.class)

Step 4: write the configuration

Logging: level: com.xxx.xxx.FeignAPI: DEBUG # need to write the full path of FeignClient interface # enable log format to logging.level.+Feign client path

Method 2: configure attribute implementation

Feign: client: config: # the name of the microservice you want to call: server-1: loggerLevel: FULL global configuration

Method 1: if @ Configuration is added to the code implementation and placed under the main class, then all Feign client instances will be shared, and the parent-child context loading conflict will be the same as the Ribbon configuration class; the only correct way to make the parent-child context ComponentScan overlap (strongly not recommended)

/ / add the defaultConfiguration configuration @ EnableFeignClients (defaultConfiguration = FeignConfig.class) to the @ EnableFeignClients annotation on the startup class

Method 2: configure attribute implementation

Feign: client: config: # change the name of the called microservice to default and configure it to be global. Default: loggerLevel: FULLFeign supports configuration item code mode configuration item action Logger.Level specifies log level Retryer specifies retry policy ErrorDecoder specifies error decoder Request.Options timeout Collection interceptor SetterFactory is used to set the configuration properties of Hystrix Fgien integration Hystrix will use the configuration attribute feign: client: config: feignName: connectTimeout: 5000 # equivalent to Request.Optionsn connection timeout readTimeout: 5000 # equivalent to Request.Options read timeout loggerLevel: full # configure Feign log level, equivalent to Logger errorDecoder: com.example.SimpleErrorDecoder # Feign error decoder in code configuration mode It is equivalent to ErrorDecoder retryer: com.example.SimpleRetryer # configuration retry in code configuration mode, and Retryer requestInterceptors: # configuration interceptor in code configuration mode. It is equivalent to whether the RequestInterceptor-com.example.FooRequestInterceptor-com.example.BarRequestInterceptor # in the code configuration mode decodes the error decode404: false encode: com.example.SimpleEncoder decoder: com.example.SimpleDecoder contract: com.example.SimpleContract

Feign also supports GZIP compression of requests and responses to improve communication efficiency, as follows:

# configure request GZIP compression feign.compression.request.enabled=true# configuration response GZIP compression feign.compression.response.enabled=true# configuration compression supported lower limit of MIME TYPEfeign.compression.request.mime-types=text/xml,application/xml,application/json# configuration compression data size feign.compression.request.min-request-size=2048Ribbon configuration VS Feign configuration granularity RibbonFeign code local @ RibbonClient (configuration=RibbonConfig.class) The RibbonConfig class must add @ Configuration, and must put @ FeignClient (configuration=FeignConfig.class) under the package that cannot be scanned by the parent context, and the @ Configuration of the FeignConfig class may not be added (optional). If so, it must be placed under the package that cannot be scanned by the parent context. The code global @ RibbonClients (defaultConfigurtion=RibbonConfig.class) @ EnableFeignClients (defaultConfiguration = FeignConfig.class)

... Configuration properties local .ribbon.NFLoadBalancerClassName

... feign.client.config..loggerLevel

... Configuration properties global no feign.client.config.default.loggerLevelFeign code mode VS configuration properties configuration mode has some disadvantages, code configuration is based on code, and is more flexible. If the configuration class of Feign is annotated with @ Configuration, you should pay attention to the parent-child context, online modifications need to be repackaged, and publishing properties configuration is easy to use.

Configuration is more intuitive

Online changes do not need to be repackaged and released

No code configuration is more flexible in extreme scenarios with higher priority.

Priority: fine-grained attribute configuration > fine-grained code configuration > global attribute configuration > global code configuration

This is the end of the content of "the composition and configuration of Spring Cloud Feign". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report