SpringCloud中@FeignClient()注解的使用方式是怎樣的,很多新手對此不是很清楚,為了幫助大家解決這個難題,下面小編將為大家詳細講解,有這方面需求的人可以來學習下,希望你能有所收獲。
由于SpringCloud采用分布式微服務架構,難免在各個子模塊下存在模塊方法互相調用的情況。比如service-admin服務要調用service-card 服務的方法。
@FeignClient()注解就是為了解決這個問題的。
@FeignClient()注解的源碼要求它必須在Interface接口上使用。( FeignClient注解被@Target(ElementType.TYPE)修飾,表示FeignClient注解的作用目標在接口上)
@RequestLine與其它請求不同,只需要簡單寫請求方式和路徑就能達到請求其它服務的目的。
@FeignClient(value = "feign-server",configuration = FeignConfig.class) //需要一個配置文件 public interface TestService { @RequestLine("POST /feign/test") //對應請求方式和路徑 String feign(@RequestBody UserDO userDO); }
@EnableFeignClients @SpringBootConfiguration public class FeignConfig { @Bean public Contract contract(){ return new feign.Contract.Default(); } }
value
: 服務名
name
: 指定FeignClient的名稱,如果項目使用了Ribbon,name屬性會作為微服務的名稱,用于服務發現
url
: url一般用于調試,可以手動指定@FeignClient調用的地址
decode404
:當發生http 404錯誤時,如果該字段位true,會調用decoder進行解碼,否則拋出FeignException
configuration
: Feign配置類,可以自定義Feign的Encoder、Decoder、LogLevel、Contract
fallback
: 定義容錯的處理類,當調用遠程接口失敗或超時時,會調用對應接口的容錯邏輯,fallback指定的類必須實現@FeignClient標記的接口
fallbackFactory
: 工廠類,用于生成fallback類示例,通過這個屬性我們可以實現每個接口通用的容錯邏輯,減少重復的代碼
path
: 定義當前FeignClient的統一前綴
此外還要求服務的啟動類要有@EnableFeignClients 注解才能使Fegin生效。
SpringCloud搭建各種微服務之后,服務間通常存在相互調用的需求,SpringCloud提供了@FeignClient 注解非常優雅的解決了這個問題
首先,保證幾個服務都在一個Eureka中注冊成功形成服務場。
如下,我一共有三個服務注冊在服務場中。COMPUTE-SERVICE ; FEIGN-CONSUMER ; TEST-DEMO;
服務中調用其他兩個服務的兩個接口
分別為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 服務中調用這個方法的話,需要在 FEIGN-CONSUMER 中新建一個接口類專門調用某一工程中的系列接口
@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注解中標識出準備調用的是當前服務場中的哪個服務,這個服務名在目標服務中的配置中取
spring.application.name
接下來,在@RequestMapping中設置目標接口的接口類型、接口地址等屬性。然后在下面定義接口參數以及返回參數
Controller層調用方法的時候
將上面接口注入進來,就可以直接用了
@Autowired ComputeClient computeClient; @RequestMapping(value = "/add", method = RequestMethod.GET) public Integer add() { return computeClient.add(10, 20); }
當然,post方法同理:
這是目標接口:
@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(); }
@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(); } }
最終調用結果如下:
OK 服務間接口調用就是這樣了!
看完上述內容是否對您有幫助呢?如果還想對相關知識有進一步的了解或閱讀更多相關文章,請關注億速云行業資訊頻道,感謝您對億速云的支持。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。