通过之前发布的《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@SpringBootApplicationpublic 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-clientserver.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@SpringBootApplicationpublic 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-consumerserver.port=2102

eureka.client.serviceUrl.defaultZone=http://eureka.didispace.com/eureka/
  • 启动eureka-feign-consumer之后,我们可以通过访问:http://localhost:2102/test ,来实验eureka-feign-consumereureka-feign-client接口的调用。

本文示例

  • 码云
  • GitHub

程序清单:

  • eureka-feign-api:服务接口定义
  • eureka-feign-client:服务接口实现的提供方
  • eureka-feign-consumer:服务接口的调用方

欢迎使用公益Eureka调试您的Spring Cloud程序:http://eureka.didispace.com/

相关阅读

  • Spring Cloud构建微服务架构:服务注册与发现(Eureka、Consul)
  • Spring Cloud构建微服务架构:服务消费者(基础)
  • Spring Cloud构建微服务架构:服务消费者(Ribbon)
  • Spring Cloud构建微服务架构:服务消费者(Feign)
  • Spring Cloud构建微服务架构:分布式配置中心
  • Spring Cloud构建微服务架构:服务容错保护(hystrix服务降级)
  • Spring Cloud构建微服务架构:服务容错保护(hystrix依赖隔离)
  • Spring Cloud构建微服务架构:服务容错保护(hystrix断路器)
  • Spring Cloud构建微服务架构:Hystrix监控面板
  • Spring Cloud构建微服务架构:Hystrix监控数据聚合
  • 更多Spring Cloud内容…

Spring Cloud实战小贴士:Feign的继承特性(伪RPC模式)相关推荐

  1. Spring Cloud实战小贴士:Zuul统一异常处理(三)【Dalston版】

    本篇作为<Spring Cloud微服务实战>一书关于Spring Cloud Zuul网关在Dalston版本对异常处理的补充.没有看过本书的读书也不要紧,可以先阅读我之前的两篇博文:& ...

  2. Spring Cloud实战小贴士:Zuul统一异常处理(二)

    在前几天发布的<Spring Cloud实战小贴士:Zuul统一异常处理(一)>一文中,我们详细说明了当Zuul的过滤器中抛出异常时会发生客户端没有返回任何内容的问题以及针对这个问题的两种 ...

  3. Spring Cloud实战小贴士:Zuul处理Cookie和重定向

    由于我们在之前所有的入门教程中,对于HTTP请求都采用了简单的接口实现.而实际使用过程中,我们的HTTP请求要复杂的多,比如当我们将Spring Cloud Zuul作为API网关接入网站类应用时,往 ...

  4. Spring Cloud实战小贴士:Zuul统一异常处理(一)

    在上一篇<Spring Cloud源码分析(四)Zuul:核心过滤器>一文中,我们详细介绍了Spring Cloud Zuul中自己实现的一些核心过滤器,以及这些过滤器在请求生命周期中的不 ...

  5. Spring Cloud实战小贴士:版本依赖关系

    去年在博客上连载了<Spring Cloud构建微服务架构>的系列博文,虽然这部分内容得到了不少关注者们的支持,但是不得不说这些内容只是适用于Spring Cloud入门阶段对各个组件的初 ...

  6. Spring Cloud实战小贴士:Ribbon的饥饿加载(eager-load)模式

    我们在使用Spring Cloud的Ribbon或Feign来实现服务调用的时候,如果我们的机器或网络环境等原因不是很好的话,有时候会发现这样一个问题:我们服务消费方调用服务提供方接口的时候,第一次请 ...

  7. Spring Cloud实战小贴士:turbine如何聚合设置了context-path的hystrix数据

    之前在spring for all社区看到这样一个问题:当actuator端点设置了context-path之后,turbine如何聚合数据?首先,我们要知道actuator端点设置了context- ...

  8. Spring Cloud实战小贴士:随机端口

    太久没有更新,一时不知道该从哪儿开始,索性就从一个小技巧开始吧. 在之前的<Spring Cloud构建微服务架构>系列博文中,我们经常会需要启动多个实例的情况来测试注册中心.配置中心等基 ...

  9. Spring Cloud实战小贴士:Zuul的饥饿加载(eager-load)使用

    上一篇 我们介绍了如何使用Ribbon的 earger-load配置加速Spring Cloud中对服务接口的第一次调用.可是这样只是解决了内部服务间的调用,另外一个问题依然经常困扰我们,那就是网关到 ...

最新文章

  1. C#访问网络共享文件夹,带用户名密码域,解决电脑重启后访问不到网络文件夹
  2. java web开发中Filter使用Annotation配置 (转载)
  3. Redis--发布订阅模式
  4. 数据库编程连接mysql_使用JDBC编程-连接MySQL数据库
  5. 关于文本框字数的限制以及动态显示剩余字数
  6. 揭秘360手机助手未经用户同意,自动开启辅助的“黑科技”
  7. c语言如何将十六进制转化为二进制数,C语言--将十进制整数转化为二进制与十六进制输出...
  8. java数据结构与算法总结(二十五)--初识BitSet之API
  9. c语言两矩阵相乘算法,用c语言实现两个矩阵相乘怎么做?
  10. 广东英语高考怎么计算机,如何应对广东高考英语听说考试
  11. 小巧 mvc servlet 通过反射 跳转 bussiness层
  12. 【第3章】局域网概述
  13. 【NDN实验】ndnSIM: NDN simulator for NS-3 全文翻译
  14. 年薪40W毕业生大厂面试题合集,学完之后你不会拿不到offer
  15. c语言用字符输出李字,二级C语言历年真题汇总__第10篇对文件的输入输出-李赛红.doc...
  16. react函数组件实现四栏轮播图切换
  17. 【数据挖掘导论】对于决策树要掌握的几个问题
  18. Redis之布隆过滤器(BloomFilter)
  19. inherit和initial:两个特殊的css值
  20. 【Node】Error: ENOENT: no such file or directory,解决方案

热门文章

  1. MsSql正反表达式
  2. 2011年影响3G手机发展四大因素
  3. js的oop方式和this指针问题
  4. 在STM32单片机上跑神经网络算法
  5. 操作系统中抢占式和非抢占式内核的区别
  6. 单片机(MCU)如何才能不死机之对齐访问(Aligned Access)
  7. Linux利器:QEMU!用它模拟开发板能替代真开发板?
  8. LinuxC高级编程——线程间同步
  9. 一、Web服务器——Tomcat Servlet学习笔记
  10. python网络爬虫系列(七)——selenium的介绍 selenium定位获取标签对象并提取数据 selenium的其它使用方法