Feign之日志輸出
在我們?nèi)粘i_發(fā)過程中铣耘,經(jīng)常會(huì)查看日志解決問題渊涝,那么Feign
是不是也有日志輸出呢扼睬?
答案:是的。
在構(gòu)建@FeignClient
注解修飾的服務(wù)客戶端時(shí)蹋辅,會(huì)為每一個(gè)客戶端都創(chuàng)建一個(gè)Feign.Logger
實(shí)例钱贯,可以利用該日志對(duì)象的Debug
模式來分析Feign
的請(qǐng)求細(xì)節(jié)。
環(huán)境配置如下:
- SpringBoot:2.1.1.RELEASE
- SpringCloud:Greenwich.RC1
- Java:1.8
- Maven:3.5.2
Feign日志輸出說明
Feign
的Level
日志級(jí)別配置默認(rèn)是:NONE
侦另,不要跟log
日志混淆秩命。
日志級(jí)別枚舉類
Logger.Level
:
NONE
:不輸出日志BASIC
:輸出請(qǐng)求方法尉共、URL
、響應(yīng)狀態(tài)碼弃锐、執(zhí)行時(shí)間HEADERS
:基本信息以及請(qǐng)求和響應(yīng)頭FULL
:請(qǐng)求和響應(yīng)的heads
袄友、body
、metadata
霹菊,建議使用這個(gè)級(jí)別
前期準(zhǔn)備
- 一個(gè)服務(wù)注冊(cè)中心
tairan-spring-cloud-eureka
剧蚣,端口8761
。參考SpringCloud組件:搭建Eureka服務(wù)注冊(cè)中心 - 創(chuàng)建
tairan-spring-cloud-feign-api
核心工程浇辜,對(duì)外提供api
接口券敌。 - 創(chuàng)建服務(wù)提供者
tairan-spring-cloud-feign-privder
工程,提供/hello
接口柳洋,端口為10001
待诅,并注冊(cè)到服務(wù)中心。參考SpringCloud組件:將微服務(wù)提供者注冊(cè)到Eureka服務(wù)中心 - 創(chuàng)建服務(wù)消費(fèi)者
tairan-spring-cloud-feign-logger
工程熊镣,提供/feign-hello
接口卑雁,端口號(hào)為10003
,并注冊(cè)到服務(wù)中心绪囱。參考SpringCloud組件:將微服務(wù)提供者注冊(cè)到Eureka服務(wù)中心
注意:2测蹲、3步驟
tairan-spring-cloud-feign-api
和tairan-spring-cloud-feign-privder
項(xiàng)目借用SpringCloud組件:創(chuàng)建第一個(gè)Feign客戶端文章中創(chuàng)建的項(xiàng)目。
構(gòu)建項(xiàng)目
同樣的是采用idea
開發(fā)工具創(chuàng)建一個(gè)SpringBoot
項(xiàng)目鬼吵,在依賴選擇界面對(duì)應(yīng)的添加Web
扣甲、Feign
以及Eureka Discovery
依賴,直接完成創(chuàng)建項(xiàng)目齿椅。
項(xiàng)目的pom.xml
內(nèi)容如下所示:
......//省略部分
<artifactId>tairan-spring-cloud-feign-logger</artifactId>
<name>tairan-spring-cloud-feign-logger</name>
<description>Demo project for Spring Boot</description>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
<skipTests>true</skipTests>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>com.tairan.chapter</groupId>
<artifactId>tairan-spring-cloud-feign-api</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
......//省略部分
tairan-spring-cloud-feign-logger配置
- 添加
FeignConfig
配置類琉挖,修改Feign
日志輸出級(jí)別,代碼如下:
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
可以掃描到的路徑下涣脚。
- 因?yàn)槲伊?xí)慣
yaml
配置方式示辈,所以更改了application.properties
文件為application.yml
。
logging.level.<FeignClient>=debug開啟指定
Feign
客戶端的DEBUG
模式日志遣蚀;
<FeignClient>
為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請(qǐng)求壓縮
feign:
compression:
request:
enabled: true
mime-types: text/xml,application/xml,application/json
min-request-size: 2048
response:
enabled: true
# Dalston SR1(待定)之后的版本默認(rèn)關(guān)閉hystrix對(duì)feign的支持矾麻,如果想要使用fallback功能這里必須啟用
hystrix:
enabled: true
logging:
level:
com.tairan.chapter.feign.api.HelloService: debug
- 入口類修改
@SpringBootApplication
掃描路徑,可以掃描到Hystrix
熔斷類芭梯,即tairan-spring-cloud-feign-api
中的HelloServiceHystrix
類险耀,在應(yīng)用主類中通過@EnableFeignClients
注解開啟Feign
功能,因?yàn)樾枰?cè)到服務(wù)中心玖喘,所以還需要@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);
}
}
運(yùn)行測(cè)試
- 啟動(dòng)服務(wù)注冊(cè)中心
tairan-spring-cloud-eureka
- 啟動(dòng)服務(wù)提供方
tairan-spring-cloud-feign-provider
- 啟動(dòng)服務(wù)消費(fèi)方
tairan-spring-cloud-feign-logger
- 訪問
tairan-spring-cloud-feign-logger
工程的/feign-hello
接口,鏈接:http://localhost:10003/feign-hello芒涡,查看idea
控制臺(tái)打印結(jié)果- 關(guān)閉服務(wù)提供方
tairan-spring-cloud-feign-provider
- 訪問
tairan-spring-cloud-feign-logger
工程的/feign-hello
接口柴灯,鏈接:http://localhost:10003/feign-hello,查看idea
控制臺(tái)打印結(jié)果
執(zhí)行4操作后费尽,訪問鏈接http://localhost:10003/feign-hello后赠群,idea
控制臺(tái)打印結(jié)果如下所示:
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)
執(zhí)行5操作后,訪問鏈接http://localhost:10003/feign-hello后旱幼,idea
控制臺(tái)打印結(jié)果如下所示:
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
的日志輸出級(jí)別修改成功。
源碼位置
本章源碼已經(jīng)上傳到淡若悠然
柏卤,請(qǐng)結(jié)合源碼進(jìn)行學(xué)習(xí)冬三,感謝閱讀。