Feign之日志输出
在我们日常开发过程中,经常会查看日志解决问题,那么Feign
是不是也有日志输出呢?
答案:是的。
在构建@FeignClient
注解修饰的服务客户端时,会为每一个客户端都创建一个Feign.Logger
实例,可以利用该日志对象的Debug
模式来分析Feign
的请求细节。
环境配置如下:
- SpringBoot:2.1.1.RELEASE
- SpringCloud:Greenwich.RC1
- Java:1.8
- Maven:3.5.2
Feign日志输出说明
Feign
的Level
日志级别配置默认是:NONE
,不要跟log
日志混淆。
日志级别枚举类
Logger.Level
:
NONE
:不输出日志BASIC
:输出请求方法、URL
、响应状态码、执行时间HEADERS
:基本信息以及请求和响应头FULL
:请求和响应的heads
、body
、metadata
,建议使用这个级别
前期准备
- 一个服务注册中心
tairan-spring-cloud-eureka
,端口8761
。参考SpringCloud组件:搭建Eureka服务注册中心 - 创建
tairan-spring-cloud-feign-api
核心工程,对外提供api
接口。 - 创建服务提供者
tairan-spring-cloud-feign-privder
工程,提供/hello
接口,端口为10001
,并注册到服务中心。参考SpringCloud组件:将微服务提供者注册到Eureka服务中心 - 创建服务消费者
tairan-spring-cloud-feign-logger
工程,提供/feign-hello
接口,端口号为10003
,并注册到服务中心。参考SpringCloud组件:将微服务提供者注册到Eureka服务中心
注意:2、3步骤
tairan-spring-cloud-feign-api
和tairan-spring-cloud-feign-privder
项目借用SpringCloud组件:创建第一个Feign客户端文章中创建的项目。
构建项目
同样的是采用idea
开发工具创建一个SpringBoot
项目,在依赖选择界面对应的添加Web
、Feign
以及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配置
- 添加
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
可以扫描到的路径下。
- 因为我习惯
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
- 入口类修改
@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);
}
}
运行测试
- 启动服务注册中心
tairan-spring-cloud-eureka
- 启动服务提供方
tairan-spring-cloud-feign-provider
- 启动服务消费方
tairan-spring-cloud-feign-logger
- 访问
tairan-spring-cloud-feign-logger
工程的/feign-hello
接口,链接:http://localhost:10003/feign-hello,查看idea
控制台打印结果- 关闭服务提供方
tairan-spring-cloud-feign-provider
- 访问
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