• 微服务组件Feign


    Feign组件
    是Netflix开发的声明试、模板化的HTTP客户端,更加便捷、优雅的调用HTTP的API;内部集成了Ribbon,与之不同的是,feign只需要定义服务绑定接口且声明的方法,实现服务的调用。
    spring Cloud 在Feign的基础上实现了OpenFeign支持了Spring mvc的注解。
    在这里插入图片描述

    Feign依赖

    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-feign</artifactId>
    </dependency>
    
    • 1
    • 2
    • 3
    • 4

    OpenFeign依赖

      <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-openfeign</artifactId>
        </dependency>
    
    • 1
    • 2
    • 3
    • 4

    二:简单使用实战

    @FeignClient(value = "mall-order",path = "/order")
    public interface OrderFeignService {
    
        @RequestMapping("/findOrderByUserId/{userId}")
        R findOrderByUserId(@PathVariable("userId") Integer userId);
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    在启动类添加注解:

    @EnableFeignClients 
    
    • 1

    三:拦截器配置
    通常我们调用接口都是有权限控制的。
    3.1Basic认证

        @Bean
        public BasicAuthRequestInterceptor basicAuthRequestInterceptor() {
            return new BasicAuthRequestInterceptor("Authorization", "123456");
        }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5

    3.2RequestInterceptor认证(推荐)
    每次feign发起HTTP调用之前,都会执行拦截器中的逻辑

    public class FeignAuthRequestInterceptor implements RequestInterceptor {
        @Override
        public void apply(RequestTemplate template) {
            // 业务逻辑  模拟认证逻辑
            String access_token = UUID.randomUUID().toString();
            template.header("Authorization",access_token);
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    3.3在yaml配置文件中配置

    feign:
      client:
        config:
         mall-order:
          requestInterceptors[0]:  #配置拦截器
           com.**.***.interceptor.FeignAuthRequestInterceptor
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    四:超时时间配置

          @Bean
          public Request.Options options(){
              return new Request.Options(5000, 5000);
          }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5

    通过 Options 可以配置连接超时时间和读取超时时间,Options 的第一个参数是连接的超时时间(ms), 默认值是 2s;第二个是请求处理的超时时间(ms),默认值是 5s。
    Feign的底层用的是Ribbon,但超时时间以Feign配置为准

    五:从Open Feign迁移到Dubbo
    使用 @DubboTransported 注解,就可以实现 从 openFeign 迁移到 Dubbo

    @FeignClient(value = "mall-order",path = "/order")
    @DubboTransported(protocol = "dubbo")
    public interface OrderFeignService {
    
        @RequestMapping("/findOrderByUserId/{userId}")
        R findOrderByUserId(@PathVariable("userId") Integer userId);
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
  • 相关阅读:
    腾讯云服务器可用区是什么意思?可用区详细说明
    Spring AOP的介绍与实现
    【kubernetes】关于k8s集群中的ingress规则案例
    全网独家首发!一份破解大厂面试官千层套路的Spring源码笔记
    python多线程返回值问题重写Thread类的run方法
    DASCTF X CBCTF 2022九月挑战赛 dino3d
    【专业技能】程序员的软件工程素养之画好 UML 时序图
    Spring Cloud 学习笔记(3 3)
    FPGA高端项目:FPGA基于GS2971+GS2972架构的SDI视频收发+图像缩放,提供3套工程源码和技术支持
    C++ 核心编程(2)
  • 原文地址:https://blog.csdn.net/qq_35529931/article/details/126680843