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


一、覆寫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 {

  

  @RequestLine("GET /getDate/{id}")
  public List<NhReportStatusHistory> dataList(@Param("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 {

  @RequestLine("GET /getDate/{id}")
  public List<NhReportStatusHistory> dataList(@Param("id") Long id);

}

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

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

日志輸出如下:


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM