[000-01-008].第05节:OpenFeign高级特性-日志打印功能
我的后端学习大纲
SpringCloud学习大纲
1、日志打印功能:
- 1.Feign 提供了日志打印功能,我们可以通过配置来调整日志级别,从而了解 Feign 中 Http 请求的细节,说白了就是对Feign接口的调用情况进行监控和输出
2、日志级别:
- NONE
:默认的
,不显示任何日志 BASIC
:仅记录请求方法、URL、响应状态码及执行时间HEADERS
:除了 BASIC 中定义的信息之外,还有请求和响应的头信息FULL
:除了 HEADERS 中定义的信息之外,还有请求和响应的正文及元数据
3、配置开启日志功能:
3.1.配置日志bean
package com.atguigu.cloud.config;
import feign.Logger;
import feign.Retryer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class FeignConfig{
@Bean
public Retryer myRetryer(){
return Retryer.NEVER_RETRY; //默认
}
@Bean
Logger.Level feignLoggerLevel() {
return Logger.Level.FULL;
}
}
3.2.YML文件里需要开启日志的Feign客户端:
- 1.公式(三段):
logging.level
+ 含有@FeignClient注解的完整带包名及接口名
+debug
- 2.YML配置:
# feign日志以什么级别监控哪个接口
logging:
level:
com:
atguigu:
cloud:
apis:
PayFeignApi: debug
3.3.测试查看后台日志:
a.带着压缩调用:
b.去掉压缩调用:
3.4测试重试机制的日志:
a.更改类FeignConfig.java,配置重试策略
package com.atguigu.cloud.config;
import feign.Logger;
import feign.Retryer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class FeignConfig{
@Bean
public Retryer myRetryer(){
//最大请求次数为3(1+2),初始间隔时间为100ms,重试间最大间隔时间为1s
return new Retryer.Default(100,1,3);
}
@Bean
Logger.Level feignLoggerLevel() {
return Logger.Level.FULL;
}
}
b.配置YML:
server:
port: 80
spring:
application:
name: cloud-consumer-openfeign-order
####Spring Cloud Consul for Service Discovery
cloud:
consul:
host: localhost
port: 8500
discovery:
prefer-ip-address: true #优先使用服务ip进行注册
service-name: ${spring.application.name}
openfeign:
client:
config:
default:
#cloud-payment-service:
#连接超时时间
connectTimeout: 2000
#读取超时时间
readTimeout: 2000
httpclient:
hc5:
enabled: true
compression:
request:
enabled: true
min-request-size: 2048
mime-types: text/xml,application/xml,application/json
response:
enabled: true
# feign日志以什么级别监控哪个接口
logging:
level:
com:
atguigu:
cloud:
apis:
PayFeignApi: debug
c.测试:
- 发送请求:
http://localhost/feign/pay/get/1
d.控制台打印:
e.完整的YML:
server:
port: 80
spring:
application:
name: cloud-consumer-openfeign-order
####Spring Cloud Consul for Service Discovery
cloud:
consul:
host: localhost
port: 8500
discovery:
prefer-ip-address: true #优先使用服务ip进行注册
service-name: ${spring.application.name}
openfeign:
client:
config:
default:
connectTimeout: 2000 #连接超时时间
readTimeout: 2000 #读取超时时间
httpclient:
hc5:
enabled: true
compression:
request:
enabled: true
min-request-size: 2048
mime-types: text/xml,application/xml,application/json
response:
enabled: true
#cloud-payment-service:
#connectTimeout: 4000 #连接超时时间
#readTimeout: 4000 #读取超时时间
# feign日志以什么级别监控哪个接口
logging:
level:
com:
atguigu:
cloud:
apis:
PayFeignApi: debug