【服务调用】OpenFeign

概述

介绍

Feign是一个声明式的Web服务客户端,让编写Web服务客户端变得非常容易,只需创建一个接口并在接口上添加注解即可。Feign也支持可拔插式的编码器和解码器。Spring Cloud 对 Feign进行了封装,使其支持了Spring MVC标准注解和HttpMessageConverters。Feign可以与Eureka和Ribbon组合使用以支持负载均衡
https://github.com/spring-cloud/spring-cloud-openfeign

作用

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

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

两者区别
Feign是Spring Cloud组件中的一个轻量级RESTful的HTTP服务客户端
Feign内置了Ribbon,用来做客户端负载均衡,去调用服务注册中心的服务。Feign
的使用方法是:使用Feign的注解定义接口,调用这个接口,就可以调用服务注册中心的服务
OpenFeign是Spring Cloud 在Feign的基础上支持了SpringMVC的注解,如@RequestMapping等等。OpenFeign的@FeignClient可以解析SpringMVC的@RequestMapping注解下的接口,并通过动态代理的方式实现类,实现类中做负载均衡并调用其他服务。

使用

  1. 新建 cloud-consumer-feign-order80 Feign在消费端使用
  2. POM文件
<dependencies> 
	
    <dependency>        
	    <groupId>org.springframework.cloudgroupId>  
        <artifactId>spring-cloud-starter-openfeignartifactId>  
    dependency>  
    <dependency>        
	    <groupId>org.springframework.cloudgroupId>  
        <artifactId>spring-cloud-starter-netflix-eureka-clientartifactId>  
    dependency>  
    <dependency>        
	    <groupId>com.atguigu.springcloudgroupId>  
        <artifactId>cloud-api-commonsartifactId>  
        <version>${project.version}version>  
    dependency>  
    <dependency>        
	    <groupId>org.springframework.bootgroupId>  
        <artifactId>spring-boot-starter-webartifactId>  
    dependency>  
    <dependency>        
	    <groupId>org.springframework.bootgroupId>  
        <artifactId>spring-boot-starter-actuatorartifactId>  
    dependency>  
    <dependency>        
	    <groupId>org.springframework.bootgroupId>  
        <artifactId>spring-boot-devtoolsartifactId>  
        <scope>runtimescope>  
        <optional>trueoptional>  
    dependency>  
    <dependency>        
	    <groupId>org.projectlombokgroupId>  
        <artifactId>lombokartifactId>  
        <optional>trueoptional>  
    dependency>  
    <dependency>        
	    <groupId>org.springframework.bootgroupId>  
        <artifactId>spring-boot-starter-testartifactId>  
        <scope>testscope>  
    dependency>
dependencies>
  1. yml
server:  
  port: 80  
  
eureka:  
  client:  
    register-with-eureka: false  
    service-url:  
      defaultZone: http://eureka7001.com:7001/eureka/,http://eureka7002.com:7002/eureka/
  1. 主启动类
package com.atguigu.springcloud;  
  
import org.springframework.boot.SpringApplication;  
import org.springframework.boot.autoconfigure.SpringBootApplication;  
import org.springframework.cloud.openfeign.EnableFeignClients;  
  
@SpringBootApplication  
@EnableFeignClients //开始使用 EnableFeign
public class OrderFeignMain80 {  
    public static void main(String[] args) {  
        SpringApplication.run(OrderFeignMain80.class,args);  
    }  
}
  1. 业务类
    业务逻辑接口+ @FeignClient配置调用provider服务
    新建PaymentFeignService接口并新增注解@FeignClient
 package com.atguigu.springcloud.service;  
  
import com.atguigu.springcloud.entities.CommonResult;  
import com.atguigu.springcloud.entities.Payment;  
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;  
  
@Component  
@FeignClient(value = "CLOUD-PAYMENT-SERVICE")  
public interface PaymentFeignService {  
  
    @GetMapping(value = "/payment/get/{id}")  
    CommonResult<Payment> getPaymentById(@PathVariable("id") Long id);  
}

控制层Controller

package com.atguigu.springcloud.controller;  
  
import com.atguigu.springcloud.entities.CommonResult;  
import com.atguigu.springcloud.entities.Payment;  
import com.atguigu.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;  
  
@RestController  
@Slf4j  
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);  
    }  
  
}

**PS:Feign 自带负载均衡配置项

超时控制

默认Feign客户端只等待一秒钟,但是服务端处理需要超过1秒钟,导致Feign客户端不想等待了,直接返回报错。为了避免这样的情况,有时候我们需要设置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: 5000  
  #指的是建立连接后从服务器读取到可用资源所用的时间  
  ConnectTimeout: 5000

日志打印功能

简介

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

日志级别

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

配置日志bean

package com.atguigu.springcloud.config;  
  
import feign.Logger;  
import org.springframework.context.annotation.Bean;  
import org.springframework.context.annotation.Configuration;  
  
@Configuration  
public class FeignConfig {  
  
    @Bean  
    Logger.Level feignLoggerLevel(){  
        return Logger.Level.FULL;  
    }  
}

YAML 开启日志客户端

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: 5000  
  #指的是建立连接后从服务器读取到可用资源所用的时间  
  ConnectTimeout: 5000  
  
logging:  
  level:  
    # feign 日志以什么级别监控哪个接口  
    com.atguigu.springcloud.service.PaymentFeignService: debug

你可能感兴趣的:(spring,cloud,java)