最近結合一些別人的開源項目來學習Spring Cloud,其中關於服務消費這方面的一個很便利的工具 Feign讓我記憶頗深。雖然網上的Demo和例子不勝枚舉,但大多比較分散,本文就來集中記錄一下聲明式客戶端 Feign的一些使用姿式。git
注: 本文首發於 博客 CodeSheep · 程序羊,歡迎光臨 小站!github
下文就結合例子來記錄這一過程,代碼在文尾處。web
三個步驟便可搞定:spring
建立一個名爲 eureka_server
的 SpringBoot工程,並在pom.xml中添加好對應依賴api
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-eureka-server</artifactId> </dependency>
修改應用主類,添加 @EnableEurekaServer
註解瀏覽器
@EnableEurekaServer @SpringBootApplication public class EurekaServerApplication { public static void main(String[] args) { SpringApplication.run(EurekaServerApplication.class, args); } }
配置 application.properties
文件以下所示:服務器
spring.application.name=eureka-server server.port=1111 eureka.instance.hostname=localhost #默認設置下,服務註冊中心本身也會將本身做爲客戶端來嘗試註冊它本身,因此咱們須要禁用它的客戶端註冊行爲 eureka.client.register-with-eureka=false eureka.client.fetch-registry=false
瀏覽器訪問之:app
此時尚未任何服務註冊上來。框架
建立一個名爲 service_provider
的 SpringBoot工程,並在pom.xml中添加好對應依賴:ide
<dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-eureka</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> </dependencies>
添加 @EnableDiscoveryClient
註解
@EnableDiscoveryClient @SpringBootApplication public class ServiceProviderApplication { public static void main(String[] args) { SpringApplication.run(ServiceProviderApplication.class, args); } }
DateServiceController
提供一個Restful接口而已,該接口的做用是獲取服務器上的時間並返回
@RestController public class DateServiceController { @RequestMapping( value = "/test", method = RequestMethod.GET ) public String test( @RequestParam String param ){ return "hello " + param; } }
spring.application.name=service_provider server.port=1112 eureka.client.serviceUrl.defaultZone=http://localhost:1111/eureka/
瀏覽器訪問服務註冊中心,咱們發現服務提供者 service_provider
已經註冊到 eureka_server
上:
同時瀏覽器訪問:http://localhost:1112/test?param=www.codesheep.cn
,能夠測試服務提供 service_provider
提供的接口工做正常
接下來咱們建立服務消費者,是 Feign該登場的時候了!
service_consumer
的 SpringBoot工程,並在pom.xml中添加好對應依賴<dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-eureka</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-feign</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies>
主要是添加有關 Feign客戶端的一些註解而已
@EnableFeignClients @EnableDiscoveryClient @SpringBootApplication public class ServiceConsumerApplication { public static void main(String[] args) { SpringApplication.run(ServiceConsumerApplication.class, args); } }
DateServiceFeignClientInterface
很明顯其內部用 @FeignClient( value = "service-provider" )
聲明的方式指向了 服務提供者,而接口方法則實現了對 服務提供者接口的實際調用
@FeignClient( value = "service-provider" ) public interface DateServiceFeignClientInterface { @GetMapping("/test") String consumer( @RequestParam("param") String param ); }
DateServiceFeignController
注意,這是服務消費者提供的 Rest接口
@RestController @RequestMapping("/consumer") public class DateServiceFeignController { @Resource DateServiceFeignClientInterface dateServiceFeignClientInterface; @GetMapping("/date") public String getDate( @RequestParam String param ) { return dateServiceFeignClientInterface.consumer( param ); } }
application.properties
spring.application.name=service-consumer server.port=1113 eureka.client.serviceUrl.defaultZone=http://localhost:1111/eureka/
咱們先去服務註冊中心上看看,發現 服務消費者也註冊上來了:
而後咱們瀏覽器訪問 服務消費者提供的Rest接口: http://localhost:1113/consumer/date?param=www.codesheep.cn
這樣咱們就經過 服務消費者的 Feign客戶端 取到了服務提供者 給予的接口數據。
上面這就是聲明式客戶端 Feign的第一種使用姿式,也是經常使用的手法,常見於不少Demo
下面咱們來實踐一下關於 Feign的繼承與實現機制,發現其使用更加靈活( Feign支持接口繼承方式快速生成客戶端,很有點RPC的意思(關於RPC的實踐能夠參考個人文章:《RPC框架實踐之:Google gRPC》、《RPC框架實踐之:Apache Thrift》) )
建立一個普通 Maven項目:service_provider_api
建立一個公共接口:DateService
public interface DateService { @GetMapping("/api/test") String consumer( @RequestParam("param") String param ); }
service_consumer
項目中添加一個新的Feign的客戶端接口@FeignClient( value = "service-provider" ) public interface DateServiceFeignClientInterface2 extends DateService { }
service_consumer
項目中添加一個新的控制器 DateServiceFeignController2
@RestController @RequestMapping("/consumer2") public class DateServiceFeignController2 { @Resource DateServiceFeignClientInterface2 dateServiceFeignClientInterface2; @GetMapping("/date") public String getDate( @RequestParam String param ) { return dateServiceFeignClientInterface2.consumer( param ); } }
service_provider
項目中來實現咱們在公共api項目 service_provider_api
中的 DateService
接口,賦予實際邏輯@RestController public class DateServiceController2 implements DateService { @Override public String consumer( @RequestParam String param) { Date now = new Date(); SimpleDateFormat simpleDateFormat = new SimpleDateFormat("今天是"+"yyyy年MM月dd日 E kk點mm分" ); String nowTime = simpleDateFormat.format( now ); return "hello again " + param + ", " + nowTime; } }
eureka_server
、service_provider
、service_consumer
三個項目分別啓動瀏覽器訪問:http://localhost:1113/consumer2/date?param=www.codesheep.cn
使用 feign的繼承特性時,能夠將服務接口的定義從服務消費者中剝離出去,造成獨立的api項目從而能夠很方便的實現接口定義和依賴的共享,不用再複製粘貼接口進行綁定,固然這種作法存在的問題就是可能會致使服務提供者和服務消費者間的耦合度增高,此時若是服務提供者修改了一個接口定義,服務消費者可能也得跟着變,進而帶來一些坑。
因爲能力有限,如有錯誤或者不當之處,還請你們批評指正,一塊兒學習交流!