OpenFeign 日志配置
假设现在有这么一个工程 mall4cloud,它有三个模块,order-openfeign、user、product。
order-openfeign 通过 feign 调用 user、product。
uer、product 分别对外提供服务:
@RestController
@RequestMapping("/user")
public class UserController {@Value("${server.port}")private String port;@RequestMapping("/findUserById/{id}")public String findUserById(@PathVariable String id) {// 模拟从数据库获取用户Map<String, String> map = new HashMap<>();map.put("1", "小林:"+port);map.put("2", "小王:"+port);return map.get(id);}
}
---------------------------------------------------
@RestController
@RequestMapping("/product")
public class ProductController {@Value("${server.port}")private String port;@RequestMapping("/findProductById/{id}")public String findProductById(@PathVariable String id) {// 模拟从数据库获取用户Map<String, String> map = new HashMap<>();map.put("1", "产品1:"+port);map.put("2", "产品2:"+port);return map.get(id);}
}
order-openfeign 有两个 feign 调用:
@FeignClient(value = "user-service", path = "/user")
public interface UserFeignService {@RequestMapping("/findUserById/{id}")String findUserById(@PathVariable("id") String id);
}
---------------------------------------------------
@FeignClient(value = "product-service", path = "/product")
public interface ProductFeignService {@RequestMapping("/findProductById/{id}")String findProductById(@PathVariable("id") String id);
}
一、日志配置
有时候为了调试或监控请求,配置日志记录是非常有用的。
在 OpenFeign 中,可以通过配置日志级别来控制日志输出的内容。
1.1、日志级别
OpenFeign 支持四种日志级别:
- NONE - 不记录任何信息(默认值)。
- BASIC【适用于生产环境追踪问题】 - 仅记录请求方法和URL以及响应状态代码和执行时间。
- HEADERS - 在BASIC的基础上,还记录请求和响应头。
- FULL【使用于开发及测试环境定位问题】- 记录请求和响应的头、体以及元数据。
1.2、配置方式
- Java Bean
- yml 配置方式
这两种配置其实都可以。建议在 yml 中配置,可以利用配置中心对配置进行统一管理。
1.3、配置的作用范围
- 全局生效
- 局部生效
二、Java Bean 全局生效
2.1、定义一个配置类,指定日志级别
package com.study.order.config;import feign.Logger;
import org.springframework.cloud.openfeign.EnableFeignClients;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;@Configuration
@EnableFeignClients(basePackages="com.study.order.feign")
public class FeignConfig {@Beanpublic Logger.Level feignLoggerLevel() {return Logger.Level.BASIC;}
}
2.2、在 application.yml 中配置 Client 的日志级别为 debug
格式是"logging.level.feign接口包路径=debug"
2.3、调用测试
调用 order-openfeign 下单接口:
localhost:8011/order/placeOrderFeign/1
@RestController
@RequestMapping("/order")
public class OrderController {@Autowiredprivate UserFeignService userFeignService;@Autowiredprivate ProductFeignService productFeignService;@RequestMapping("/placeOrderFeign/{userId}")public String placeOrderFeign(@PathVariable String userId) {String user = userFeignService.findUserById(userId);String product = productFeignService.findProductById("1");return "用户[" + user + "]下单成功!购买商品[" + product + "]";}
}
BISIC 记录的信息:
- 请求方法-GET
- URL-http://user-service/user/findUserById/1
- 协议-HTTP/1.1
- 响应状态代码-200
- 执行时间-85ms
从这也能看出,不管对于 UserFeignService 的调用,还是 ProductFeignService 的调用,使用全局配置,都能打印日志。
2.4、FULL 测试
@Bean
public Logger.Level feignLoggerLevel() {return Logger.Level.FULL;
}
再次调用后台日志:
FULL 记录的信息:
记录请求和响应的头、体以及元数据。
三、 Java Bean 局部生效
在指定的微服务 @FeignClient 注解中指定 configuration 属性
2.1、注释掉上一个配置类,feignLoggerLevel
@Configuration
@EnableFeignClients(basePackages="com.study.order.feign")
public class FeignConfig {/*@Beanpublic Logger.Level feignLoggerLevel() {return Logger.Level.FULL;}*/
}
2.2、新建一个 feign 日志配置类
public class FeignLoggerConfig {@Beanpublic Logger.Level feignLoggerLevel() {return Logger.Level.FULL;}
}
2.3、在 UserFeignService @FeignClient 注解 configuration 指定这个配置类
@FeignClient(value = "user-service", path = "/user", configuration = FeignLoggerConfig.class)
public interface UserFeignService {@RequestMapping("/findUserById/{id}")String findUserById(@PathVariable("id") String id);}
2.4、调用测试
localhost:8011/order/placeOrderFeign/1
可以看到这次只有 UserFeignService 的调用日志。
四、yml 全局生效
yml 配置文件配置方式:
- 全局生效:配置 {服务名} 为 default
- 局部生效:配置 {服务名} 为 具体服务名
4.1、去除 UserFeignService @FeignClient 注解 configuration 配置
4.2、修改 applicatiom.yml
logging:level:com.study.order.feign: DEBUG
feign:client:config:default:logger-level: BASIC
注意!
不同版本的 springcloud 配置会有不同。我的版本是
<spring.cloud.version>2021.0.5</spring.cloud.version>
4.3、调用测试
可以看到 UserFeignService 和 ProductFeignService 的调用都打印出日志。
五、yml 局部生效
配置全局日志级别为 FULL,UserFeignService 为 FULL
logging:level:com.study.order.feign: DEBUG
feign:client:config:default: # 全局生效logger-level: BASICuser-service: # 服务名logger-level: FULL