七、OpenFeign服务接口调用

一、概述

1.1、OpenFeign概述

  GitHub :https://github.com/spring-cloud/spring-cloud-openfeign

  Feign是一个声明式WebService客户端。所以在微服务架构中作用在服务的调用方。使用Feign能让编写Web Service客户端更加简单。它的使用方法是定义一个服务接口然后在上面添加注释,Feign也支持可拔插式的编码器和解码器。Spring Cloud对Feign进行了封装,使其支持了Spring MVC标准注解和HttpMessageConverters。Feign可以与Eureka和Ribbon组合使用以支持负载均衡

  Feign是一个声明式的web服务客户端,让编写web服务客户端变得非常容易,只需创建一个接口并在接口上添加注解即可。

1.2、Feign作用

  Feign旨在使编写Java Http客户端变的更加容易。

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

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

1.3、Feign和OpenFeign两者区别

FeignOpenFeign
Feign是SpringCloud组件中一个轻量级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使用

  OpenFeign使用接口+注解的方式,微服务调用接口+@FeignClient

1、新建Spring Boot Model名称:cloud-consumer-feign-order80

2、pom添加如下依赖

<dependencies>
  <dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-openfeign</artifactId>
  </dependency>
  <dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
  </dependency>
  <dependency>
    <groupId>com.jdy</groupId>
    <artifactId>cloud-api-commons</artifactId>
    <version>1.0-SNAPSHOT</version>
  </dependency>
  <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
  </dependency>
  <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-actuator</artifactId>
  </dependency>
  <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-devtools</artifactId>
    <scope>runtime</scope>
    <optional>true</optional>
  </dependency>
  <dependency>
    <groupId>org.projectlombok</groupId>
    <artifactId>lombok</artifactId>
    <optional>true</optional>
  </dependency>
</dependencies>

3、编写配置文件application.yml

server:
  port: 80
eureka:
  client:
    register-with-eureka: false
    service-url:
      defaultZone: http://eureka7001.com:7001/eureka, http://eureka7002.com:7002/eureka

4、编写主启动类添加@EnableFeignClients注解

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

5、业务类

  业务逻辑接口+@FeignClient配置调用provider服务。新建PaymentFeignService接口并新增注解@FeignClient

@Component
@FeignClient(value = "CLOUD-PAYMENT-SERVICE")
public interface PaymentFeignService {

    @GetMapping(value = "/payment/get/{id}")
    public CommonResult getPaymentById(@PathVariable("id") Long id);
}

  控制层Controller

@RestController
public class OrderFeignController {

    @Resource
    private PaymentFeignService paymentFeignService;

    @GetMapping(value = "/consumer/payment/get/{id}")
    public CommonResult<Payment> getPaymentById(@PathVariable("id") Long id){
       return paymentFeignService.getPaymentById(id);
    }
}

6、测试

三、OpenFeign超时控制

1、超时设置

  故意设置超时演示出错情况。服务提供方8001故意写暂停程序

@GetMapping(value = "/payment/feign/timeout")
public String paymentFeignTimeout(){
    try { 
      TimeUnit.SECONDS.sleep(3);
   }catch (Exception e) {
      e.printStackTrace();
  }
    return serverPort;
}

  服务消费方80添加超时方法PaymentFeignService

@GetMapping(value = "/payment/feign/timeout")
public String paymentFeignTimeout();

  服务消费方80添加超时方法OrderFeignController

@GetMapping(value = "/consumer/payment/feign/timeout")
public String paymentFeignTimeout(){
   return paymentFeignService.paymentFeignTimeout();
}

  测试:http://localhost/consumer/payment/feign/timeout

 OpenFeign默认等待一秒钟,超过后报错

2、超时说明

  默认Feign客户端只等待一秒钟,但是服务端处理需要超过1秒钟,导致Feign客户端不想等待了,直接返回报错。为了避免这样的情况,有时候我们需要设置Feign客户端的超时时间。

3、开启OpenFeign客户端超时控制

ribbon:
  #建立链接所用的时间,适用于网络状态正常的情况下,两端链接所用的时间
  ReadTimeout:  5000
  # 建立链接后从服务器读取到可用资源所用的时间。
  ConnectTimeout: 5000

四、OpenFeign日志打印功能

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

  日志级别

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

1、配置日志bean  

@Bean
Logger.Level feignLoggerLevel(){
      return Logger.Level.FULL;
}

2、YML文件里需要开启日志的Feign客户端

logging:
  level:
    # feign日志以什么级别监控那个接口
    com.jdy.springcloud.service.PaymentFeignService: debug
原文地址:https://www.cnblogs.com/jdy1022/p/14555070.html