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

扫描二维码关注公众号,回复: 8597560 查看本文章

只添加上面配置还无法实现对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)

测试效果和在启动类上配置效果一样

                                                                               微信公众号: 

                                               

                                                                             JAVA程序猿成长之路

                          分享资源,记录程序猿成长点滴。专注于Java,Spring,SpringBoot,SpringCloud,分布式,微服务。 

猜你喜欢

转载自www.cnblogs.com/niugang0920/p/12193286.html