1. 程式人生 > >spring cloud feign覆寫默認配置級feign client的日誌打印

spring cloud feign覆寫默認配置級feign client的日誌打印

st2 分享 color 配置 ram time 會有 logger ima

一、覆寫fegin的默認配置

1、新增配置類FeignConfiguration.java

package com.config;

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

import feign.Contract;
import feign.Logger;
@Configuration
public class FeignConfiguration {

    @Bean
    public Contract feignContract() {
        
//這裏可以配置默認配置 return new feign.Contract.Default(); } @Bean Logger.Level feignLoggerLevel() { return Logger.Level.FULL; } }

需要之一的是此配置文件不能再spring cloud掃描包的路徑下,否則會有問題出現

2、定義一個FeignClient2.java

package com.pupeiyuan.feignClient;

import java.util.List;

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.config.FeignConfiguration; import com.pupeiyuan.bean.NhReportStatusHistory; import feign.Param; import
feign.RequestLine; @FeignClient(name = "MULTIPLE",configuration = FeignConfiguration.class) public interface FeignClient2 { @RequestMapping(value = "/getDate/{id}", method = RequestMethod.GET) public List<NhReportStatusHistory> dataList(@PathVariable("id") Long id); }

3、controller中調用

FeignClientController.java

package com.pupeiyuan.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
import com.pupeiyuan.bean.NhReportStatusHistory;
import com.pupeiyuan.feignClient.FeignClient2;
import com.pupeiyuan.feignClient.UserFeignClient;

@RestController
public class FeignClientController {

    @Autowired
    private FeignClient2 feignClient2;
     
    @GetMapping("/movie2/{id}")
      public List<NhReportStatusHistory> list2(@PathVariable Long id) {
        return this.feignClient2.dataList(id);
      }
}

實現的效果和上一篇文章是一樣的

技術分享圖片

二、feign client的日誌打印

默認情況下feign是沒有日誌打印出來的,需要增加相關配置:
1、創建Feign的配置文件,並在其中設置日誌等級

/**
 * Feign 客戶端配置
 *
 * @author xushiling
 * @date 2018/8/13
 */
@Configuration
public class FeignConfiguration {
    @Bean
    Logger.Level feignLoggerLevel() {
        //這裏記錄所有,根據實際情況選擇合適的日誌level
        return Logger.Level.FULL;
    }
}

這裏的level級別控制如下

NONE, No logging (DEFAULT).
BASIC, Log only the request method and URL and the response status code and execution time.
HEADERS, Log the basic information along with request and response headers.
FULL, Log the headers, body, and metadata for both requests and responses.
NONE, 不記錄 (DEFAULT).
BASIC, 僅記錄請求方式和URL及響應的狀態代碼與執行時間.
HEADERS, 日誌的基本信息與請求及響應的頭.
FULL, 記錄請求與響應的頭和正文及元數據.

2、在客戶端接口指定此配置

package com.pupeiyuan.feignClient;

import java.util.List;

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.config.FeignConfiguration;
import com.pupeiyuan.bean.NhReportStatusHistory;

import feign.Param;
import feign.RequestLine;

@FeignClient(name = "MULTIPLE",configuration = FeignConfiguration.class)
public interface FeignClient2 {

    @RequestMapping(value = "/getDate/{id}", method = RequestMethod.GET)
    public List<NhReportStatusHistory> dataList(@PathVariable("id") Long id);
}

3、配置文件開啟日誌記錄
application.properties設置:
logging.level.com.haoait.client.UserServiceClient:debug
如果是yml配置文件則做如下配置:

logging:
  level:
    com.haoait.client.UserServiceClient:debug

日誌輸出如下:

技術分享圖片

spring cloud feign覆寫默認配置級feign client的日誌打印