本文節選自《瘋狂Spring Cloud微服務架構實戰》html
京東購買地址:https://item.jd.com/12256011.htmljava
噹噹網購買地址:http://product.dangdang.com/25201393.htmlgit
Spring Cloud教學視頻:https://my.oschina.net/JavaLaw/blog/1552993spring
Spring Cloud電子書:https://my.oschina.net/JavaLaw/blog/1570383瀏覽器
前面講解了Feign的使用,在瞭解如何單獨使用Feign後,再學習Spring Cloud中使用Feign,將會有很是大的幫助。雖然Spring Cloud對Feign進行了封裝,但萬變不離其宗,只要瞭解其內在原理,使用起來就能夠駕輕就熟。服務器
在開始本小節前,先準備Spring Cloud的測試項目。測試案例主要有如下三個項目:架構
爲服務調用者(spring-feign-invoker)的pom.xml文件加入如下依賴:app
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-feign</artifactId> </dependency>
在服務調用者的啓動類中,打開Feign開關,請見代碼清單5-18。負載均衡
代碼清單5-18:框架
codes\05\5.3\spring-feign-invoker\src\main\java\org\crazyit\cloud\InvokerApplication.java
@SpringBootApplication @EnableEurekaClient @EnableFeignClients public class InvokerApplication { public static void main(String[] args) { SpringApplication.run(InvokerApplication.class, args); } }
接下來,編寫客戶端接口,與直接使用Feign相似,代碼清單5-19爲服務端接口。
代碼清單5-19:
codes\05\5.3\spring-feign-invoker\src\main\java\org\crazyit\cloud\PersonClient.java
@FeignClient("spring-feign-provider") //聲明調用的服務名稱 public interface PersonClient { @RequestMapping(method = RequestMethod.GET, value = "/hello") String hello(); }
與單獨使用Fiegn不一樣的是,接口使用了@FeignClient註解來修飾,而且聲明瞭須要調用的服務名稱,本例的服務提供者名稱爲「spring-feign-provider」。另外,接口方法使用了@RequestMapping來修飾,根據5.2.7章節可知,經過編寫「翻譯器(Contract)」,可讓Feign知道第三方註解的含義,Spring Cloud也提供翻譯器,會將@RequestMapping註解的含義告知Feign,所以咱們的服務接口就能夠直接使用該註解。
除了方法的@RequestMapping註解外,默認還支持@RequestParam、@RequestHeader、@PathVariable這3個參數註解,也就是說,在定義方法時,可使用方式定義參數:
@RequestMapping(method = RequestMethod.GET, value = "/hello/{name}") String hello(@PathVariable("name") String name);
須要注意的是,使用了Spring Cloud的「翻譯器」後,將不能再使用Feign的默認註解。接下來,在控制器中調用接口方法,請見代碼清單是5-20。
代碼清單5-20:
codes\05\5.3\spring-feign-invoker\src\main\java\org\crazyit\cloud\InvokerController.java
@RestController @Configuration public class InvokerController { @Autowired private PersonClient personClient; @RequestMapping(value = "/invokeHello", method = RequestMethod.GET) public String invokeHello() { return personClient.hello(); } }
在控制器中,爲其注入了PersonClient的bean,不難看出,客戶端實例的建立及維護,Spring容器都幫咱們實現了。查看本例的效果,請按如下步驟操做:
在前面章節,咱們嘗試過編寫自定義的Feign客戶端,在Spring Cloud中,一樣提供了自定義的Feign客戶端。可能你們已經猜到,若是結合Ribbon使用,Spring Cloud所提供的客戶端,會擁有負載均衡的功能。
Spring Cloud實現的Feign客戶端,類名爲LoadBalancerFeignClient,在該類中,維護着與SpringClientFactory相關的實例,經過SpringClientFactory能夠獲取負載均衡器,負載均衡器會根據必定的規則來選取處理請求的服務器,最終實現負載均衡的功能。接下來,調用「服務提供者」的「/person/{personId}」服務來測試負載均衡。爲客戶端接口添加內容,請見代碼清單5-21。
代碼清單5-21:
codes\05\5.3\spring-feign-invoker\src\main\java\org\crazyit\cloud\PersonClient.java
@RequestMapping(method = RequestMethod.GET, value = "/person/{personId}") Person getPerson(@PathVariable("personId") Integer personId);
爲「服務調用者」的控制器添加方法,請見代碼清單5-22。
代碼清單5-22:
@RequestMapping(value = "/router", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE) public String router() { // 調用服務提供者的接口 Person p = personClient.getPerson(2); return p.getMessage(); }
運行「服務調用者」,在瀏覽器中輸入:http://localhost:9000/router,進行刷新,能夠看到8080與8081端口被循環調用。
Spring Cloud爲Feign的使用提供了各類默認屬性,例如前面講到的「註解翻譯器(Contract)」、Feign客戶端,默認狀況下,Spring將會爲Feign的屬性提供瞭如下的Bean:
通常狀況下,Spring提供的這些Bean已經足夠咱們使用,若是有些更特殊的需求,能夠實現本身的Bean,請見後面章節。
本文節選自《瘋狂Spring Cloud微服務架構實戰》
Spring Cloud教學視頻:https://my.oschina.net/JavaLaw/blog/1552993
Spring Cloud電子書:https://my.oschina.net/JavaLaw/blog/1570383
本書代碼共享地址:https://gitee.com/yangenxiong/SpringCloud