Spring Cloud OpenFeign REST服務客戶端原理及用法解析
OpenFeign是什么?
OpenFeign是REST服務客戶端,REST其實就是HTTP啦,所以OpenFeign其實就是HTTP客戶端,那么他和HttpClient有什么不同呢
OpenFeign的使用方法更加的簡單 OpenFeign配合Spring的HttpMessageConverters可以自動把結果轉換成Java對象 OpenFeign配合Ribbon、Eureka和Spring Cloud LoadBalancer可以支持負載均衡如何使用OpenFeign
第一步引入OpenFeign
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-openfeign</artifactId></dependency>
第二步啟動OpenFeign客戶端功能
@SpringBootApplication@EnableFeignClientspublic class Application { public static void main(String[] args) { SpringApplication.run(Application.class, args); } }
第三步編寫REST服務接口
@FeignClient(name = 'stores', url = 'http://localhost:7074')<br data-filtered='filtered'>public interface StoreClient { @RequestMapping(method = RequestMethod.GET, value = '/stores') List<Store> getStores(); @RequestMapping(method = RequestMethod.POST, value = '/stores/{storeId}', consumes = 'application/json') Store update(@PathVariable('storeId') Long storeId, Store store);}
在@FeignClient中的字符串稱為Feign客戶端名字,它可以是任意的字符串,設置名字的目的就是為了方便在其它地方引用它,例如配置Rabbin或Spring Cloud LoadBalancer負載均衡(后面會詳細介紹如何做)。
在@FeignClient中還可以設置url參數(shù),它表示提供REST服務的地址,如果你沒有設置url參數(shù),那么就要在配置文件中配置。
之后我們就可以把StoreClient注入到我們需要使用的地方啦。
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持好吧啦網。
相關文章:
1. .NET中l(wèi)ambda表達式合并問題及解決方法2. CSS hack用法案例詳解3. PHP設計模式中工廠模式深入詳解4. ASP.NET MVC遍歷驗證ModelState的錯誤信息5. Ajax實現(xiàn)表格中信息不刷新頁面進行更新數(shù)據(jù)6. asp(vbs)Rs.Open和Conn.Execute的詳解和區(qū)別及&H0001的說明7. 解決AJAX返回狀態(tài)200沒有調用success的問題8. ThinkPHP5實現(xiàn)JWT Token認證的過程(親測可用)9. JSP數(shù)據(jù)交互實現(xiàn)過程解析10. ASP 信息提示函數(shù)并作返回或者轉向
