14.Spring-Cloud-Feign中請求壓縮配置和日志配置


   請求壓縮配置

    Spring Cloud  feign支持對請求與響應進行GZIP壓縮,以減少通信中的性能損耗,主要是在spring-cloud-netflix-core.jar文件中.

默認對請求和相應壓縮是禁用的,從
org.springframework.cloud.netflix.feign.encoding.FeignContentGzipEncodingAutoConfiguration類中可以看到請求壓縮是禁用的
源碼:
@ConditionalOnProperty(value = "feign.compression.request.enabled", matchIfMissing = false)
org.springframework.cloud.netflix.feign.encoding.FeignAcceptGzipEncodingAutoConfiguration.class類中可以看到響應壓縮是禁用的
源碼:

@ConditionalOnProperty(value = "feign.compression.response.enabled", matchIfMissing = false)

org.springframework.cloud.netflix.feign.encoding.FeignClientEncodingProperties類中配置了一些默認的編碼屬性
源碼:
@ConfigurationProperties("feign.compression.request")
public class FeignClientEncodingProperties {
    /**
     * The list of supported mime types.
     */
    private String[] mimeTypes = new String[]{"text/xml", "application/xml", "application/json"};
    /**
     * The minimum threshold content size.
     */
    private int minRequestSize = 2048;

}

在application.properties中自定義請求壓縮

feign.compression.request.enabled=true
feign.compression.response.enabled=true
#默認配置
feign.compression.request.mine-types=text/xml,application/xml,application/json 
#默認配置
feign.compression.request.min-request-size=2048
 
        

日志配置

在構建@FeignClient注解修飾的服務客戶端時,會為一個客戶端都創建一個feign.Logger實例,可以利用該日志對象的DEBUG模式來分析Feign的請求細節。具體配置在application.properties中配置:

logging.level.<FeignClient>=DEBUG開啟指定Feign客戶端的DEBUG模式日志;

<FeignClient>為Feign客戶端定義接口的完整路徑

如:
logging.level.com.niugang.service.HelloService=DEBUG

只添加上面配置還無法實現對DEBUG日志的輸出,以因為Feign客戶端默認的logger.level對象定義為none級別,所以不會記錄feign調用過程中的信息。

feign中日志級別
  /**
   * Controls the level of logging.
   */
  public enum Level {
    /**
     * No logging.不記錄日志
     */
    NONE,
    /**
*只記錄請求方法和URL,以及響應狀態代碼和執行時間。
     * Log only the request method and URL and the response status code and execution time.
     */
    BASIC,
    /**
*記錄請求和響應頭基本信息。
     * Log the basic information along with request and response headers.
     */
    HEADERS,
    /**
*記錄請求和響應的標頭、正文和元數據
     * Log the headers, body, and metadata for both requests and responses.
     */
    FULL

  }

創建方式一:在啟動類上配置

@SpringBootApplication
@EnableDiscoveryClient
//掃描聲明它們是feign客戶端的接口(通過@FeignClient)
@EnableFeignClients
public class Application {
@Bean
Logger.Level feignLoggerLevel(){
return Logger.Level.FULL;
}
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}

 

 


測試

   輸入:http://localhost:9001/feign-consumer1/zhangsan

   輸入:http://localhost:9001/feign-consumer2/zhangsan/15094031789

創建方式二:定義配置類

 

1.創建專門的配置類

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import feign.Logger;
@Configuration
public class FeignLoggerConfig {
@Bean
Logger.Level feignLoggerLevel(){
return Logger.Level.FULL;
}
}

 

2.在服務調用接口上進行綁定

//value:調用服務的名
//fallback:配置服務降級類
//configuration:定制客戶端的自定義@configuration。可以包含對組成客戶端的片段的重寫@Bean定義

@FeignClient(value="service-provide",fallback=ServiceProvideFallBack.class,configuration=FeignLoggerConfig.class)

測試效果和在啟動類上配置效果一樣

 微信公眾號

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM