Spring Cloud實戰小貼士:Feign的繼承特性(偽RPC模式)
通過之前發布的《Spring Cloud構建微服務架構:服務消費者(Feign)》,我們已經學會如何使用Spring MVC的注解來綁定服務接口。我們幾乎完全可以從服務提供方的Controller中依靠復制操作,來構建出相應的服務接口客戶端,或是通過Swagger生成的API文檔來編寫出客戶端,亦或是通過Swagger的代碼生成器來生成客戶端綁定。即便如此,有很多的方式來產生Feign的客戶端程序,依然有很多開發者熱衷于利用公共的依賴接口來連接服務提供者和服務消費者的方式。由此,Feign的繼承特性就能很好的派上用處。下面,我們來詳細看看如何使用Spring Cloud Feign的繼承特性。
動手試一試
接下來的示例將分為三個模塊:
- 服務接口定義模塊:通過Spring MVC注解定義抽象的interface服務接口
- 服務接口實現模塊:實現服務接口定義模塊的interface,該模塊作為服務提供者注冊到eureka
- 服務接口消費模塊:服務接口定義模塊的客戶端實現,該模塊通過注冊到eureka來消費服務接口
服務接口的定義
- 創建一個Spring Boot項目:eureka-feign-api,pom.xml的主要內容如下:
- <parent>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter-parent</artifactId>
- <version>1.5.6.RELEASE</version>
- <relativePath/>
- </parent>
- <dependencies>
- <dependency>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter-web</artifactId>
- </dependency>
- </dependencies>
- <dependencyManagement>
- <dependencies>
- <dependency>
- <groupId>org.springframework.cloud</groupId>
- <artifactId>spring-cloud-dependencies</artifactId>
- <version>Dalston.SR2</version>
- <type>pom</type>
- <scope>import</scope>
- </dependency>
- </dependencies>
- </dependencyManagement>
- 使用Spring MVC注解來定義服務接口:
- public interface HelloService {
- @GetMapping("/hello")
- String hello(@RequestParam(value = "name") String name);
- }
- 完成了上述構建之后,我們使用mvn install將該模塊構建到本地的Maven倉庫中。
服務接口的實現
- 創建一個Spring Boot項目:eureka-feign-client,pom.xml的主要內容如下:
- <parent>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter-parent</artifactId>
- <version>1.5.6.RELEASE</version>
- <relativePath/>
- </parent>
- <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>com.didispace</groupId>
- <artifactId>eureka-feign-api</artifactId>
- <version>1.0.0</version>
- </dependency>
- </dependencies>
- <dependencyManagement>
- <dependencies>
- <dependency>
- <groupId>org.springframework.cloud</groupId>
- <artifactId>spring-cloud-dependencies</artifactId>
- <version>Dalston.SR2</version>
- <type>pom</type>
- <scope>import</scope>
- </dependency>
- </dependencies>
- </dependencyManagement>
該模塊需要依賴上面定義的eureka-feign-api,將使用上述定義的HelloService接口來實現對應的REST服務。同時依賴Eureka是為了將該服務注冊到Eureka上供服務消費者發現。
- 創建應用主類。使用@EnableDiscoveryClient注解開啟服務注冊與發現,并實現HelloService接口的REST服務:
- @EnableDiscoveryClient
- @SpringBootApplication
- public class Application {
- @RestController
- class HelloController implements HelloService {
- @Override
- public String hello(String name) {
- return "hello " + name;
- }
- }
- public static void main(String[] args) {
- new SpringApplicationBuilder(Application.class).web(true).run(args);
- }
- }
- 編輯application.properties配置內容:
- spring.application.name=eureka-feign-client
- server.port=2101
- eureka.client.serviceUrl.defaultZone=http://eureka.didispace.com/eureka/
配置了服務提供者的名稱eureka-feign-client,服務提供者的端口號2101,并將該服務注冊到我的公益Eureka注冊中心上。啟動該項目,我們可以通過訪問:http://eureka.didispace.com/ ,在該頁面中找到它。
服務接口的消費
- 創建一個Spring Boot項目:eureka-feign-consumer,pom.xml的主要內容如下:
- <parent>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter-parent</artifactId>
- <version>1.5.6.RELEASE</version>
- <relativePath/>
- </parent>
- <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>com.didispace</groupId>
- <artifactId>eureka-feign-api</artifactId>
- <version>1.0.0</version>
- </dependency>
- </dependencies>
- <dependencyManagement>
- <dependencies>
- <dependency>
- <groupId>org.springframework.cloud</groupId>
- <artifactId>spring-cloud-dependencies</artifactId>
- <version>Dalston.SR2</version>
- <type>pom</type>
- <scope>import</scope>
- </dependency>
- </dependencies>
- </dependencyManagement>
該模塊較服務提供者的依賴增加了Feign的依賴,因為這里將使用Feign來綁定服務接口的客戶端。下面我們將使用Feign的繼承特性來輕松的構建Feign客戶端。
- 創建應用主類。使用@EnableDiscoveryClient注解開啟服務注冊與發現,并通過@FeignClient注解來聲明服務綁定客戶端:
- @EnableFeignClients
- @EnableDiscoveryClient
- @SpringBootApplication
- public class Application {
- @FeignClient("eureka-feign-client")
- interface HelloServiceClient extends HelloService {
- }
- @RestController
- class TestController {
- @Autowired
- private HelloServiceClient helloServiceClient;
- @GetMapping("/test")
- public String test(String name) {
- return helloServiceClient.hello(name);
- }
- }
- public static void main(String[] args) {
- new SpringApplicationBuilder(Application.class).web(true).run(args);
- }
- }
從上述代碼中我們可以看到,利用Feign的繼承特性,@FeignClient注解只需要通過聲明一個接口來繼承在API模塊中定義的公共interface就能產生服務接口的Feign客戶端了。而@FeignClient中的值需要填寫該服務的具體服務名(服務提供者的spring.application.name配置值)。
- 編輯服務消費者的application.properties配置內容,將服務消費者注冊到eureka上來消費服務:
- spring.application.name=eureka-feign-consumer
- server.port=2102
- eureka.client.serviceUrl.defaultZone=http://eureka.didispace.com/eureka/
- 啟動eureka-feign-consumer之后,我們可以通過訪問:http://localhost:2102/test ,來實驗eureka-feign-consumer對eureka-feign-client接口的調用。
本文示例
程序清單:
- eureka-feign-api:服務接口定義
- eureka-feign-client:服務接口實現的提供方
- eureka-feign-consumer:服務接口的調用方
【本文為51CTO專欄作者“翟永超”的原創稿件,轉載請通過51CTO聯系作者獲取授權】