自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

Spring Cloud實(shí)戰(zhàn)小貼士:Feign的繼承特性(偽RPC模式)

企業(yè)動(dòng)態(tài)
我們幾乎完全可以從服務(wù)提供方的Controller中依靠復(fù)制操作,來(lái)構(gòu)建出相應(yīng)的服務(wù)接口客戶端,或是通過(guò)Swagger生成的API文檔來(lái)編寫(xiě)出客戶端,亦或是通過(guò)Swagger的代碼生成器來(lái)生成客戶端綁定。

[[199690]]

通過(guò)之前發(fā)布的《Spring Cloud構(gòu)建微服務(wù)架構(gòu):服務(wù)消費(fèi)者(Feign)》,我們已經(jīng)學(xué)會(huì)如何使用Spring MVC的注解來(lái)綁定服務(wù)接口。我們幾乎完全可以從服務(wù)提供方的Controller中依靠復(fù)制操作,來(lái)構(gòu)建出相應(yīng)的服務(wù)接口客戶端,或是通過(guò)Swagger生成的API文檔來(lái)編寫(xiě)出客戶端,亦或是通過(guò)Swagger的代碼生成器來(lái)生成客戶端綁定。即便如此,有很多的方式來(lái)產(chǎn)生Feign的客戶端程序,依然有很多開(kāi)發(fā)者熱衷于利用公共的依賴接口來(lái)連接服務(wù)提供者和服務(wù)消費(fèi)者的方式。由此,F(xiàn)eign的繼承特性就能很好的派上用處。下面,我們來(lái)詳細(xì)看看如何使用Spring Cloud Feign的繼承特性。

動(dòng)手試一試

接下來(lái)的示例將分為三個(gè)模塊:

  • 服務(wù)接口定義模塊:通過(guò)Spring MVC注解定義抽象的interface服務(wù)接口
  • 服務(wù)接口實(shí)現(xiàn)模塊:實(shí)現(xiàn)服務(wù)接口定義模塊的interface,該模塊作為服務(wù)提供者注冊(cè)到eureka
  • 服務(wù)接口消費(fèi)模塊:服務(wù)接口定義模塊的客戶端實(shí)現(xiàn),該模塊通過(guò)注冊(cè)到eureka來(lái)消費(fèi)服務(wù)接口

服務(wù)接口的定義

  • 創(chuàng)建一個(gè)Spring Boot項(xiàng)目:eureka-feign-api,pom.xml的主要內(nèi)容如下:
  1. <parent> 
  2.     <groupId>org.springframework.boot</groupId> 
  3.     <artifactId>spring-boot-starter-parent</artifactId> 
  4.     <version>1.5.6.RELEASE</version> 
  5.     <relativePath/> 
  6. </parent> 
  7. <dependencies> 
  8.     <dependency> 
  9.         <groupId>org.springframework.boot</groupId> 
  10.         <artifactId>spring-boot-starter-web</artifactId> 
  11.     </dependency> 
  12. </dependencies> 
  13. <dependencyManagement> 
  14.     <dependencies> 
  15.         <dependency> 
  16.             <groupId>org.springframework.cloud</groupId> 
  17.             <artifactId>spring-cloud-dependencies</artifactId> 
  18.             <version>Dalston.SR2</version> 
  19.             <type>pom</type> 
  20.             <scope>import</scope> 
  21.         </dependency> 
  22.     </dependencies> 
  23. </dependencyManagement> 
  • 使用Spring MVC注解來(lái)定義服務(wù)接口:
  1. public interface HelloService { 
  2.     @GetMapping("/hello"
  3.     String hello(@RequestParam(value = "name") String name); 
  • 完成了上述構(gòu)建之后,我們使用mvn install將該模塊構(gòu)建到本地的Maven倉(cāng)庫(kù)中。

服務(wù)接口的實(shí)現(xiàn)

  • 創(chuàng)建一個(gè)Spring Boot項(xiàng)目:eureka-feign-client,pom.xml的主要內(nèi)容如下:
  1. <parent> 
  2.     <groupId>org.springframework.boot</groupId> 
  3.     <artifactId>spring-boot-starter-parent</artifactId> 
  4.     <version>1.5.6.RELEASE</version> 
  5.     <relativePath/> 
  6. </parent> 
  7. <dependencies> 
  8.     <dependency> 
  9.         <groupId>org.springframework.boot</groupId> 
  10.         <artifactId>spring-boot-starter-web</artifactId> 
  11.     </dependency> 
  12.     <dependency> 
  13.         <groupId>org.springframework.cloud</groupId> 
  14.         <artifactId>spring-cloud-starter-eureka</artifactId> 
  15.     </dependency> 
  16.     <dependency> 
  17.         <groupId>com.didispace</groupId> 
  18.         <artifactId>eureka-feign-api</artifactId> 
  19.         <version>1.0.0</version> 
  20.     </dependency> 
  21. </dependencies> 
  22. <dependencyManagement> 
  23.     <dependencies> 
  24.         <dependency> 
  25.             <groupId>org.springframework.cloud</groupId> 
  26.             <artifactId>spring-cloud-dependencies</artifactId> 
  27.             <version>Dalston.SR2</version> 
  28.             <type>pom</type> 
  29.             <scope>import</scope> 
  30.         </dependency> 
  31.     </dependencies> 
  32. </dependencyManagement> 

該模塊需要依賴上面定義的eureka-feign-api,將使用上述定義的HelloService接口來(lái)實(shí)現(xiàn)對(duì)應(yīng)的REST服務(wù)。同時(shí)依賴Eureka是為了將該服務(wù)注冊(cè)到Eureka上供服務(wù)消費(fèi)者發(fā)現(xiàn)。

  • 創(chuàng)建應(yīng)用主類。使用@EnableDiscoveryClient注解開(kāi)啟服務(wù)注冊(cè)與發(fā)現(xiàn),并實(shí)現(xiàn)HelloService接口的REST服務(wù):
  1. @EnableDiscoveryClient 
  2. @SpringBootApplication 
  3. public class Application { 
  4.     @RestController 
  5.     class HelloController implements HelloService { 
  6.         @Override 
  7.         public String hello(String name) { 
  8.             return "hello " + name
  9.         } 
  10.     } 
  11.     public static void main(String[] args) { 
  12.         new SpringApplicationBuilder(Application.class).web(true).run(args); 
  13.     } 
  • 編輯application.properties配置內(nèi)容:
  1. spring.application.name=eureka-feign-client 
  2. server.port=2101 
  3. eureka.client.serviceUrl.defaultZone=http://eureka.didispace.com/eureka/ 

配置了服務(wù)提供者的名稱eureka-feign-client,服務(wù)提供者的端口號(hào)2101,并將該服務(wù)注冊(cè)到我的公益Eureka注冊(cè)中心上。啟動(dòng)該項(xiàng)目,我們可以通過(guò)訪問(wèn):http://eureka.didispace.com/ ,在該頁(yè)面中找到它。

服務(wù)接口的消費(fèi)

  • 創(chuàng)建一個(gè)Spring Boot項(xiàng)目:eureka-feign-consumer,pom.xml的主要內(nèi)容如下:
  1. <parent> 
  2.     <groupId>org.springframework.boot</groupId> 
  3.     <artifactId>spring-boot-starter-parent</artifactId> 
  4.     <version>1.5.6.RELEASE</version> 
  5.     <relativePath/> 
  6. </parent> 
  7. <dependencies> 
  8.     <dependency> 
  9.         <groupId>org.springframework.boot</groupId> 
  10.         <artifactId>spring-boot-starter-web</artifactId> 
  11.     </dependency> 
  12.     <dependency> 
  13.         <groupId>org.springframework.cloud</groupId> 
  14.         <artifactId>spring-cloud-starter-eureka</artifactId> 
  15.     </dependency> 
  16.     <dependency> 
  17.         <groupId>org.springframework.cloud</groupId> 
  18.         <artifactId>spring-cloud-starter-feign</artifactId> 
  19.     </dependency> 
  20.     <dependency> 
  21.         <groupId>com.didispace</groupId> 
  22.         <artifactId>eureka-feign-api</artifactId> 
  23.         <version>1.0.0</version> 
  24.     </dependency> 
  25. </dependencies> 
  26. <dependencyManagement> 
  27.     <dependencies> 
  28.         <dependency> 
  29.             <groupId>org.springframework.cloud</groupId> 
  30.             <artifactId>spring-cloud-dependencies</artifactId> 
  31.             <version>Dalston.SR2</version> 
  32.             <type>pom</type> 
  33.             <scope>import</scope> 
  34.         </dependency> 
  35.     </dependencies> 
  36. </dependencyManagement> 

該模塊較服務(wù)提供者的依賴增加了Feign的依賴,因?yàn)檫@里將使用Feign來(lái)綁定服務(wù)接口的客戶端。下面我們將使用Feign的繼承特性來(lái)輕松的構(gòu)建Feign客戶端。

  • 創(chuàng)建應(yīng)用主類。使用@EnableDiscoveryClient注解開(kāi)啟服務(wù)注冊(cè)與發(fā)現(xiàn),并通過(guò)@FeignClient注解來(lái)聲明服務(wù)綁定客戶端:
  1. @EnableFeignClients 
  2. @EnableDiscoveryClient 
  3. @SpringBootApplication 
  4. public class Application { 
  5.     @FeignClient("eureka-feign-client"
  6.     interface HelloServiceClient extends HelloService { 
  7.     } 
  8.     @RestController 
  9.     class TestController { 
  10.         @Autowired 
  11.         private HelloServiceClient helloServiceClient; 
  12.         @GetMapping("/test"
  13.         public String test(String name) { 
  14.             return helloServiceClient.hello(name); 
  15.         } 
  16.     } 
  17.     public static void main(String[] args) { 
  18.         new SpringApplicationBuilder(Application.class).web(true).run(args); 
  19.     } 

從上述代碼中我們可以看到,利用Feign的繼承特性,@FeignClient注解只需要通過(guò)聲明一個(gè)接口來(lái)繼承在API模塊中定義的公共interface就能產(chǎn)生服務(wù)接口的Feign客戶端了。而@FeignClient中的值需要填寫(xiě)該服務(wù)的具體服務(wù)名(服務(wù)提供者的spring.application.name配置值)。

  • 編輯服務(wù)消費(fèi)者的application.properties配置內(nèi)容,將服務(wù)消費(fèi)者注冊(cè)到eureka上來(lái)消費(fèi)服務(wù):
  1. spring.application.name=eureka-feign-consumer 
  2. server.port=2102 
  3. eureka.client.serviceUrl.defaultZone=http://eureka.didispace.com/eureka/ 
  • 啟動(dòng)eureka-feign-consumer之后,我們可以通過(guò)訪問(wèn):http://localhost:2102/test ,來(lái)實(shí)驗(yàn)eureka-feign-consumer對(duì)eureka-feign-client接口的調(diào)用。

本文示例

碼云

GitHub

程序清單:

  • eureka-feign-api:服務(wù)接口定義
  • eureka-feign-client:服務(wù)接口實(shí)現(xiàn)的提供方
  • eureka-feign-consumer:服務(wù)接口的調(diào)用方

【本文為51CTO專欄作者“翟永超”的原創(chuàng)稿件,轉(zhuǎn)載請(qǐng)通過(guò)51CTO聯(lián)系作者獲取授權(quán)】

戳這里,看該作者更多好文

責(zé)任編輯:武曉燕 來(lái)源: 51CTO專欄
相關(guān)推薦

2017-09-26 16:17:39

Ribboneager-load模式

2017-05-19 15:13:05

過(guò)濾器Spring ClouZuul

2017-05-18 14:14:25

過(guò)濾器Spring ClouZuul

2017-07-31 15:47:50

Zuul統(tǒng)一處理

2017-05-02 23:05:44

HTTPZuulCookie

2017-10-20 14:55:06

Spring ClouZuul加載

2025-03-07 08:57:46

HTTP客戶端框架

2017-04-13 11:06:28

SpringCloud隨機(jī)端口

2017-10-18 16:00:14

SpringCloudZuul路徑

2017-08-10 11:15:05

Spring Clou微服務(wù)架構(gòu)

2025-02-10 00:23:11

Spring微服務(wù)架構(gòu)

2021-10-22 09:00:59

令牌JWT

2021-11-04 10:11:02

Sentinel網(wǎng)關(guān)限流

2025-03-04 02:20:00

EurekaNetflixSpring

2021-11-16 11:45:00

SpringSpring ClouJava

2017-12-01 08:54:18

SpringCloudHystrix

2022-01-07 07:29:08

Rbac權(quán)限模型

2009-06-18 15:40:07

Spring Batc

2025-02-28 09:40:21

SidecarSCA服務(wù)

2010-06-04 15:44:06

Hadoop偽分布
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)