SpringCloud组件:Feign之日志输出

Feign之日志输出

在我们日常开发过程中,经常会查看日志解决问题,那么Feign是不是也有日志输出呢?
答案:是的。
在构建@FeignClient注解修饰的服务客户端时,会为每一个客户端都创建一个Feign.Logger实例,可以利用该日志对象的Debug模式来分析Feign的请求细节。

环境配置如下:

  • SpringBoot:2.1.1.RELEASE
  • SpringCloud:Greenwich.RC1
  • Java:1.8
  • Maven:3.5.2

Feign日志输出说明

FeignLevel日志级别配置默认是:NONE,不要跟log日志混淆。

日志级别枚举类Logger.Level

  1. NONE:不输出日志
  2. BASIC:输出请求方法、URL、响应状态码、执行时间
  3. HEADERS:基本信息以及请求和响应头
  4. FULL:请求和响应的headsbodymetadata,建议使用这个级别

前期准备

  1. 一个服务注册中心tairan-spring-cloud-eureka,端口8761。参考SpringCloud组件:搭建Eureka服务注册中心
  2. 创建tairan-spring-cloud-feign-api核心工程,对外提供api接口。
  3. 创建服务提供者tairan-spring-cloud-feign-privder工程,提供/hello接口,端口为10001,并注册到服务中心。参考SpringCloud组件:将微服务提供者注册到Eureka服务中心
  4. 创建服务消费者tairan-spring-cloud-feign-logger工程,提供/feign-hello接口,端口号为10003,并注册到服务中心。参考SpringCloud组件:将微服务提供者注册到Eureka服务中心

注意:2、3步骤tairan-spring-cloud-feign-apitairan-spring-cloud-feign-privder项目借用SpringCloud组件:创建第一个Feign客户端文章中创建的项目。

构建项目

同样的是采用idea开发工具创建一个SpringBoot项目,在依赖选择界面对应的添加WebFeign以及Eureka Discovery依赖,直接完成创建项目。
项目的pom.xml内容如下所示:

......//省略部分
tairan-spring-cloud-feign-logger
tairan-spring-cloud-feign-logger
Demo project for Spring Boot


    UTF-8
    UTF-8
    1.8
    true



    
        org.springframework.cloud
        spring-cloud-starter-netflix-eureka-client
    
    
        org.springframework.cloud
        spring-cloud-starter-openfeign
    
    
        org.springframework.boot
        spring-boot-starter-web
    
    
        com.tairan.chapter
        tairan-spring-cloud-feign-api
    

    
        org.springframework.boot
        spring-boot-starter-test
        test
    

......//省略部分

tairan-spring-cloud-feign-logger配置

  1. 添加FeignConfig配置类,修改Feign日志输出级别,代码如下:
package com.tairan.chapter.feign.log.config;

import feign.Logger;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * Feign配置
 * 该配置放到SpringBoot可以扫描到的路径下
 */
@Configuration
public class FeignConfig {

    @Bean
    Logger.Level feignLevel() {
        return Logger.Level.FULL;
    }
}

注意:该配置放到SpringBoot可以扫描到的路径下。

  1. 因为我习惯yaml配置方式,所以更改了application.properties文件为application.yml

logging.level.=debug开启指定Feign客户端的DEBUG模式日志;
Feign客户端定义接口的完整路径

application.yml文件代码如下所示:

spring:
  application:
    name: tairan-spring-cloud-feign-logger
server:
  port: 10003
eureka:
  client:
    service-url:
      defaultZone: http://localhost:8761/eureka/
      # defaultZone: http://litairan:litairan@localhost:8761/eureka/
# 开启Feign请求压缩
feign:
  compression:
    request:
      enabled: true
      mime-types: text/xml,application/xml,application/json
      min-request-size: 2048
    response:
      enabled: true
  # Dalston SR1(待定)之后的版本默认关闭hystrix对feign的支持,如果想要使用fallback功能这里必须启用
  hystrix:
    enabled: true
logging:
  level:
    com.tairan.chapter.feign.api.HelloService: debug
  1. 入口类修改@SpringBootApplication扫描路径,可以扫描到Hystrix熔断类,即tairan-spring-cloud-feign-api中的HelloServiceHystrix类,在应用主类中通过@EnableFeignClients注解开启Feign功能,因为需要注册到服务中心,所以还需要@EnableDiscoveryClient注解,代码如下所示:
package com.tairan.chapter.feign.log;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.openfeign.EnableFeignClients;

@SpringBootApplication(scanBasePackages = "com.tairan.chapter.feign")
@EnableDiscoveryClient
@EnableFeignClients("com.tairan.chapter.feign.api")
public class TairanSpringCloudFeignLoggerApplication {

    public static void main(String[] args) {
        SpringApplication.run(TairanSpringCloudFeignLoggerApplication.class, args);
    }

}

运行测试

  1. 启动服务注册中心tairan-spring-cloud-eureka
  2. 启动服务提供方tairan-spring-cloud-feign-provider
  3. 启动服务消费方tairan-spring-cloud-feign-logger
  4. 访问tairan-spring-cloud-feign-logger工程的/feign-hello接口,链接:http://localhost:10003/feign-hello,查看idea控制台打印结果
  5. 关闭服务提供方tairan-spring-cloud-feign-provider
  6. 访问tairan-spring-cloud-feign-logger工程的/feign-hello接口,链接:http://localhost:10003/feign-hello,查看idea控制台打印结果

执行4操作后,访问链接http://localhost:10003/feign-hello后,idea控制台打印结果如下所示:

2019-03-01 19:46:17.191 DEBUG 6643 --- [ign-provider-10] c.tairan.chapter.feign.api.HelloService  : [HelloService#getMessage] ---> GET http://tairan-spring-cloud-feign-provider/hello HTTP/1.1
2019-03-01 19:46:17.191 DEBUG 6643 --- [ign-provider-10] c.tairan.chapter.feign.api.HelloService  : [HelloService#getMessage] Accept-Encoding: gzip
2019-03-01 19:46:17.191 DEBUG 6643 --- [ign-provider-10] c.tairan.chapter.feign.api.HelloService  : [HelloService#getMessage] Accept-Encoding: deflate
2019-03-01 19:46:17.191 DEBUG 6643 --- [ign-provider-10] c.tairan.chapter.feign.api.HelloService  : [HelloService#getMessage] ---> END HTTP (0-byte body)
2019-03-01 19:46:17.197 DEBUG 6643 --- [ign-provider-10] c.tairan.chapter.feign.api.HelloService  : [HelloService#getMessage] <--- HTTP/1.1 200 (5ms)
2019-03-01 19:46:17.197 DEBUG 6643 --- [ign-provider-10] c.tairan.chapter.feign.api.HelloService  : [HelloService#getMessage] cache-control: no-cache, no-store, max-age=0, must-revalidate
2019-03-01 19:46:17.197 DEBUG 6643 --- [ign-provider-10] c.tairan.chapter.feign.api.HelloService  : [HelloService#getMessage] content-length: 40
2019-03-01 19:46:17.197 DEBUG 6643 --- [ign-provider-10] c.tairan.chapter.feign.api.HelloService  : [HelloService#getMessage] content-type: text/plain;charset=UTF-8
2019-03-01 19:46:17.197 DEBUG 6643 --- [ign-provider-10] c.tairan.chapter.feign.api.HelloService  : [HelloService#getMessage] date: Fri, 01 Mar 2019 11:46:17 GMT
2019-03-01 19:46:17.197 DEBUG 6643 --- [ign-provider-10] c.tairan.chapter.feign.api.HelloService  : [HelloService#getMessage] expires: 0
2019-03-01 19:46:17.197 DEBUG 6643 --- [ign-provider-10] c.tairan.chapter.feign.api.HelloService  : [HelloService#getMessage] pragma: no-cache
2019-03-01 19:46:17.197 DEBUG 6643 --- [ign-provider-10] c.tairan.chapter.feign.api.HelloService  : [HelloService#getMessage] x-content-type-options: nosniff
2019-03-01 19:46:17.197 DEBUG 6643 --- [ign-provider-10] c.tairan.chapter.feign.api.HelloService  : [HelloService#getMessage] x-frame-options: DENY
2019-03-01 19:46:17.197 DEBUG 6643 --- [ign-provider-10] c.tairan.chapter.feign.api.HelloService  : [HelloService#getMessage] x-xss-protection: 1; mode=block
2019-03-01 19:46:17.197 DEBUG 6643 --- [ign-provider-10] c.tairan.chapter.feign.api.HelloService  : [HelloService#getMessage] 
2019-03-01 19:46:17.198 DEBUG 6643 --- [ign-provider-10] c.tairan.chapter.feign.api.HelloService  : [HelloService#getMessage] tairan-spring-cloud-feign-provider:10001
2019-03-01 19:46:17.198 DEBUG 6643 --- [ign-provider-10] c.tairan.chapter.feign.api.HelloService  : [HelloService#getMessage] <--- END HTTP (40-byte body)

执行5操作后,访问链接http://localhost:10003/feign-hello后,idea控制台打印结果如下所示:

2019-03-01 19:47:27.782 DEBUG 6643 --- [ign-provider-10] c.tairan.chapter.feign.api.HelloService  : [HelloService#getMessage] ---> GET http://tairan-spring-cloud-feign-provider/hello HTTP/1.1
2019-03-01 19:47:27.782 DEBUG 6643 --- [ign-provider-10] c.tairan.chapter.feign.api.HelloService  : [HelloService#getMessage] Accept-Encoding: gzip
2019-03-01 19:47:27.782 DEBUG 6643 --- [ign-provider-10] c.tairan.chapter.feign.api.HelloService  : [HelloService#getMessage] Accept-Encoding: deflate
2019-03-01 19:47:27.782 DEBUG 6643 --- [ign-provider-10] c.tairan.chapter.feign.api.HelloService  : [HelloService#getMessage] ---> END HTTP (0-byte body)
2019-03-01 19:47:27.791 DEBUG 6643 --- [ign-provider-10] c.tairan.chapter.feign.api.HelloService  : [HelloService#getMessage] <--- ERROR ConnectException: Connection refused (Connection refused) (8ms)
2019-03-01 19:47:27.792 DEBUG 6643 --- [ign-provider-10] c.tairan.chapter.feign.api.HelloService  : [HelloService#getMessage] java.net.ConnectException: Connection refused (Connection refused)
    at java.net.PlainSocketImpl.socketConnect(Native Method)
    at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:350)
    ……

2019-03-01 19:47:27.792 DEBUG 6643 --- [ign-provider-10] c.tairan.chapter.feign.api.HelloService  : [HelloService#getMessage] <--- END ERROR

如上所示,Feign的日志输出级别修改成功。

源码位置

本章源码已经上传到淡若悠然,请结合源码进行学习,感谢阅读。

码云地址(本章源码):https://gitee.com/litairan/tairan-spring-cloud

你可能感兴趣的:(SpringCloud组件:Feign之日志输出)