springcloud----负载均衡--Feign与OpenFeign

什么是 Feign

Feign是声明性Web服务客户端。它使编写Web服务客户端更加容易。要使用Feign,请创建一个接口并对其进行注释。它具有可插入注释支持,包括Feign注释JAX-RS注释。Feign还支持可插拔编码器解码器。Spring Cloud添加了对Spring MVC注释的支持,并支持使用HttpMessageConvertersSpring Web默认使用的注释。Spring Cloud集成了RibbonEureka以及Spring Cloud LoadBalancer,以在使用Feign时提供负载平衡的http客户端


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


Feign 能干什么

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

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

Feign 集成了 Ribbon

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

Feign 与 OpenFeign 的区别

Feign

feign 是 spring cloud 组件中的一个轻量级 RESTful的HTTP 服务客户端 Reign 内置了 Ribbon, 用来做客户端服务在均衡,去调用服务注册中心的服务,Feign 的使用方式是:使用Feign 注解自定义接口,调用这个接口,就可以低哦啊用服务注册中心的服务。

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-feign</artifactId>
</dependency>

OpenFeign

openfeign 是 spring cloudfeign 的基础上支持 spring mvc注解 ,如 @RequestMapping 等等,OpenFeign 的@FeignClient 可以解析Spring MVC 的 @RequestMapping注解下的接口,并通过动态代理的方式生产实现类 ,实现类中做负载均衡并且 低耦合调用其他服务。

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>

使用

1、模块名 cloud-consumer-feign-order80
2、pml.xml (web、actuator以及其他的java自行导入)
<!--openfeign  也整合了ribbon-->
<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>
3、yml 配置文件
server:
  port: 80
spring:
  application:
    name: cloud-order-consumer
eureka:
  client:
    register-with-eureka: false
    service-url:
      defaultZone: http://eureka7001.com:7001/eureka
4、主启动
@SpringBootApplication
@EnableFeignClients  // 开启使用Fegin
public class OrderFeignMain80 {
    public static void main(String[] args){
        SpringApplication.run(OrderFeignMain80.class,args) ;
    }
}

服务接口

@FeignClient(value = "CLOUD-PAYMENT-SERVICE")   // 微服务名
@Component
public interface PaymentFeignService {
    @GetMapping("/payment/get/{id}")
    CommonResult getPaymentById(@PathVariable("id") Long id) ;

    @GetMapping("/payment/feign/timeout")
    String paymentFeignTimeout();
}

本服务接口

@RestController
public class PaymentFeignController {
    @Autowired
    private PaymentFeignService paymentFeignService ;

    @GetMapping("/consumer/payment/get/{id}")
    public CommonResult<Payment> getPaymentById(@PathVariable("id") Long id){
        System.out.println("feign-------"+id);
        return paymentFeignService.getPaymentById(id) ;
    }

    @GetMapping("/consumer/payment/feign/timeout")
    public String paymentFeignTimeout(){
        return paymentFeignService.paymentFeignTimeout() ;
    }
}
5、测试

OpenFeign 的超时控制

在配置文件中进行配置

ribbon: 
  ReadTimeout: 5000
  ConnectTimeout: 5000

参数说明

ribbon.ReadTimeout: 指建立连接所用时间,适用于网络状况正常情况下,两端连接所有时间(毫秒)
ribbon.ConnectTimeout: 指建立连接后从服务器读到可用资源所用时间

OpenFeign 日志打印

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

日志级别

日志级别 内容
NONE 默认,不显示任何日志 ;
BASIC 仅记录请求方法、URL、响应状态码和执行时间
HEADERS BASIC + 请求头和响应头
FULL HEADERS+ 请求和响应的正文及元数据

1、yml配置

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

2、配置类

@Configuration
public class FeignConfig {
    /**
     * 配置日志级别
     * @return
     */
    @Bean
    public Logger.Level feignLoggerLevel() {
        return Logger.Level.FULL;
    }
}
发布了119 篇原创文章 · 获赞 9 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/getchar97/article/details/105088716