【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的值,否则,自动织入时就会报找不到ProductServicebean的错误.
编写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 中使用). 什么是声明式,有什么作用,解决什么问题? 声明式调用就像调用本地方法一样调用远程方法;无 ...
随机推荐
- Yii Restful api自定义字段
- C#操作XML方法详解
using System.Xml; //初始化一个xml实例 XmlDocument xml=new XmlDocument(); //导入指定xml文件 xml.Load(path); xml. ...
- springboot报错Whitelabel Error Page
第一次使用springboot没有问题.隔了两天继续看.一直报错Whitelabel Error Page. 重新搭建试了任何方法都错了. 报的就是一个404错误,犯了一个习惯性错误,一般都是loca ...
- Locust 设置响应断言
转:http://www.testclass.net/locust/assert/ 性能测试也需要设置断言么? 某些情况下是需要,比如你在请求一个页面时,就可以通过状态来判断返回的 HTTP 状态码是 ...
- php冒泡排序实现方法,传入几个数字排序后 输出实战例子
php冒泡排序实现方法,传入几个数字排序后 输出实战例子 算法和数据结构是一个编程工作人员的内功.四种入门级排序算法: 冒泡排序.选择排序.插入排序.快速排序. 一.冒泡排序 原理:对一组数据,比较相 ...
- javanio2
package com.lanhuigu.nio.selector; import java.net.InetSocketAddress; import java.nio.ByteBuffer; im ...
- Radio中REG
Auto REG/REG OFF在广播接收质量不好时,收音机首先仅调整到该广播电台当前发射的可选频率.但是,如果接收质量差到“该发射电台濒临消失”的程度,则收音机也会接收德国NDR1(北德意志广播电台 ...
- HDU 1232 畅通工程 (并查集)
某省调查城镇交通状况,得到现有城镇道路统计表,表中列出了每条道路直接连通的城镇.省政府“畅通工程”的目标是使全省任何两个城镇间都可以实现交通(但不一定有直接的道路相连,只要互相间接通过道路可达即可). ...
- Symfony2学习笔记之事件分配器
----EventDispatcher组件使用 简介: 面向对象编程已经在确保代码的可扩展性方面走过了很长一段路.它是通过创建一些责任明确的类,让它们之间变得更加灵活,开发者可以通过继承这 ...
- <转>jmeter(四)HTTP请求
本博客转载自:http://www.cnblogs.com/imyalost/category/846346.html 个人感觉不错,对jmeter讲解非常详细,担心以后找不到了,所以转发出来,留着慢 ...