一 介绍
很多场景下,需要了解Feign处理请求的具体要求,那么如何满足这种需求呢?
Feign对日志的处理非常灵活,可为每个Feign客户端指定日志记录策略,每个Feign客户端都会创建一个logger。默认情况下,logger的名称是Feigh接口的完整类名。需要注意的是,Feign的日志打印只会对DEBUG级别做出响应。
我们可以为每个Feign客户端配置各种的Logger.Level对象,告诉Feign记录哪些日志。Logger.Level的值有以下选择。
NONE,无记录(DEFAULT)。
BASIC,只记录请求方法和URL以及响应状态代码和执行时间。
HEADERS,记录基本信息以及请求和响应标头。
FULL,记录请求和响应的头文件,正文和元数据。
二 新建项目microservice-consumer-movie-feign-logging
三 编写Feign的配置类
package com.itmuch.cloud.config; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import feign.Logger; @Configuration public class FeignLogConfiguration { @Bean Logger.Level feignLoggerLevel() { return Logger.Level.BASIC; } }
四 修改Feign的接口,指定配置类
package com.itmuch.cloud.study.user.feign; import org.springframework.cloud.netflix.feign.FeignClient; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import com.itmuch.cloud.config.FeignLogConfiguration; import com.itmuch.cloud.study.user.entity.User; @FeignClient(name = "microservice-provider-user", configuration = FeignLogConfiguration.class) public interface UserFeignClient { @RequestMapping(value = "/{id}", method = RequestMethod.GET) public User findById(@PathVariable("id") Long id); }
五 修改application.yml
server: port: 8010 spring: application: name: microservice-consumer-movie eureka: client: serviceUrl: defaultZone:http://localhost:8761/eureka/ instance: prefer-ip-address: true logging: level: com.itmuch.cloud.study.user.feign.UserFeignClient: DEBUG # 将Feign接口的日志级别设置成DEBUG,因为Feign的Logger.Level只对DEBUG作出响应。
六 测试
1 启动eureka
2 启动user微服务
3 启动feign
4 访问http://localhost:8010/user/1
2018-06-17 14:31:40.350 DEBUG 3876 --- [provider-user-2] c.i.c.study.user.feign.UserFeignClient : [UserFeignClient#findById] ---> GET
http://microservice-provider-user/1 HTTP/1.1
2018-06-17 14:31:40.373 DEBUG 3876 --- [provider-user-2] c.i.c.study.user.feign.UserFeignClient : [UserFeignClient#findById] <--- HTTP/1.1 200 (23ms)