【springCloudAlibaba-学习笔记】03-openFeign

文章来源B站   作者:临窗旋墨   发布时间:2024-02-07   阅读:358   标签:spring-cloud-alibaba,学习笔记 分类:spring cloud alibaba 专题:学习记录

【springCloudAlibaba-学习笔记】03-openFeign

springCloudAlibaba-学习笔记

039-OpenFeign概述

Spring Cloud OpenFeign

Declarative REST Client: Feign creates a dynamic implementation of an interface decorated with JAX-RS or Spring MVC annotations

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

  • JAX-RS:Java api extensions of RESTful web services。

OpenFeign总结:

  1. 只涉及consumer,和provider无关。用于消费端调用提供端
  2. 仅仅是一个伪客户端,不会对请求做任何的处理
  3. 是通过注解方式实现Restful请求的

OpenFeign 与 Ribbon

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

Spring Cloud OpenFeign

  1. 添加依赖
  1. <dependency>
  2. <groupId>org.springframework.cloud</groupId>
  3. <artifactId>spring-cloud-starter-openfeign</artifactId>
  4. </dependency>
  5. <dependency>
  6. <groupId>org.springframework.cloud</groupId>
  7. <artifactId>spring-cloud-starter-loadbalancer</artifactId>
  8. </dependency>
  1. 定义接口,controller调用
  1. /**
  2. * 描述: feign接口, 参见提供端controller
  3. * @author Vic.xu
  4. * @date 2023-10-20 16:43
  5. */
  6. @FeignClient(value = "depart-provider", path = "/provider/depart")
  7. public interface DepartService {
  8. @PostMapping("/save")
  9. boolean save(@RequestBody Depart depart);
  10. @DeleteMapping("/delete/{id}")
  11. boolean delete(@PathVariable("id") int id);
  12. @PutMapping("/update")
  13. boolean update(@RequestBody Depart depart);
  14. @GetMapping("/get/{id}")
  15. Depart get(@PathVariable("id") int id);
  16. @GetMapping("/list")
  17. List<Depart> list();
  18. }
  1. 开启

@EnableFeignClients

一些配置:

  1. # default is global, can be changed to a real feignName
  2. spring.cloud.openfeign.client.config.default.connectTimeout=5000
  3. spring.cloud.openfeign.client.config.default.readTimeout=1

feign对请求和响应进行压缩

  1. spring.cloud.openfeign.compression.request.enabled=true
  2. spring.cloud.openfeign.compression.request.mime-types=text/xml,application/xml,application/json
  3. spring.cloud.openfeign.compression.request.min-request-size=2048

OpenFeign 底层实现技术选型

  • 默认httpclientspring.cloud.openfeign.httpclient.enabled=true
  • 支持httpclient5:spring.cloud.openfeign.httpclient.hc5.enabled=false
  • 也支持okhttp: spring.cloud.openfeign.okhttp.enabled=false

feign 的远程调用底层实现技术默认采用的是jdkURLConnection,同时还支持HttpclientOkhttp

由于jdkURLConnection不支持连接池,通信效率很低,所以生产中是不会使用该默认实现的,所在在spring cloud OpenFeign中直接将默认实现该为HttpClient,同是也支持OkHttp.

负载均衡以及策略更换

  • Client feignClient: If Spring Cloud LoadBalancer is on the classpath, FeignBlockingLoadBalancerClient is used. If none of them is on the classpath, the default feign client is used.

  • 加上LoadBalancer 依赖后默认负载均衡(轮询的方式)

eg: 配置随机策略:

  • 启动类上加:@LoadBalancerClients(defaultConfiguration = DepartConfig.class)

  • 编写随机策略:

    1. public class DepartConfig {
    2. private static final Logger LOGGER = LoggerFactory.getLogger(DepartConfig.class);
    3. /**
    4. * 配置随机均衡策略
    5. */
    6. @Bean
    7. public ReactorLoadBalancer<ServiceInstance> randomLoadbalancer(Environment environment, LoadBalancerClientFactory factory){
    8. //获取负载均衡客户端名称,即提供者服务名称
    9. String name = environment.getProperty(LoadBalancerClientFactory.PROPERTY_NAME);
    10. ObjectProvider<ServiceInstanceListSupplier> lazyProvider =
    11. factory.getLazyProvider(name, ServiceInstanceListSupplier.class);
    12. LOGGER.info("获取负载均衡客户端名称,即提供者服务名称:{}", name);
    13. //从所有provider实例中指定名称的实例列表中随机选择一个实例
    14. // 参数1:获取指定名称的所有provider实例列表
    15. //参数2:指定要获取的provider实例名称
    16. return new RandomLoadBalancer(lazyProvider, name);
    17. }
    18. }
  • ReactorLoadBalancer相关实现类:随机、轮询、nacos

所以可以直接使用rpc框架:dubbo,均衡策略更丰富


发表评论

目录