概述
Feign是一个声明式WebService客户端。使用Feign能让编写Web Service客户端更加简单。
  它的使用方法是定义一个服务接口然后在上面添加注解。Feign也支持可拨插式的编码器和解码器。SpringCloud对Feign进行了封装,使其支持了Spring MVC标准注解和HTTPMessageConverters。Feign可以与Eureka和Ribbon组合使用以支持负载均衡。
简单来说,Feign是一个声明式的Web服务客户端,让编写Web服务客户端变得非常容易,只需要创建一个接口并在接口上添加注解即可。
GitHub地址:https://github.com/spring-cloud/spring-cloud-openfeign
Feign旨在使编写java Http客户端变得更加容易。
  前面在使用Ribbon+RestTemplate时,利用RestTemplate对http请求的封装处理,形成了一套模板化的调用方法。但是在实际开发中,由于对服务依赖的调用可能不止一处,往往一个接口会被多处调用,所以通常都会针对每个微服务自行封装一些客户端类来包装这些依赖服务的调用。所以Feign在此基础上做了进一步的封装,由它来帮我们定义和实现依赖服务接口的定义。
  在Feign的实现下,我们只需创建一个接口并使用注解的方式来配置它(以前是dao接口上面标注Mapper注解,现在是一个微服务接口上标注一个Feign注解即可),即可完成对服务提供方的接口绑定,简化了使用Spring cloud Ribbon时,自动封装服务调用客户端的开发量。
Feign集成了Ribbon
  利用Ribbon维护了Payment的服务列表信息,并且通过轮询实现了客户端的负载均衡,而与Ribbon不同的是,通过feign只需要定义服务绑定接口且以声明式的方法,优雅而简单的实现了服务调用。
Feign和OpenFeign的区别
| Feign | OpenFeign | 
|---|---|
| Feign是Spring Cloud组件中的一个轻量级RESTful的http服务客户端。Feign内置了Ribbon,用来做客户端负载均衡,去调用服务注册中心的服务。Feign的使用方式是:使用Feign的注解定义接口,调用这个接口,就可以调用服务注册中心的服务 | OpenFeign是Spring Cloud在Feign的基础上支持了SpringMVC的注解,如@RequestMapping等。OpenFeign的@FeignClient可以解析SpringMVC的@RequestMapping注解下的接口,并通过动态代理的方式产生实现类,实现类中做负载均衡并调用其他服务。 | 
<dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-feign</artifactId></dependency><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-openfeign</artifactId></dependency>
Openfeign使用步骤
接口+注解
微服务调用接口+@FeignClient
新建Module
新建Module cloud-consumer-feign-order80
pom文件
<?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>cloud2020</artifactId>
        <groupId>com.sgy.cloud2020</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>
    <artifactId>cloud-consumer-feign-order80</artifactId>
    <dependencies>
        <dependency>
            <groupId>com.sgy.cloud2020</groupId>
            <artifactId>cloud-consumer</artifactId>
            <version>${project.version}</version>
        </dependency>
        <!-- openFeign -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-openfeign</artifactId>
        </dependency>
        <!--eureka client-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>
        <!-- 添加健康监控依赖 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <scope>provided</scope>
        </dependency>
    </dependencies>
</project>
application.yml文件
server:
  port: 80
eureka:
  client:
    register-with-eureka: false
    service-url:
      defaultZone: http://eureka7001.com:7001/eureka,http://eureka7002.com:7002/eureka
启动类
@SpringBootApplication
@EnableFeignClients
public class OrderFeign80 {
    public static void main(String[] args) {
        SpringApplication.run(OrderFeign80.class,args);
    }
}
业务类
业务逻辑接口+@FeignClient配置调用provider服务(新建PaymentfeignService接口并新增注解@FeignClient)
package com.sgy.springcloud.service;
import com.sgy.springcloud.entities.R;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
/**
 * Created by AaronShen on 2020/5/28
 * 该接口与8091生产者对应的Controller
 */
@Component
@FeignClient(value = "CLOUD-PAYMENT-SERVER") // 指定调用哪个微服务
public interface PaymentFeignService {
    /**
     * 根据订单id,查询订单
     */
    @GetMapping(value = "/payment/find/{id}")
    R find(@PathVariable(value = "id",required = true) String id);
}
消费者Controller
package com.sgy.springcloud.controller;
import com.sgy.springcloud.entities.R;
import com.sgy.springcloud.service.PaymentFeignService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;
/**
 * Created by AaronShen on 2020/5/28
 */
@RestController
@Slf4j
public class OrderfeignController {
    @Resource
    PaymentFeignService paymentFeignService;
    @GetMapping("/order/find/payment/{id}")
    public R find(@PathVariable(value = "id") String id) {
        return paymentfeignService.find(id);
    }
}
测试
1)启动eureka
2)启动生产者
3)启动消费者(OpenFeign)
4)postman输入网址

再次发送请求

OpenFeign默认采用的也是轮询算法。
OpenFeign的超时控制
OpenFeign默认等待1秒钟,超过后报错
情景模拟
1)在生产者的controller层模拟一个默认的超时的方法(集群都需要添加)
@GetMapping("/payemnt/timeout")
public String testTimeout() {
    try {
        TimeUnit.SECONDS.sleep(5);
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
    return "success";
}
- 在80服务调用
 
/**
 * Created by AaronShen on 2020/5/28
 * 该接口与8091生产者对应的Controller
 */
@Component
@FeignClient(value = "CLOUD-PAYMENT-SERVER") // 指定调用哪个微服务
public interface PaymentfeignService {
    /**
     * 根据订单id,查询订单
     */
    @GetMapping(value = "/payment/find/{id}")
    R find(@PathVariable(value = "id",required = true) String id);
    @GetMapping("/payemnt/timeout")
    String testTimeout();
}
- controller层
 
/**
 * Created by AaronShen on 2020/5/28
 */
@RestController
@Slf4j
public class OrderfeignController {
    @Resource
    PaymentfeignService paymentfeignService;
    @GetMapping("/order/find/payment/{id}")
    public R find(@PathVariable(value = "id") String id) {
        return paymentfeignService.find(id);
    }
    @GetMapping("/test/timeout")
    public String testTimeout() {
        return paymentfeignService.testTimeout();
    }
}
测试
- 直接调用8090的controller
 

- 调用80的controller
 
后台报错

超时控制
默认Feign客户端只等待一秒钟,但是服务端处理需要超过1秒钟,导致feign客户端不想等了,直接返回报错。为了避免这样的情况,有时候我们需要设置Feign客户端的超时控制
在yml中配置:
server:
  port: 80
eureka:
  client:
    register-with-eureka: false
    service-url:
      defaultZone: http://eureka7001.com:7001/eureka,http://eureka7002.com:7002/eureka
# 设置feign客户端超时时间(OpenFeign默认支持Ribbon)
#ribbon:
#  ReadTimeout: 10000           # 建立连接所用时间,适用于网络状况正常的情况下,两端连接所用的时间
#  ConnectTimeout: 10000        # 建立连接后,从服务器读取到的可用资源所用的时间
feign:
  hystrix:
    enabled: true
  client:
    config:
      default:
        # 设置feign客户端超时时间(OpenFeign默认支持Ribbon)
        #简历连接所用的时间,适用于网络状况正常的情况下,两端连接所需要的时间
        ConnectTimeout: 5000
        #指建立连接后从服务端读取到可用资源所用的时间
        ReadTimeout: 10000
设置完之后,等待80重新部署,完成,再次访问,如图:

OpenFeign日志打印功能
Feign提供了日志打印功能,我们可以通过配置来调整日志级别,从而了解Feign中Http请求的细节。说白了就是对Feign接口的调用情况进行监控和输出
日志级别
NONE:默认的,不显示任何日志
BASIC:仅记录请求方法、URL、响应状态码及执行时间
HEADERS:除了BASIC中定义的信息之外,还有请求和响应的头信息
FULL:除了HEADERS中定义的信息之外,还有请求和响应的正文及元数据
配置Bean

@Configuration
public class FeignConfig {
    @Bean
    Logger.Level feignLogger() {
        return Logger.Level.FULL;
    }
}
yml文件开启日志的Feign客户端
server:
  port: 80
eureka:
  client:
    register-with-eureka: false
    service-url:
      defaultZone: http://eureka7001.com:7001/eureka,http://eureka7002.com:7002/eureka
# 设置feign客户端超时时间(OpenFeign默认支持Ribbon)
ribbon:
  ReadTimeout: 10000           # 建立连接所用时间,适用于网络状况正常的情况下,两端连接所用的时间
  ConnectTimeout: 10000        # 建立连接后,从服务器读取到的可用资源所用的时间
logging:
  level:
    com.sgy.springcloud.service.PaymentFeignService: debug   # feign日志以什么级别监控哪个接口
查看日志
启动80微服务,调用服务,然后查看日志记录


