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-19 15:13:05

过滤器Spring ClouZuul

2017-05-18 14:14:25

过滤器Spring ClouZuul

2017-05-02 23:05:44

HTTPZuulCookie

2017-07-31 15:47:50

Zuul统一处理

2017-10-20 14:55:06

Spring ClouZuul加载

2017-04-13 11:06:28

SpringCloud随机端口

2017-10-18 16:00:14

SpringCloudZuul路径

2017-08-10 11:15:05

Spring Clou微服务架构

2021-10-22 09:00:59

令牌JWT

2021-11-04 10:11:02

Sentinel网关限流

2021-11-16 11:45:00

SpringSpring ClouJava

2017-12-01 08:54:18

SpringCloudHystrix

2022-01-07 07:29:08

Rbac权限模型

2009-06-18 15:40:07

Spring Batc

2010-06-04 15:44:06

Hadoop伪分布

2017-09-20 09:46:38

Spring BootSpring Clou内存

2018-06-01 23:08:01

Spring Clou微服务服务器

2021-06-04 08:48:46

Spring ClouMaven Centr版本

2022-08-11 09:17:38

架构开发
点赞
收藏

51CTO技术栈公众号