OpenFeign是什么

官网解释:
https://cloud.spring.io/spring-cloud-static/Hoxton.SR1/reference/htmlsingle/#spring-cloud-openfeign
Feign是一个声明式WebService客户端。 使用Feign能让编写Web Service客户端更加简单。
它的使用方法是定义一个服务接口然后在上面添加注解。Feign也支 持可拔插式的编码器和解码器。Spring Cloud对Feign进行了封装,使其支持了Spring MVC标准注解和HttpMessageConverters。 Feign可以与Eureka和Ribbon组合使用以支持负载均衡
image.png
Feign是一个声明式的Web服务客户端,让编写Web服务客户端变得非常容易,只需#创建一个接口并在接口上添加注解即可
源码 https://github.com/spring-cloud/spring-cloud-openfeign

作用

Feign能干什么

Feign旨在使编写Java Http客户端变得更容易。
前面在使用Ribbon+ RestTemplate时,利用RestTemplate对http请求的封装处理,形成了一套模版化的调用方法。 但是在实际开发中,由于对服务依赖的调用可能不止一处, 往往一个接口会被多处调用, 所以通常都会针对每个微服务自行封装-些客户端类来包装这些依赖服务的调用。所以,Feign在此基础上做了进-步封装, 由他来帮助我们定义和实现依赖服务接口的定义。在Feign的实现下,我们只需创建一个接口并使用注解的方式来配置它(以前是Dao接口 上面标注Mapper注解,现在是一个微服务接口 上面标注一个Feign注解即可,即可完成对服务提供方的接口绑定,简化了使用Spring cloud Ribbon时,自动封装服务调用客户端的开发量。

Feign集成了Ribbon

利用Ribbon维护了Payment的服务列表信息,并且通过轮询实现了客户端的负载均衡。而与Ribbon不同的是,通过feign只需要定义服务绑定接口且以声明式的方法,优雅而简单的实现了服务调用

Feign和OpenFeign两者区别
image.png

OpenFeign使用步骤

新建模块

cloud-consumer-feign-order80

pom

  1. <description>订单消费者之feign</description>
  2. <dependencies>
  3. <!--openfeign-->
  4. <dependency>
  5. <groupId>org.springframework.cloud</groupId>
  6. <artifactId>spring-cloud-starter-openfeign</artifactId>
  7. </dependency>
  8. <!--eureka client-->
  9. <dependency>
  10. <groupId>org.springframework.cloud</groupId>
  11. <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
  12. </dependency>
  13. <dependency>
  14. <groupId>com.carve.springcloud</groupId>
  15. <artifactId>cloud-api-common</artifactId>
  16. <version>${project.version}</version>
  17. </dependency>
  18. <dependency>
  19. <groupId>org.springframework.boot</groupId>
  20. <artifactId>spring-boot-starter-web</artifactId>
  21. </dependency>
  22. <!--监控-->
  23. <dependency>
  24. <groupId>org.springframework.boot</groupId>
  25. <artifactId>spring-boot-starter-actuator</artifactId>
  26. </dependency>
  27. <!--热部署-->
  28. <dependency>
  29. <groupId>org.springframework.boot</groupId>
  30. <artifactId>spring-boot-devtools</artifactId>
  31. <scope>runtime</scope>
  32. <optional>true</optional>
  33. </dependency>
  34. <dependency>
  35. <groupId>org.projectlombok</groupId>
  36. <artifactId>lombok</artifactId>
  37. <optional>true</optional>
  38. </dependency>
  39. <dependency>
  40. <groupId>org.springframework.boot</groupId>
  41. <artifactId>spring-boot-starter-test</artifactId>
  42. <scope>test</scope>
  43. </dependency>
  44. </dependencies>

yml

  1. server:
  2. port: 80
  3. eureka:
  4. client:
  5. register-with-eureka: false
  6. fetch-registry: true
  7. service-url:
  8. defaultZone: http://eureka7001.com:7001/eureka,http://eureka7002.com:7002/eureka

启动类

@SpringBootApplication
@EnableEurekaClient // eureka
@EnableFeignClients // feign
public class OrderFeignMain80 {
    public static void main(String[] args) {
        SpringApplication.run(OrderFeignMain80.class, args);
    }
}

业务类

新建一个feign的调用接口,添加@FeignClient注解
此处的 @GetMapping()注解一定要有,并且方法的参数要和8001和8002的参数一致才可以,方法名不一致也可以。

@Component
@FeignClient("cloud-payment-service")
public interface PaymentFeignService {
    @GetMapping("/payment/get/{id}")
    CommonResult get(@PathVariable("id") Long id);
}

Controller

@RestController
@Slf4j
public class OrderController {

    @Autowired
    private PaymentFeignService paymentFeignService;

    @GetMapping("/consumer/payment/get/{id}")
    public CommonResult get(@PathVariable Long id) {
        return paymentFeignService.get(id);
    }
}

测试

先启动2个eureka集群7001/7002
再启动2个微服务8001/8002
启动OpenFeign80
http://localhost/consumer/payment/get/31
Feign自带负载均衡配置项

总结

image.png

OpenFeign超时控制

超时错误

8001,8002 写一个睡眠3秒的接口
openfeign80 进行调用
OpenFeign默认等待1秒钟,超过后报错
image.png

超时配置

修改yml 增加ribbon配置

# 设置feign客户端超时时间(OpenFeign默认支持ribbon)
ribbon:
  # 指的是建立连接所用的时间,适用于网络状态正常的情况下,两端连接所用的时间
  ReadTimeout: 5000
  # 指的是建立连接后从服务器读取到可用资源所用的时间
  ConnectTimeout: 5000

OpenFeign日志打印功能

Feign提供了日志打印功能,我们可以通过配置来调整日志级别,从而了解Feign中Http请求的细节。
说白了就是对Feign接口的调用情况进行监控和输出

日志级别

  • NONE:默认的,不显示任何日志;
  • BASIC:仅记录请求方法、URL、 响应状态码及执行时间;
  • HEADERS:除了BASIC中定义的信息之外,还有请求和响应的头信息;
  • FULL:除了HEADERS中定义的信息之外,还有请求和响应的正文及元数据。

步骤

1.创建配置bean

/**
 * OpenFeignClient配置
 **/
@Configuration
public class FeignConfig {
    /**
     * feignClient配置日志级别
     */
    @Bean
    public Logger.Level feignLoggerLevel() {
        // 请求和响应的头信息,请求和响应的正文及元数据
        return Logger.Level.FULL;
    }
}

2.修改yml

在yml中增加logging配置

logging:
  level:
    # feign日志以什么级别监控哪个接口
    com.carve.springcloud.service.PaymentFeignService: debug

3.查看控制台

image.png