3、openFeign日志配置

3、openFeign日志配置,第1张

3、openFeign日志配置

有时候我们遇到 Bug,比如接口调用失败、参数没收到等问题,或者想看看调用性能,就需要配置 Feign 的日志了,以此让 Feign 把请求信息输出来。

日志等级有4种,分别是:

  • NONE【性能最佳,适用于生产】:不记录任何日志(默认值)。
  • BASIC【适用于生产环境追踪问题】:仅记录请求方法、URL、响应状态代码以及执行时间。
  • HEADERS:记录BASIC级别的基础上,记录请求和响应的header。
  • FULL【比较适用于开发及测试环境定位问题】:记录请求和响应的header、body和元数据。
全局配置 一、创建product商品模块 第一步:在父聚合springcloudalibaba项目创建子模板

第二步:pom.xml添加依赖


    
        springcloudalibaba
        com.example
        0.0.1-SNAPSHOT
    
    4.0.0

    product-nacos

    
        
            org.springframework.boot
            spring-boot-starter-web
        

        
        
            com.alibaba.cloud
            spring-cloud-starter-alibaba-nacos-discovery
        
    



第三步:新建启动类
package com.example.product;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class ProductApplication {

    public static void main(String[] args) {
        SpringApplication.run(ProductApplication.class,args);
    }
}
第四步:新建application.yml
server:
  port: 8087
spring:
  application:
    name: product-service
  cloud:
    nacos:
      server-addr: 127.0.0.1:8848
      discovery:
        username: nacos
        password: nacos
第五步:新建ProductController
package com.example.product.controller;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/product")
public class ProductController {

    @Value("${server.port}")
    String port;

    @RequestMapping("/{id}")
    public String get(@PathVariable Integer id) throws InterruptedException {
        System.out.println("查询商品"+id);
        return "查询商品"+id+":"+port;
    }

}

二、在order-openfeign模块 第一步:添加FeignConfig
package com.example.order.config;

import feign.Logger;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;


@Configuration
public class FeignConfig {

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

第二步:添加ProductFeignService
package com.example.order.feign;

import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;


@FeignClient(name = "product-service",path = "/product")
public interface ProductFeignService {

    @RequestMapping("/{id}")
    String get(@PathVariable("id") Integer id);
}

第三步:修改OrderController
package com.example.order.controller;

import com.example.order.feign.ProductFeignService;
import com.example.order.feign.StockFeignService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

@RestController
@RequestMapping("/order")
public class OrderController {

    @Autowired
    private StockFeignService stockFeignService;

    @Autowired
    private ProductFeignService productFeignService;

    @RequestMapping("/add")
    public String add(){
        System.out.println("下单成功");
        String msg = stockFeignService.reduck();

        String s = productFeignService.get(1);
        return "Hello World " + msg + ",商品:"+s;
    }
}

第四步:启动测试


清除启动日志,访问http://localhost:8086/order/add我们会发现,没有打印调用提供方的信息

为什么呢?
答:因为feign调试日志是debug级别输出,springboot默认的日志级别是info,所以feign的debug日志级别就不会输出

第五步:修改application.yml
server:
  port: 8086
#应用名称(nacos会将该名称当做服务名称)
spring:
  application:
    name: order-service
  cloud:
    nacos:
      server-addr: 127.0.0.1:8848
      discovery:
        username: nacos
        password: nacos
        namespace: public
  # 因为feign调试日志是debug级别输出,springboot默认的日志级别是info,所以feign的debug日志级别就不会输出
  # logging.level=debug这样配置是对所有的日志级别进行配置
  # 该场景只需要对feign接口进行debug配置,所以是这样配置logging.level.com.example.order.feign=debug
logging:
  level:
    com.example.order.feign: debug

第六步:重启order-openfeign 重启order-openfeign,并清除启动日志。 访问:http://localhost:8086/order/add看日志

局部配置 第一种:通过配置类的方式

在order-openfeign模块

第一步:修改FeignConfig
package com.example.order.config;

import feign.Logger;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;


//@Configuration
public class FeignConfig {

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

第二步:局部配置 我们在该场景配置StockFeignService,我们想只打印StockFeignService服务的请求信息
package com.example.order.feign;

import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.RequestMapping;


@FeignClient(name = "stock-service",path = "/stock",configuration = FeignConfig.class)
public interface StockFeignService {

    
    @RequestMapping("/reduck")
    String reduck();    
}

第三步:重启order-openfeign 清除重启获取到的日志,访问http://localhost:8086/order/add,看日志我们发现只有访问stock的请求信息

第二种:通过配置文件的方式 在order-openfeign模块 该场景我们设置product商品模块的日志级别为BASIC,他会比FULL打印的少。FULL=BASIC+HEADERS+数据 第一步:修改application.yml
server:
  port: 8086
#应用名称(nacos会将该名称当做服务名称)
spring:
  application:
    name: order-service
  cloud:
    nacos:
      server-addr: 127.0.0.1:8848
      discovery:
        username: nacos
        password: nacos
        namespace: public
  # 因为feign调试日志是debug级别输出,springboot默认的日志级别是info,所以feign的debug日志级别就不会输出
  # logging.level=debug这样配置是对所有的日志级别进行配置
  # 该场景只需要对feign接口进行debug配置,所以是这样配置logging.level.com.example.order.feign=debug
logging:
  level:
    com.example.order.feign: debug
feign:
  client:
    config:
      # 提供方的服务名
      product-service:
        #请求日志级别
        loggerLevel: BASIC

第二步:重启order-feign 重启order-feign,清除启动日志和第一次访问日志,再次访问http://localhost:8086/order/add,会发现product打印会比较少。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/zaji/5693648.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-17
下一篇 2022-12-17

发表评论

登录后才能评论

评论列表(0条)

保存