一、FeignClient接口,不能使用@GettingMapping 之類的組合注解
代碼示例:
1
2
3
4
5
6
|
@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}")
。
二、FeignClient接口中,如果使用到@PathVariable ,必須指定其value
代碼示例:
1
2
3
4
5
6
|
@FeignClient ( "microservice-provider-user" ) public interface UserFeignClient { @RequestMapping (value = "/simple/{id}" , method = RequestMethod.GET) public User findById( @PathVariable ( "id" ) Long id); ... } |
這邊的@PathVariable("id")
中的”id”,不能省略,必須指定。
三、FeignClient多參數的構造
如果想要請求microservice-provider-user 服務,并且參數有多個例如:http://microservice-provider-user/query-by?id=1&username=張三 要怎么辦呢?
直接使用復雜對象:
1
2
3
4
5
6
|
@FeignClient ( "microservice-provider-user" ) public interface UserFeignClient { @RequestMapping (value = "/query-by" , method = RequestMethod.GET) public User queryBy(User user); ... } |
該請求不會成功,只要參數是復雜對象,即使指定了是GET方法,feign依然會以POST方法進行發送請求。
正確的寫法:
寫法1:
1
2
3
4
5
|
@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:
1
2
3
4
5
|
@FeignClient (name = "microservice-provider-user" ) public interface UserFeignClient { @RequestMapping (value = "/query-by" , method = RequestMethod.GET) public List<User> queryBy( @RequestParam Map<String, Object> param); } |
四、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支持呢?
需要以下兩步:
第一步:添加依賴,示例:
1
2
3
4
5
|
<!-- 整合hystrix,其實feign中自帶了hystrix,引入該依賴主要是為了使用其中的hystrix-metrics-event-stream,用于dashboard --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-hystrix</artifactId> </dependency> |
第二步:在啟動類上添加@EnableCircuitBreaker 注解,示例:
1
2
3
4
5
6
7
8
9
|
@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監控數據了。
五、如果需要自定義單個Feign配置,Feign的@Configuration 注解的類不能與@ComponentScan 的包重疊
如果包重疊,將會導致所有的Feign Client都會使用該配置。
六、首次請求失敗
詳見:解決Spring Cloud中Feign/Ribbon第一次請求失敗的方法
七、@FeignClient 的屬性注意點
(1) serviceId屬性已經失效,盡量使用name屬性。例如:
1
|
@FeignClient (serviceId = "microservice-provider-user" ) |
這么寫是不推薦的,應寫為:
1
|
@FeignClient (name = "microservice-provider-user" ) |
(2) 在使用url屬性時,在老版本的Spring Cloud中,不需要提供name屬性,但是在新版本(例如Brixton、Camden)@FeignClient必須提供name屬性,并且name、url屬性支持占位符。例如:
1
|
@FeignClient (name = "${feign.name}" , url = "${feign.url}" ) |
總結
以上就是這篇文章的全部內容了,希望本文的內容對大家的學習或者工作能帶來一定的幫助,如果有疑問大家可以留言交流,謝謝大家對服務器之家的支持。
原文鏈接:http://www.itmuch.com/spring-cloud-sum-feign/