【Dalston】【第三章】声明式服务调用(Feign)
当我们通过RestTemplate调用其它服务的API时,所需要的参数须在请求的URL中进行拼接,如果参数少的话或许我们还可以忍受,一旦有多个参数的话,这时拼接请求字符串就会效率低下,并且显得好傻。那么有没有更好的解决方案呢?答案是确定的有,Netflix已经为我们提供了一个框架:Feign。
Feign是一个声明式的Web Service客户端,它的目的就是让Web Service调用更加简单。Feign提供了HTTP请求的模板,通过编写简单的接口和插入注解,就可以定义好HTTP请求的参数、格式、地址等信息。而Feign则会完全代理HTTP请求,我们只需要像调用方法一样调用它就可以完成服务请求及相关处理。
Feign整合了Ribbon和Hystrix(关于Hystrix我们后面再讲),可以让我们不再需要显式地使用这两个组件。此外,Spring Cloud还对Feign提供了Spring MVC注解的支持,也使得我们在Web中可以使用同一个HttpMessageConverter
。
总起来说,Feign具有如下特性:
- 可插拔的注解支持,包括Feign注解和JAX-RS注解;
- 支持可插拔的HTTP编码器和解码器;
- 支持Hystrix和它的Fallback;
- 支持Ribbon的负载均衡;
- 支持HTTP请求和响应的压缩。
1. 示例代码
在我们编写代码之前先看看我们这次构建系统的系统框架是怎么的。

本示例将构建一个电子商城项目。其中Product-Service
提供商品服务,是一个Eureka Client,角色是一个服务提供者。Mall
则是一个Web项目,同样也是一个Eureka Client,作为服务消费者,同时也是本篇重点Feign Client。而Service-discover
保持不变。
标注:Product-Service在项目中起名为FeignClient,Service-discover为Eureka-Server
1.1 构建Product-Service
编写pom.xml文件
Product-Service
是一个标准的Eureka Client,所以pom.xml
和之前Service-Hello
一样,pom.xml:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>com.sunny</groupId> <artifactId>SpringCloud</artifactId> <version>0.0.1-SNAPSHOT</version> </parent> <artifactId>FeignClient</artifactId> <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> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
编写启动类
package com.product; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.client.discovery.EnableDiscoveryClient; @EnableDiscoveryClient @SpringBootApplication public class Application { public static void main(String[] args) { SpringApplication.run(Application.class, args); } }
这里和之前也没有什么不同
商品(Product)实体
package com.product.entity; import java.io.Serializable; public class Product implements Serializable{ private static final long serialVersionUID = 4668175645476846099L; /**产品货号*/ private String itemCode; /**产品名称*/ private String name; /**产品品牌名称*/ private String brandName; /**产品价格(分)*/ private int price; public Product() { } public Product(String itemCode, String name, String brandName, int price) { this.itemCode = itemCode; this.name = name; this.brandName = brandName; this.price = price; } public String getItemCode() { return itemCode; } public void setItemCode(String itemCode) { this.itemCode = itemCode; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getPrice() { return price; } public void setPrice(int price) { this.price = price; } public String getBrandName() { return brandName; } public void setBrandName(String brandName) { this.brandName = brandName; } }
编写具体商品服务
package com.product.controller; import java.util.ArrayList; import java.util.List; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RestController; import com.product.entity.Product; @RestController @RequestMapping("/products") public class ProductEndPoint { protected Logger logger = LoggerFactory.getLogger(ProductEndPoint.class); @RequestMapping(method = RequestMethod.GET) public List<Product> list() { logger.info("===do products ==="); return this.buildProducts(); } @RequestMapping(value = "/{itemCode}", method = RequestMethod.GET) public Product detail(@PathVariable String itemCode) { logger.info("===do detail ==="); List<Product> products = this.buildProducts(); for (Product product : products) { if (product.getItemCode().equalsIgnoreCase(itemCode)) return product; } return null; } /** * @Description 创建数据 * @return * @return List<Product> * @author SUNBIN * @date 2017年12月13日 */ protected List<Product> buildProducts() { List<Product> products = new ArrayList<>(); products.add(new Product("1", "测试商品-1", "xx品牌", 100)); products.add(new Product("2", "测试商品-2", "xx品牌", 200)); products.add(new Product("3", "测试商品-3", "xx品牌", 300)); products.add(new Product("4", "测试商品-4", "xx品牌", 400)); products.add(new Product("5", "测试商品-5", "xx品牌", 500)); products.add(new Product("6", "测试商品-6", "xx品牌", 600)); return products; } }
该服务提供下面两个接口:
- list: 获取商品列表;
- detail: 获取指定商品的详细数据。
编写配置文件
server.port=2100 spring.application.name=PRODUCT-SERVICE eureka.client.service-url.defaultZone=http://localhost:8260/eureka
启动测试
运行service
和client
后,在Eureka服务器控制台中我们可以看到下面的界面:

可以看到PRODUCT-SERVICE
已经注册成功。
1.2 构建Mall
编写pom.xml文件
我们的Mall项目需要引入对Feign的依赖,如下:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>com.sunny</groupId> <artifactId>SpringCloud</artifactId> <version>0.0.1-SNAPSHOT</version> </parent> <artifactId>FeignConsumer</artifactId> <dependencies> <dependency> <groupId>com.sunny</groupId> <artifactId>FeignClient</artifactId> <version>0.0.1-SNAPSHOT</version> </dependency> <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-ribbon</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-feign</artifactId> </dependency> </dependencies> </project>
编写启动类
package com.product; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.client.discovery.EnableDiscoveryClient; import org.springframework.cloud.netflix.feign.EnableFeignClients; @EnableFeignClients/*(basePackages = "com.product.**")*///开启Feign相关功能,打成jar包时必须指定包的路径 @EnableDiscoveryClient @SpringBootApplication public class Application { public static void main(String[] args) { SpringApplication.run(Application.class, args); } }
这里我们在启动类中增加了@EnableFeignClients
注解,用来开启Feign相关功能。
编写服务调用类
服务调用类是一个我们之前常写接口,由Spring Cloud根据该接口创建可供程序直接调用的代理类。为了完成代理,我们需要添加一些注解,其中@FeignClient中的name为商品服务所定义的PRODUCT-SERVICE
。而@RequestMapping是SpringMVC的注解,其对应商品服务中所提供的两个API接口:
package com.product.service; import java.util.List; import org.springframework.cloud.netflix.feign.FeignClient; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import com.product.entity.Product; @FeignClient("PRODUCT-SERVICE") public interface ProductService { @RequestMapping(value = "/products", method = RequestMethod.GET) List<Product> findAll(); @RequestMapping(value = "/products/{itemCode}", method = RequestMethod.GET) Product loadByItemCode(@PathVariable("itemCode") String itemCode); }
说明: 假如你的Service是单独编译一个jar包,那么在使用@EnableFeignClients
注解时需要指定basePackages
的值,否则,自动织入时就会报找不到ProductService
bean的错误.
编写Controller
package com.product.controller; import java.util.List; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RestController; import com.product.entity.Product; import com.product.service.ProductService; @RestController @RequestMapping("/products") public class ProductController { @Autowired private ProductService productService; @RequestMapping(method = RequestMethod.GET) public List<Product> list() { return this.productService.findAll(); } @RequestMapping(value = "/{itemCode}", method = RequestMethod.GET) public Product detail(@PathVariable String itemCode) { return this.productService.loadByItemCode(itemCode); } }
这里我们自动织入了之前的服务调用接口ProductService
。
编写配置文件
server.port=8800 spring.application.name=MALL-WEB eureka.client.service-url.defaultZone=http://localhost:8260/eureka
1.3 启动测试
启动后我们可以访问:http://localhost:8080/products,可以看到如下界面:

然后,我们在浏览器中继续输入http://localhost:8080/products/1,可以看到如下界面:

PRODUCT-SERVICE
所提供的服务。2. 参数绑定
Feign支持多种注解,在使用Feign时,我们可以根据需要使用Feign自带的注解或者JAX-RS注解。Spring Cloud对Feign进行了增强,使得Feign支持了Spring MVC注解。如上面的代码:
@RequestMapping(value = "/products", method = RequestMethod.GET) List<Product> findAll(); @RequestMapping(value = "/products/{itemCode}", method = RequestMethod.GET) Product loadByItemCode(@PathVariable("itemCode") String itemCode);
我们使用的都是Spring MVC的注解。在Spring MVC中我们常用的注解有:
- @RequestParam 绑定单个请求参数值;
- @PathVariable 绑定URI模板变量值;
- @RequestHeader 绑定请求头数据;
- @RequestBody 绑定请求的内容区数据并能进行自动类型转换等。
使用@RequestParam、@PathVariable和@RequestHeader样例如下:
@RequestMapping(value = "/products/detail", method = RequestMethod.GET) Product loadByItemCode(@RequestParam("itemCode") String itemCode); @RequestMapping(value = "/products/{itemCode}", method = RequestMethod.GET) Product loadByItemCode(@PathVariable("itemCode") String itemCode); @RequestMapping(value = "/products/detail", method = RequestMethod.GET) Product loadByItemCode(@RequestHeader("itemCode") String itemCode);
这几个样例都是同样的功能加载指定产品的详情。不过,这里需要注意的是不论@RequestParam、@PathVariable还是@RequestHeader它们的value
都是不可以少的,也就是说我们不能像Spring MVC Controller中这样声明:
@RequestMapping(value = "/products/{itemCode}", method = RequestMethod.GET) Product loadByItemCode(@PathVariable String itemCode);
因为,在Spring MVC中这些注解会根据参数名称来作为默认值,而Feign则不会,必须声明。
当然,我们也可以在参数绑定中使用多个或者混用,如下:
@RequestMapping(value = "/products/{itemCode}", method = RequestMethod.POST) Product changPrice(@PathVariable("itemCode") String itemCode, @RequestParam("price") int price);
2.1 传递对象
我们在定义接口的时候,在参数中往往会包含一个对象,比如我们的服务接口如下:
User register(User user);
这个接口提供一个用户注册功能,那么如何把复杂的用户信息传递过去呢?答案也非常简单,就是使用上面列出来的@RequestBody注解,使用方法如下:
@RequestMapping(value = "/users/register", method = RequestMethod.POST) User register(@RequestBody User user);
同样,在使用的时候需要注意User
对象必须要有默认构造函数,不然Feign会无法根据传递过来的JSON字符串转换为User
对象,从而抛出异常,造成调用不成功。
Spring MVC中注解还有一些如:@CookieValue、@SessionAttributes、@RequestPart等,笔者并没有一个个进行测试。也许你会使用这,到时不妨告诉笔者是否可以。
3. 继承
在读之前的代码时会发现,Client
工程中的ProductEndpoint
和Consumer
工程中ProductService
中的方法声明几乎都是一样的,那么是否可以定义一个共同的父类来消除代码的重复呢?
答案是肯定的,Feign是支持继承的。比如,我们在增加一个公用的工程:FeignApi
,然后将父类:ProductApiService
定义到该工程中,代码如下:
package com.product.api; import java.util.List; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import com.product.entity.Product; public interface ProductApiService { @RequestMapping(value = "/products", method = RequestMethod.GET) List<Product> list(); @RequestMapping(value = "/products/{itemCode}", method = RequestMethod.GET) Product detail (@PathVariable("itemCode") String itemCode); }
将Product实体类挪到Api工程下
然后Consumer和Client工程分别依赖Api工程,pom.xml中增加
<dependency> <groupId>com.sunny</groupId> <artifactId>FeignClient</artifactId> <version>0.0.1-SNAPSHOT</version> </dependency>
Client中的ProductEndPoint.java代码:
package com.product.controller; import java.util.ArrayList; import java.util.List; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RestController; import com.product.api.ProductApiService; import com.product.entity.Product; @RestController //@RequestMapping("/products") public class ProductEndPoint implements ProductApiService{ protected Logger logger = LoggerFactory.getLogger(ProductEndPoint.class); // @RequestMapping(method = RequestMethod.GET) public List<Product> list() { logger.info("===do products ==="); return this.buildProducts(); } // @RequestMapping(value = "/{itemCode}", method = RequestMethod.GET) public Product detail(@PathVariable String itemCode) { logger.info("===do detail ==="); List<Product> products = this.buildProducts(); for (Product product : products) { if (product.getItemCode().equalsIgnoreCase(itemCode)) return product; } return null; } /** * @Description 创建数据 * @return * @return List<Product> * @author SUNBIN * @date 2017年12月13日 */ protected List<Product> buildProducts() { List<Product> products = new ArrayList<>(); products.add(new Product("1", "测试商品-1", "xx品牌", 100)); products.add(new Product("2", "测试商品-2", "xx品牌", 200)); products.add(new Product("3", "测试商品-3", "xx品牌", 300)); products.add(new Product("4", "测试商品-4", "xx品牌", 400)); products.add(new Product("5", "测试商品-5", "xx品牌", 500)); products.add(new Product("6", "测试商品-6", "xx品牌", 600)); return products; } }
删除Client中的Product实体类
Consumer中的ProductService代码:
package com.product.service; import org.springframework.cloud.netflix.feign.FeignClient; import com.product.api.ProductApiService; @FeignClient("PRODUCT-SERVICE") public interface ProductService extends ProductApiService{ // @RequestMapping(value = "/products", method = RequestMethod.GET) // List<Product> findAll(); // // @RequestMapping(value = "/products/{itemCode}", method = RequestMethod.GET) // Product loadByItemCode(@PathVariable("itemCode") String itemCode); }
最后修改下Consumer中的Controller请求路径和方法名称
package com.product.controller; import java.util.List; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RestController; import com.product.entity.Product; import com.product.service.ProductService; @RestController @RequestMapping("/prod") public class ProductController { @Autowired private ProductService productService; @RequestMapping(method = RequestMethod.GET) public List<Product> list() { return this.productService.list(); } @RequestMapping(value = "/{itemCode}", method = RequestMethod.GET) public Product detail(@PathVariable String itemCode) { return this.productService.detail(itemCode); } }
完毕
最后通过页面请求prod路径测试,与之前结果是一样的
从修改的代码上可以看到,通过继承的确帮我们简化了Feign的开发,但Spring Cloud的是不推荐这种做法的,也不建议在服务提供方和服务消费方共享接口,因为这种方式会造成服务提供方与服务消费方之间代码的紧耦合。当然,是否能够使用这种特性还得具体到你自己的产品如何去权衡。
4. 与Swagger集成冲突
假如你的项目中使用了Swagger,那么可能会造成无法启动的错误:
Error starting ApplicationContext. To display the auto-configuration report re-run your application with 'debug' enabled. 2017-07-06 16:25:10 656 [restartedMain] ERROR o.s.boot.SpringApplication - Application startup failed org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'productController': Unsatisfied dependency expressed through field 'productService'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'io.twostepsfromjava.cloud.web.mall.service.ProductService': FactoryBean threw exception on object creation; nested exception is java.lang.NullPointerException at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:588) at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:88) ...
这个是因为所使用的Swagger过低造成的,只需要将Swagger升级即可,如:
<dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger-ui</artifactId> <version>2.6.1</version> </dependency> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger2</artifactId> <version>2.6.1</version> </dependency>
【Dalston】【第三章】声明式服务调用(Feign)的更多相关文章
- SpringCloud 源码系列(6)—— 声明式服务调用 Feign
SpringCloud 源码系列(1)-- 注册中心 Eureka(上) SpringCloud 源码系列(2)-- 注册中心 Eureka(中) SpringCloud 源码系列(3)-- 注册中心 ...
- SpringCloud之声明式服务调用 Feign(三)
一 Feign简介 Feign是一种声明式.模板化的HTTP客户端,也是netflix公司组件.使用feign可以在远程调用另外服务的API,如果调用本地API一样.我们知道,阿里巴巴的doubbo采 ...
- 第六章 声明式服务调用: Spring Cloud Feign
我们在使用 Spring Cloud Ribbon 时, 通常都会利用它对 RestTemplate 的请求拦截来实现对依赖服务的接口调用, 而 RestTemplate 已经实现了对 HTTP 请求 ...
- 004声明式服务调用Feign & 断路器Hystrix
1.POM配置 和普通Spring Boot工程相比,添加了Eureka Client.Feign.Hystrix依赖和Spring Cloud依赖管理 <dependencies> &l ...
- SpringCloud开发学习总结(七)—— 声明式服务调用Feign(一)
在实践的过程中,我们会发现在微服务架构中实现客户端负载均衡的服务调用技术Spring Cloud Ribbon<SpringCloud开发学习总结(四)—— 客户端负载均衡Ribbon> ...
- Spring Cloud 声明式服务调用 Feign
一.简介 在上一篇中,我们介绍注册中心Eureka,但是没有服务注册和服务调用,服务注册和服务调用本来应该在上一章就应该给出例子的,但是我觉得还是和Feign一起讲比较好,因为在实际项目中,都是使用声 ...
- Spring Cloud第七篇 | 声明式服务调用Feign
本文是Spring Cloud专栏的第七篇文章,了解前六篇文章内容有助于更好的理解本文: Spring Cloud第一篇 | Spring Cloud前言及其常用组件介绍概览 Spring Cloud ...
- Spring Cloud Feign 1(声明式服务调用Feign 简介)
Spring Cloud Feign基于Netflix Feign 同时整合了Spring Cloud Ribbon和Spring Cloud Hytrix,除了提供两者的强大功能外,它还提供了一种声 ...
- 声明式服务调用Feign
什么是 Feign Feign 是种声明式.模板化的 HTTP 客户端(仅在 consumer 中使用). 什么是声明式,有什么作用,解决什么问题? 声明式调用就像调用本地方法一样调用远程方法;无 ...
随机推荐
- 5.JVM的内存区域划分
一.JVM介绍 1. 什么是JVM? JVM是Java Virtual Machine(Java虚拟机)的缩写,JVM是一种用于计算设备的规范,它是一个虚构出来的计算机,是通过在实际的计算机上仿真模拟 ...
- sitecore中的两种编辑工具介绍
在Sitecore中,有两种编辑工具,您可以在其中创建和编辑网站上的内容: 内容编辑器 - 专为熟悉Sitecore及其包含的功能的经验丰富的内容作者而设计的应用程序. 体验编辑器 - 一种直观的编辑 ...
- XML系列之--对电文格式XML的简单操作(三)
前两章介绍了关于Linq创建.解析SOAP格式的XML,在实际运用中,可能会对xml进行一些其它的操作,比如基础的增删该查,而操作对象首先需要获取对象,针对于DOM操作来说,Linq确实方便了不少,如 ...
- 【Redis学习之八】Redis集群:主从复制
环境 虚拟机:VMware 10 Linux版本:CentOS-6.5-x86_64 客户端:Xshell4 FTP:Xftp4 jdk8 redis-2.8.18 Redis集群分类: 主从复制 R ...
- 转:【专题九】实现类似QQ的即时通信程序
引言: 前面专题中介绍了UDP.TCP和P2P编程,并且通过一些小的示例来让大家更好的理解它们的工作原理以及怎样.Net类库去实现它们的.为了让大家更好的理解我们平常中常见的软件QQ的工作原理,所以在 ...
- docker Dockerfile指令ADD和COPY的区别,添加目录方法
docker Dockerfile指令ADD和COPY的区别,添加目录方法 ADD指令的功能是将主机构建环境(上下文)目录中的文件和目录.以及一个URL标记的文件 拷贝到镜像中.其格式是: ADD 源 ...
- java实现 HTTP/HTTPS请求绕过证书检测代码实现
java实现 HTTP/HTTPS请求绕过证书检测代码实现 1.开发需求 需要实现在服务端发起HTTP/HTTPS请求,访问其他程序资源. 2.URLConnection和HTTPClient的比较 ...
- TensorFlow 1.2.0新版本完美支持Python3.6,windows在cmd中输入pip install tensorflow就能下载应用最新tensorflow
TensorFlow 1.2.0新版本完美支持Python3.6,windows在cmd中输入pip install tensorflow就能下载应用最新tensorflow 只需在cmd中输入pip ...
- 一篇关于蓝牙SDP和L2CAP协议的文章
SDP地址:http://www.cnblogs.com/strive-forever/archive/2011/11/04/2236640.html L2CAP地址:http://www.cnblo ...
- AtCoder Beginner Contest 044 C - 高橋君とカード / Tak and Cards
题目链接:http://abc044.contest.atcoder.jp/tasks/arc060_a Time limit : 2sec / Memory limit : 256MB Score ...