一、FeignClient接口,不能使用@GettingMapping 之類的組合注解
代碼示例:
@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
代碼示例:
@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=張三 要怎么辦呢?
直接使用復雜對象:
@FeignClient("microservice-provider-user")
public interface UserFeignClient {
@RequestMapping(value = "/query-by", method = RequestMethod.GET)
public User queryBy(User user);
...
}
該請求不會成功,只要參數是復雜對象,即使指定了是GET方法,feign依然會以POST方法進行發送請求。
正確的寫法:
寫法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);
}
四、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監控數據了。
五、如果需要自定義單個Feign配置,Feign的@Configuration 注解的類不能與@ComponentScan 的包重疊
如果包重疊,將會導致所有的Feign Client都會使用該配置。
六、首次請求失敗
詳見:解決Spring Cloud中Feign/Ribbon第一次請求失敗的方法
七、@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}")
總結
以上就是這篇文章的全部內容了,希望本文的內容對大家的學習或者工作能帶來一定的幫助,如果有疑問大家可以留言交流,謝謝大家對腳本之家的支持。
<div class="art_xg">
您可能感興趣的文章:
- 使用Spring Cloud Feign作為HTTP客戶端調用遠程HTTP服務的方法(推薦)
- 詳解spring cloud Feign使用中遇到的問題總結
- spring cloud feign不支持@RequestBody+ RequestMethod.GET報錯的解決方法
- 詳解springcloud Feign的Hystrix支持
- SpringCloud之Feign示例詳解
- 使用Spring Cloud Feign上傳文件的示例
- spring cloud 之 Feign 使用HTTP請求遠程服務的實現方法
- 解決Spring Cloud中Feign/Ribbon第一次請求失敗的方法
- Spring Cloud Feign簡單使用詳解
