Spring Cloud系列之Feign的常見問題總結

1、FeignClient接口,不能使用@GettingMapping 之類的組合註解spring

代碼示例:app

@FeignClient("microservice-provider-user")
public interface UserFeignClient {
 @RequestMapping(value = "/simple/{id}", method = RequestMethod.GET)
 public User findById(@PathVariable("id") Long id);
 ...
}

這邊的@RequestMapping(value = "/simple/{id}", method = RequestMethod.GET) 不能寫成@GetMapping("/simple/{id}") ide

2、FeignClient接口中,若是使用到@PathVariable ,必須指定其value學習

代碼示例:ui

@FeignClient("microservice-provider-user")
public interface UserFeignClient {
 @RequestMapping(value = "/simple/{id}", method = RequestMethod.GET)
 public User findById(@PathVariable("id") Long id);
 ...
}

這邊的@PathVariable("id") 中的」id」,不能省略,必須指定。url

3、FeignClient多參數的構造spa

若是想要請求microservice-provider-user 服務,而且參數有多個例如:http://microservice-provider-user/query-by?id=1&username=張三 要怎麼辦呢?.net

直接使用複雜對象:code

@FeignClient("microservice-provider-user")
public interface UserFeignClient {
 @RequestMapping(value = "/query-by", method = RequestMethod.GET)
 public User queryBy(User user);
 ...
}

該請求不會成功,只要參數是複雜對象,即便指定了是GET方法,feign依然會以POST方法進行發送請求。htm

正確的寫法:

寫法1:

@FeignClient("microservice-provider-user")
public interface UserFeignClient {
 @RequestMapping(value = "/query-by", method = RequestMethod.GET)
 public User queryBy(@RequestParam("id")Long id, @RequestParam("username")String username);
}

寫法2:

@FeignClient(name = "microservice-provider-user")
public interface UserFeignClient {
 @RequestMapping(value = "/query-by", method = RequestMethod.GET)
 public List<User> queryBy(@RequestParam Map<String, Object> param);
}

4、Feign若是想要使用Hystrix Stream,須要作一些額外操做

咱們知道Feign自己就是支持Hystrix的,能夠直接使用@FeignClient(value = "microservice-provider-user", fallback = XXX.class) 來指定fallback的類,這個fallback類集成@FeignClient所標註的接口便可。

可是假設咱們須要使用Hystrix Stream進行監控,默認狀況下,訪問http://IP:PORT/hystrix.stream 是個404。如何爲Feign增長Hystrix Stream支持呢?

須要如下兩步:

第一步:添加依賴,示例:

<!-- 整合hystrix,其實feign中自帶了hystrix,引入該依賴主要是爲了使用其中的hystrix-metrics-event-stream,用於dashboard -->
<dependency>
 <groupId>org.springframework.cloud</groupId>
 <artifactId>spring-cloud-starter-hystrix</artifactId>
</dependency>

第二步:在啓動類上添加@EnableCircuitBreaker 註解,示例:

@SpringBootApplication
@EnableFeignClients
@EnableDiscoveryClient
@EnableCircuitBreaker
public class MovieFeignHystrixApplication {
 public static void main(String[] args) {
 SpringApplication.run(MovieFeignHystrixApplication.class, args);
 }
}

這樣修改之後,訪問任意的API後,再訪問http://IP:PORT/hystrix.stream,就會展現出一大堆的API監控數據了。

5、若是須要自定義單個Feign配置,Feign的@Configuration 註解的類不能與@ComponentScan 的包重疊

若是包重疊,將會致使全部的Feign Client都會使用該配置。

6、首次請求失敗

詳見:解決Spring Cloud中Feign/Ribbon第一次請求失敗的方法

7、@FeignClient 的屬性注意點

(1) serviceId屬性已經失效,儘可能使用name屬性。例如:

@FeignClient(serviceId = "microservice-provider-user")

這麼寫是不推薦的,應寫爲:

@FeignClient(name = "microservice-provider-user")

(2) 在使用url屬性時,在老版本的Spring Cloud中,不須要提供name屬性,可是在新版本(例如Brixton、Camden)@FeignClient必須提供name屬性,而且name、url屬性支持佔位符。例如:

@FeignClient(name = "${feign.name}", url = "${feign.url}")

總結

以上就是這篇文章的所有內容了,但願本文的內容對你們的學習或者工做能帶來必定的幫助,若是有疑問你們能夠留言交流,謝謝你們對腳本之家的支持

相關文章
相關標籤/搜索