一、介绍
1、Feign是Netflix公司开源的轻量级Rest客户端( https://github.com/OpenFeign/feign ),使用 Feign 可以非常方便、简单的实现 Http 客户端,使用 Feign 只需要定义一个接口,然后在接口上添加注解即可。
2、Spring Cloud 对 Feign 进行了封装,Feign 默认集成了 Ribbon 实现了客户端负载均衡调用。
目前大家更习惯用面向接口编程,比如 Service接口,Dao接口等,这是大家默认遵守的规范
3、微服务间的调用就有两种方式:
a) 通过微服务名称,获得服务的调用地址
b) 通过接口+注解,获得服务的调用 ——Feign (为适应业界其它程序员提出的,还是遵循面向接口编程)
类似于以前Mapper接口上使用@Mapper注解进行标识,而使用Feign就只要在接口上标注@FeignClient注解。
二、工作原理
1、Feign通过接口的方法调用Rest服务(之前是Ribbon+RestTemplate),请求发送给 Eureka 服务器(http://MICROSERVICE-PRODUCT/product/list), 通过Feign直接找到服务接口 ,因为集成了 Ribbon 技术,Feign 自带负载均衡配置功能。
2、 启动类添加@EnableFeignClients注解,Spring会扫描标记了@FeignClient注解的接口,并生成此接口的代理对象
3、 @FeignClient("服务名称 ") 即指定了 product 服务名称,Feign会从Eureka注册中心获取 product 服务列表,并通过负载均衡算法进行服务调用。
4、在接口方法中使用注解 @RequestMapping(value = “/product/list”,method = RequestMethod.GET),指定调用的url,Feign 会根据url进行远程调用。
三、 Feign 注意事项
1、SpringCloud对Feign进行了增强兼容了SpringMVC的注解 ,在使用SpringMVC的注解时需要注意:
- @FeignClient接口方法有基本类型参数在参数必须加@PathVariable(“XXX”) 或 @RequestParam(“XXX”)
- @FeignClient接口方法返回值为复杂对象时,此类型必须有无参构造方法。
四、代码
1、pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<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">
<parent>
<artifactId>microservice-cloud-01</artifactId>
<groupId>com.haoxiansheng.springcloud</groupId>
<version>1.0-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>microservice-cloud-07-consumer-product-feign</artifactId>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
<!-- Ribbon 相关依赖,eureka会自动引入Ribbon -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<dependency>
<groupId>com.haoxiansheng.springcloud</groupId>
<artifactId>microservice-cloud-02-api</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
</project>
2、application.yml
server:
port: 80
eureka:
client:
register-with-eureka: false
fetch-registry: true
service-url:
defaultZone: http://127.0.0.1:6001/eureka/,http://127.0.0.1:6002/eureka/
# 需要开启hystrix
feign:
hystrix:
enabled: true
3、Controller
package com.haoxiansheng.springcloud;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.openfeign.EnableFeignClients;
//会扫描标记了指定包下@FeignClient注解的接口,并生成此接口的代理对象
@EnableFeignClients(basePackages= {"com.zenlayer.springcloud"})
@EnableEurekaClient
@SpringBootApplication
public class ProductConsumer_80_Feign {
public static void main(String[] args) {
SpringApplication.run(ProductConsumer_80_Feign.class, args);
}
}
package com.haoxiansheng.springcloud.controller;
import com.zenlayer.springcloud.entities.Product;
import com.haoxiansheng.springcloud.service.ProductClientService;
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.RestController;
import java.util.List;
@RestController
@RequestMapping("/consumer/product")
public class ProductController_Feign {
@Autowired
private ProductClientService productClientService;
@RequestMapping(value = "/add")
public boolean add(Product product) {
return productClientService.add(product);
}
@RequestMapping(value = "/get/{id}")
public Product get(@PathVariable("id") Integer id) {
return productClientService.get(id);
}
@RequestMapping(value = "/list")
public List<Product> list() {
return productClientService.list();
}
}
4、Service
package com.haoxiansheng.springcloud.service;
import com.zenlayer.springcloud.entities.Product;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import java.util.List;
//指定调用的服务 MICROSERVICE-PRODUCT
//
@FeignClient(value = "MICROSERVICE-PRODUCT", fallback = ProductClientServiceFallBack.class)
public interface ProductClientService {
@GetMapping(value = "/product/get/{id}")
Product get(@PathVariable("id") Integer id);
@GetMapping(value = "/product/list")
List<Product> list();
@PostMapping(value = "/product/add")
boolean add(Product product);
}
package com.haoxiansheng.springcloud.service;
import com.zenlayer.springcloud.entities.Product;
import org.springframework.stereotype.Component;
import java.util.List;
@Component
public class ProductClientServiceFallBack implements ProductClientService {
@Override
public Product get(Integer id) {
return new Product(id, "id="+id +"无数据--feign&hystrix","无有效数据库");
}
@Override
public List<Product> list() {
return null;
}
@Override
public boolean add(Product product) {
return false;
}
}