成人免费xxxxx在线视频软件_久久精品久久久_亚洲国产精品久久久_天天色天天色_亚洲人成一区_欧美一级欧美三级在线观看

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

企業動態
我們幾乎完全可以從服務提供方的Controller中依靠復制操作,來構建出相應的服務接口客戶端,或是通過Swagger生成的API文檔來編寫出客戶端,亦或是通過Swagger的代碼生成器來生成客戶端綁定。

[[199690]]

通過之前發布的《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的主要內容如下:
  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注解來定義服務接口:
  1. public interface HelloService { 
  2.     @GetMapping("/hello"
  3.     String hello(@RequestParam(value = "name") String name); 
  • 完成了上述構建之后,我們使用mvn install將該模塊構建到本地的Maven倉庫中。

服務接口的實現

  • 創建一個Spring Boot項目:eureka-feign-client,pom.xml的主要內容如下:
  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接口來實現對應的REST服務。同時依賴Eureka是為了將該服務注冊到Eureka上供服務消費者發現。

  • 創建應用主類。使用@EnableDiscoveryClient注解開啟服務注冊與發現,并實現HelloService接口的REST服務:
  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配置內容:
  1. spring.application.name=eureka-feign-client 
  2. server.port=2101 
  3. 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的主要內容如下:
  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> 

該模塊較服務提供者的依賴增加了Feign的依賴,因為這里將使用Feign來綁定服務接口的客戶端。下面我們將使用Feign的繼承特性來輕松的構建Feign客戶端。

  • 創建應用主類。使用@EnableDiscoveryClient注解開啟服務注冊與發現,并通過@FeignClient注解來聲明服務綁定客戶端:
  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注解只需要通過聲明一個接口來繼承在API模塊中定義的公共interface就能產生服務接口的Feign客戶端了。而@FeignClient中的值需要填寫該服務的具體服務名(服務提供者的spring.application.name配置值)。

  • 編輯服務消費者的application.properties配置內容,將服務消費者注冊到eureka上來消費服務:
  1. spring.application.name=eureka-feign-consumer 
  2. server.port=2102 
  3. eureka.client.serviceUrl.defaultZone=http://eureka.didispace.com/eureka/ 
  • 啟動eureka-feign-consumer之后,我們可以通過訪問:http://localhost:2102/test ,來實驗eureka-feign-consumer對eureka-feign-client接口的調用。

本文示例

碼云

GitHub

程序清單:

  • eureka-feign-api:服務接口定義
  • eureka-feign-client:服務接口實現的提供方
  • eureka-feign-consumer:服務接口的調用方

【本文為51CTO專欄作者“翟永超”的原創稿件,轉載請通過51CTO聯系作者獲取授權】

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

責任編輯:武曉燕 來源: 51CTO專欄
相關推薦

2017-09-26 16:17:39

Ribboneager-load模式

2017-05-18 14:14:25

過濾器Spring ClouZuul

2017-05-19 15:13:05

過濾器Spring ClouZuul

2017-05-02 23:05:44

HTTPZuulCookie

2017-07-31 15:47:50

Zuul統一處理

2017-10-20 14:55:06

Spring ClouZuul加載

2025-03-07 08:57:46

HTTP客戶端框架

2017-04-13 11:06:28

SpringCloud隨機端口

2017-10-18 16:00:14

SpringCloudZuul路徑

2025-05-29 01:22:00

FeignJSONRPC

2017-08-10 11:15:05

Spring Clou微服務架構

2025-02-10 00:23:11

Spring微服務架構

2021-10-22 09:00:59

令牌JWT

2021-11-04 10:11:02

Sentinel網關限流

2025-03-04 02:20:00

EurekaNetflixSpring

2021-11-16 11:45:00

SpringSpring ClouJava

2017-12-01 08:54:18

SpringCloudHystrix

2009-06-18 15:40:07

Spring Batc

2022-01-07 07:29:08

Rbac權限模型

2025-02-28 09:40:21

SidecarSCA服務
點贊
收藏

51CTO技術棧公眾號

主站蜘蛛池模板: 97精品国产97久久久久久免费 | 91免费在线| 日韩久久综合 | 日本欧美国产在线观看 | 中文字幕乱码一区二区三区 | 一区二区三区在线播放 | 国产乱码精品一区二区三区五月婷 | 亚洲少妇综合网 | 一级毛片,一级毛片 | 99久久99 | 久久久999精品 | 亚洲成人毛片 | 国产yw851.c免费观看网站 | 中国美女av | 久久精品一区 | 国产亚洲精品精品国产亚洲综合 | 久久激情五月丁香伊人 | 国产97碰免费视频 | 成人精品视频在线 | 国产精品中文 | 午夜视频在线观看网址 | av中文在线 | 国产一区二区 | 日韩欧美第一页 | 亚洲精品乱码久久久久久按摩 | 日本一区二区三区免费观看 | 国产精品1区| 国产在线一区二区 | 久久精品黄色 | 国产视频久久 | 中文字幕高清 | 精品国产欧美日韩不卡在线观看 | 日韩电影a | 精品国产黄a∨片高清在线 成人区精品一区二区婷婷 日本一区二区视频 | 国产一卡二卡三卡 | 免费观看av | 日本中出视频 | 超级黄色一级片 | re久久 | 久久久久久久久久久一区二区 | 精品视频在线播放 |