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 全局生效

利用  @Configuration 实现 全局生效,对所有的微服务调用者都生效

2.1、定义一个配置类,指定日志级别

在 order-openfeign 中,定义一个配置类
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 {

    @Bean
    public 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 {

    @Autowired
    private UserFeignService userFeignService;

    @Autowired
    private 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 记录的信息:

从这也能看出,不管对于 UserFeignService 的调用,还是 ProductFeignService 的调用,使用全局配置,都能打印日志。

2.4、FULL 测试

将 openfeign 的日志级别,改为 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 {

    /*@Bean
    public Logger.Level feignLoggerLevel() {
        return Logger.Level.FULL;
    }*/
}

2.2、新建一个 feign 日志配置类

public class FeignLoggerConfig {

    @Bean
    public 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: BASIC
      user-service: # 服务名
        logger-level: FULL

六、源码

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值