SpringCloud之@FeignClient()注解的使用方式
@FeignClient()注解的使用
由于SpringCloud采用分布式微服務(wù)架構(gòu),難免在各個子模塊下存在模塊方法互相調(diào)用的情況。比如service-admin服務(wù)要調(diào)用service-card 服務(wù)的方法。
- @FeignClient()注解就是為了解決這個問題的。
- @FeignClient()注解的源碼要求它必須在Interface接口上使用。( FeignClient注解被@Target(ElementType.TYPE)修飾,表示FeignClient注解的作用目標(biāo)在接口上)
@RequestLine與其它請求不同,只需要簡單寫請求方式和路徑就能達(dá)到請求其它服務(wù)的目的。
@FeignClient(value = "feign-server",configuration = FeignConfig.class) //需要一個配置文件 public interface TestService { @RequestLine("POST /feign/test") //對應(yīng)請求方式和路徑 String feign(@RequestBody UserDO userDO); }
@EnableFeignClients @SpringBootConfiguration public class FeignConfig { @Bean public Contract contract(){ return new feign.Contract.Default(); } }
@FeignClient標(biāo)簽的常用屬性如下
value
: 服務(wù)名name
: 指定FeignClient的名稱,如果項目使用了Ribbon,name屬性會作為微服務(wù)的名稱,用于服務(wù)發(fā)現(xiàn)url
: url一般用于調(diào)試,可以手動指定@FeignClient調(diào)用的地址decode404
:當(dāng)發(fā)生http 404錯誤時,如果該字段位true,會調(diào)用decoder進(jìn)行解碼,否則拋出FeignExceptionconfiguration
: Feign配置類,可以自定義Feign的Encoder、Decoder、LogLevel、Contractfallback
: 定義容錯的處理類,當(dāng)調(diào)用遠(yuǎn)程接口失敗或超時時,會調(diào)用對應(yīng)接口的容錯邏輯,fallback指定的類必須實現(xiàn)@FeignClient標(biāo)記的接口fallbackFactory
: 工廠類,用于生成fallback類示例,通過這個屬性我們可以實現(xiàn)每個接口通用的容錯邏輯,減少重復(fù)的代碼path
: 定義當(dāng)前FeignClient的統(tǒng)一前綴
此外還要求服務(wù)的啟動類要有@EnableFeignClients 注解才能使Fegin生效。
SpringCloud 服務(wù)間互相調(diào)用 @FeignClient注解
SpringCloud搭建各種微服務(wù)之后,服務(wù)間通常存在相互調(diào)用的需求,SpringCloud提供了@FeignClient 注解非常優(yōu)雅的解決了這個問題
首先,保證幾個服務(wù)都在一個Eureka中注冊成功形成服務(wù)場。
如下,我一共有三個服務(wù)注冊在服務(wù)場中。COMPUTE-SERVICE ; FEIGN-CONSUMER ; TEST-DEMO;
我在FEIGN-CONSUMER
服務(wù)中調(diào)用其他兩個服務(wù)的兩個接口
分別為get帶參和post不帶參兩個接口如下這個是COMPUTE-SERVICE中的get帶參方法
@RequestMapping(value = "/add" ,method = RequestMethod.GET) public Integer add(@RequestParam Integer a, @RequestParam Integer b) { ServiceInstance instance = client.getLocalServiceInstance(); Integer r = a + b; logger.info("/add, host:" + instance.getHost() + ", service_id:" + instance.getServiceId() + ", result:" + r); return r; }
如果要在FEIGN-CONSUMER 服務(wù)中調(diào)用這個方法的話,需要在 FEIGN-CONSUMER 中新建一個接口類專門調(diào)用某一工程中的系列接口
@FeignClient("compute-service") public interface ComputeClient { @RequestMapping(method = RequestMethod.GET, value = "/add") Integer add(@RequestParam(value = "a") Integer a, @RequestParam(value = "b") Integer b); }
其中,@FeignClient注解中標(biāo)識出準(zhǔn)備調(diào)用的是當(dāng)前服務(wù)場中的哪個服務(wù),這個服務(wù)名在目標(biāo)服務(wù)中的配置中取
spring.application.name
接下來,在@RequestMapping中設(shè)置目標(biāo)接口的接口類型、接口地址等屬性。然后在下面定義接口參數(shù)以及返回參數(shù)
在FEIGN-CONSUMER
Controller層調(diào)用方法的時候
將上面接口注入進(jìn)來,就可以直接用了
@Autowired ComputeClient computeClient; @RequestMapping(value = "/add", method = RequestMethod.GET) public Integer add() { return computeClient.add(10, 20); }
當(dāng)然,post方法同理:
這是目標(biāo)接口:
@RestController @RequestMapping("/demo") @EnableAutoConfiguration public class HelloController { @RequestMapping(value = "/test",method = RequestMethod.POST) String test1(){ return "hello,test1()"; } }
這是在本項目定義的接口文件:
@FeignClient("test-Demo") public interface TestDemo { @RequestMapping(method = RequestMethod.POST, value = "/demo/test") String test(); }
這是項目中的Controller層
@RestController public class ConsumerController { @Autowired TestDemo testDemo; @Autowired ComputeClient computeClient; @RequestMapping(value = "/add", method = RequestMethod.GET) public Integer add() { return computeClient.add(10, 20); } @RequestMapping(value = "/test", method = RequestMethod.GET) public String test() { return testDemo.test(); } }
最終調(diào)用結(jié)果如下:
OK 服務(wù)間接口調(diào)用就是這樣了!
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
Springboot多數(shù)據(jù)源配置之整合dynamic-datasource方式
這篇文章主要介紹了Springboot多數(shù)據(jù)源配置之整合dynamic-datasource方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-03-03Java并發(fā) CompletableFuture異步編程的實現(xiàn)
這篇文章主要介紹了Java并發(fā) CompletableFuture異步編程的實現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-01-01SpringMVC?RESTFul實戰(zhàn)案例訪問首頁
這篇文章主要為大家介紹了SpringMVC?RESTFul實戰(zhàn)案例訪問首頁,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-05-05