spring clould feign的坑

1. 學習用feign 時,一開始用@GetMapping 註解還可遠程調用,沒有問題

在當用@PathVariable傳路徑變量時,服務總報500.
百度發現 @PathVariable時,@RequestMapping不能用GetMapping直接替換,必須用對其value參數直接賦值纔可用。正確如下:

@FeignClient(value = "test")
public interface IFeignTestService {
    // if PathVariable is used, it is forbidden to use @GetMapping to replace @RequestMapping;
    @RequestMapping(value = "/test/ribbon/test/{id}", method = RequestMethod.GET)
    public Object getTest(@PathVariable("id") String id);
}

2. 使用Feign 自定義配置時,爲防止 @Configuration 被當成@Component加載,以致對所有@FeignClient 生效。

當用@ComponentScan 顯示加載指定包,排除FeignConguration。
對@FeignClient的想要使用指定自定義配置的,可用conguration屬性來指定

3. Feign 調用接口的日誌

日誌級別枚舉類Logger.Level

NONE:不輸出日誌
BASIC:輸出請求方法、URL、響應狀態碼、執行時間
HEADERS:基本信息以及請求和響應頭
FULL:請求和響應的heads、body、metadata,建議使用這個級別

注:1. Feign的Level日誌級別配置默認是:NONE,不要跟log日誌混淆。 
   2. feign 日誌的  log日誌級別爲DEBUG.

/**

  • if this configuration is scanned by the @ComponentScan, it will take effects on all the FeignClients.

  • Therefore, we use IgnoreComponentScan to avoid this configuration’s being scanned so that it can work

  • the specific Feign clients with annotation @FeignClient taking FeignConfiguration.class as the value of

  • its attribute configuration.
    */
    @Configuration
    @IgnoreComponentScan
    public class FeignConfiguration {

    /**

    • set feign log level basic, this need the debug level of
    • the logger(for example logback)to print the feign logs.
    • @return
      */
      @Bean
      Logger.Level feignLoggerLevel() {
      return Logger.Level.BASIC;
      }

}

參: Spring Cloud中Feign常見問題

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章