Spring Cloud Feign的兩種使用姿勢
Spring Cloud中宣告式客戶端 Feign的使用過程記錄
概述
最近結合一些別人的開源專案來學習Spring Cloud,其中關於服務消費這方面的一個很便利的工具 Feign讓我記憶頗深。雖然網上的Demo和例子不勝列舉,但大多比較分散,本文就來集中記錄一下宣告式客戶端 Feign的一些使用姿勢。
注:本文首發於 ofollow,noindex" target="_blank"> 部落格 CodeSheep · 程式羊 ,歡迎光臨 小站 !
下文就結合例子來記錄這一過程,程式碼在文尾處。
建立基於 Eureka的服務註冊中心
三個步驟即可搞定:
- 建工程
建立一個名為 eureka_server
的 SpringBoot工程,並在pom.xml中新增好對應依賴
<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
- 啟動服務註冊中心
瀏覽器訪問之:
此時還沒有任何服務註冊上來。
建立服務提供者
- 建工程
建立一個名為 service_provider
的 SpringBoot工程,並在pom.xml中新增好對應依賴:
<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; } }
- 配置 application.properties檔案
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該登場的時候了!
建立基於 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); } }
- 建立一個 Feign客戶端的介面:
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的實踐可以參考我的文章: gRPC/" rel="nofollow,noindex" target="_blank">《RPC框架實踐之:Google gRPC》 、 Thrift/" rel="nofollow,noindex" target="_blank">《RPC框架實踐之:Apache Thrift》 ) )
抽象出一個公共的 API服務
-
建立一個普通 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專案從而可以很方便的實現介面定義和依賴的共享,不用再複製貼上介面進行繫結,當然這種做法存在的問題就是可能會導致服務提供者和服務消費者間的耦合度增高,此時如果服務提供者修改了一個介面定義,服務消費者可能也得跟著變,進而帶來一些坑。
後 記
由於能力有限,若有錯誤或者不當之處,還請大家批評指正,一起學習交流!
- My Personal Blog:CodeSheep 程式羊
- 我的半年技術部落格之路
可 長按 或 掃描 下面的 小心心 來訂閱 CodeSheep ,獲取更多 務實、能看懂、可復現的 原創文 ↓↓↓