Spring Cloud Alibaba OpenFeign 日志配置

OpenFeign提供了日志打印功能,我们可以通过配置来调整日恙级别,从而了解Feign 中 Http请求的细节。

说白了就是对Feign接口的调用情况进行监控和输出

日志级别

  • NONE:默认的,不显示任何日志;
  • BASIC:仅记录请求方法、URL、响应状态码及执行时间;
  • HEADERS:除了BASIC中定义的信息之外,还有请求和响应的头信息;
  • FULL:除了HEADERS中定义的信息之外,还有请求和响应的正文及元数据。
package com.wsm.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("id") Integer id){
        System.out.println("查询商品");
        return "查询商品"+id+":"+port;
    }
}

 

package com.wsm.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}")
    public String get(@PathVariable("id") Integer id);
}

 

 

package com.wsm.order.config;

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

/**
 * 全局配置: 当使用@Configuration会将配置作用所有的报务提供方
 * 局部配置: 如果只针对个别服务进行配置,就不要加@Configuration
 *
 */
@Configuration
public class FeignConfig {

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


}

 

 

package com.wsm.order.controller;

import com.wsm.order.feign.ProductFeignService;
import com.wsm.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;

import javax.swing.*;

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

//    @Autowired
//    RestTemplate restTemplate;
    @Autowired
    StockFeignService stockFeignService;
    @Autowired
    ProductFeignService productFeignService;

    @RequestMapping("/add")
    public String add(){
        System.out.println("aaaaaaaaaaaaa");
//        String msg = restTemplate.getForObject("http://localhost:8011/stock/reduct", String.class);
//        String msg = restTemplate.getForObject("http://stock-service/stock/reduct", String.class);
        String stock_msg = stockFeignService.reduct();
        String product_msg = productFeignService.get(1);
        return "hello feign "+ stock_msg +" "+product_msg;
    }
}

 

server:
  port: 8040
  #应用名称  (nacos 会将该名称当作服务名称)
spring:
  application:
    name: order-openfeign-service
  cloud:
    nacos:
#      server-addr: 127.0.0.1:8848
      server-addr: 192.168.133.128:8847  #集群 nginx 负载均衡访问 nacos
      discovery:
        username: nacos
        password: nacos
        namespace: public
#springboot 默认的日志级别是info,feign的debug日志级别就不会输出
logging:
  level:
    com.wsm.order.feign: debug

 

 

 

 

 

 

package com.wsm.order.config;

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

/**
 * 全局配置: 当使用@Configuration会将配置作用所有的报务提供方
 * 局部配置: 如果只针对个别服务进行配置,就不要加@Configuration
 *
 */
//@Configuration
public class FeignConfig {

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

}

 

package com.wsm.order.feign;

import com.wsm.order.config.FeignConfig;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.RequestMapping;

/**
 * 添加feign接口的方法
 * name 指定调用rest接口所对应的服务名
 * path 指定调用rest接口所在的StockController指定的@RequestMapping
 */
@FeignClient(name = "stock-service",path="/stock",configuration = FeignConfig.class)
public interface StockFeignService {

    //声明需要调用的rest接口对应的方法
    @RequestMapping("/reduct")//与 StockController 中的reduct()方法的@RequestMapping一致
    public String reduct(); //与 StockController 中的reduct()方法对应
}

 

server:
  port: 8040
  #应用名称  (nacos 会将该名称当作服务名称)
spring:
  application:
    name: order-openfeign-service
  cloud:
    nacos:
#      server-addr: 127.0.0.1:8848
      server-addr: 192.168.133.128:8847  #集群 nginx 负载均衡访问 nacos
      discovery:
        username: nacos
        password: nacos
        namespace: public
#springboot 默认的日志级别是info,feign的debug日志级别就不会输出
logging:
  level:
#    com.wsm.order.feign: debug
    com.wsm.order.feign.StockFeignService: debug

 

 

 

 

 

 

 

局部配置有两种方式,一是上面的配置类,二是通过配置文件

server:
  port: 8040
  #应用名称  (nacos 会将该名称当作服务名称)
spring:
  application:
    name: order-openfeign-service
  cloud:
    nacos:
#      server-addr: 127.0.0.1:8848
      server-addr: 192.168.133.128:8847  #集群 nginx 负载均衡访问 nacos
      discovery:
        username: nacos
        password: nacos
        namespace: public
#springboot 默认的日志级别是info,feign的debug日志级别就不会输出
logging:
  level:
#    com.wsm.order.feign: debug
    com.wsm.order.feign.StockFeignService: debug
# Feign 日志局部配置
feign:
  client:
    config:
      product-service:
        loggerLevel: BASIC

 

 

 

 

posted @ 2021-11-07 12:58  残星  阅读(662)  评论(0编辑  收藏  举报