OpenFeign 与负载均衡

1. 概述

1.1 OpenFeign简介

        Feign,假装、伪装。OpenFeign 可以将提供者提供的 Restful 服务伪装为接口进行消费,消费者只需使用“feign 接口 + 注解”的方式即可直接调用提供者提供的 Restful 服务,而无需再使用 RestTemplate

对于 OpenFeign,可简单总结为以下几点:

  • OpenFeign 只涉及 Consumer 与 Provider 无关。因为其是用于 Consumer 调用 Provider 的

  • OpenFeign 仅仅就是一个伪客户端,其不会对请求做任务的处理

  • OpenFeign 是通过注解的方式实现 RESTful 请求的

1.2 OpenFeign 与 Ribbon

        OpenFeign 具有负载均衡功能,其可以对指定的微服务采用负载均衡方式进行消费、访问。之前老版本 Spring Cloud 所集成的 OpenFeign 默认采用了 Ribbon 负载均衡器。但由于Netflix 已不再维护 Ribbon,所以从 Spring Cloud 2021.x 开始集成的 OpenFeign 中已彻底丢弃Ribbon,而是采用 Spring Cloud 自行研发的 Spring Cloud Loadbalancer 作为负载均衡器。

2. OpenFeign 用法

consumer方

2.1 Maven 依赖

<!--feign 依赖-->
<dependency>
 <groupId>org.springframework.cloud</groupId>
 <artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>

2.2 定义 Feign 接口

2.3 修改启动类

在启动类上添加@EnableFeignClients 注解

2.4 设置超时时间

spring:
  application:
    name: depart-consumer

  cloud:
    nacos:
      discovery:
        server-addr: localhost:8848   # nacos注册中心地址
        username: nacos
        password: nacos

    openfeign:
      client:
        config:
          default:   # 全局设置
            # 连接超时:consumer连接上provider的时间阈值,起决定作用的是网络状况
            connect-timeout: 1
            # 读超时:consumer发出请求到接收到provider的响应这段时间阈值,起决定作用的是provider的业务逻辑
            read-timeout: 1
          depart-provider:   # 局部设置
            connect-timeout: 1
            read-timeout: 20

2.5 Gzip 压缩设置

OpenFeign 可对请求与响应进行压缩设置

spring:
  application:
    name: depart-consumer

  cloud:
    nacos:
      discovery:
        server-addr: localhost:8848   # nacos注册中心地址
        username: nacos
        password: nacos

    openfeign:
      client:
        config:
          default:   # 全局设置
            # 连接超时:consumer连接上provider的时间阈值,起决定作用的是网络状况
            connect-timeout: 1
            # 读超时:consumer发出请求到接收到provider的响应这段时间阈值,起决定作用的是provider的业务逻辑
            read-timeout: 1
          depart-provider:   # 局部设置
            connect-timeout: 1
            read-timeout: 20
      compression:
        request:
          enabled: true
          mime-types: ["text/xml", "application/xml", "application/json", "video/mp4"]
          min-request-size: 1024
        response:
          enabled: true

3. 选择远程调用的底层实现技术

3.1 理论基础

        feign 的远程调用底层实现技术默认采用的是 JDK 的 URLConnection,同时还支持HttpClient 与 OkHttp。
        由于 JDK 的 URLConnection 不支持连接池,通信效率很低,所以生产中是不会使用该默认实现的。所以在 Spring Cloud OpenFeign 中直接将默认实现变为了 HttpClient,同时也支持OkHttp。

用户可根据业务需求选择要使用的远程调用底层实现技术。

3.2 配置说明

        在 spring.cloud.openfeign.httpclient 下有大量 HttpClient 的相关属性设置。其中可以发现spring.cloud.openfeign.httpclient.enabled 默认为 true。

        在 spring.cloud.openfeign.okhttp.enabled 默认值为 false,表明默认没有启动 OkHttp。

4. 负载均衡

4.1 默认负载均衡策略

        OpenFeign 的负载均衡器 Ribbon 默认采用的是轮询算法

4.2 更换负载均衡策略

        定义一个 Config 类

        

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

public class DepartConfig {

    @Bean
    public ReactorLoadBalancer<ServiceInstance> randomLoadBalancer(Environment e, LoadBalancerClientFactory factory) {
        // 获取负载均衡客户端名称,即提供者服务名称
        String name = e.getProperty(LoadBalancerClientFactory.PROPERTY_NAME);
        // 从所有provider实例中指定名称的实例列表中随机选择一个实例
        // 参数1:获取指定名称的所有provider实例列表
        // 参数2:指定要获取的provider服务名称
        return new RandomLoadBalancer(factory.getLazyProvider(name, ServiceInstanceListSupplier.class), name);
    }
}

        在启动类上添加@LoadBalancerClients 注解,并指定前面定义的配置类。

@LoadBalancerClients(defaultConfiguration = DepartConfig.class)
@SpringBootApplication
@EnableFeignClients   // 开启OpenFeign
public class OpenFeignConsumer {

    public static void main(String[] args) {
        SpringApplication.run(OpenFeignConsumer.class, args);
    }

}

  • 14
    点赞
  • 18
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值