• SpringCloud 之OpenFeign 自定义配置和使用/自定义拦截器


    什么是Feign

    Feign 是一个声明性的 Web 服务客户端。它使编写 Web 服务客户端变得更加容易。要使用Feign,请创建一个界面并对其进行注释。它具有可插入的注释支持,包括假装注释和 JAX-RS 注释。Feign 还支持可插拔编码器和解码器。春云增加了对弹簧MVC注释的支持,以及使用春季网页中默认使用的相同Http消息转换器的支持。弹簧云集成了尤里卡,弹簧云断路器,以及弹簧云负载平衡器,以便在使用Feign时提供负载平衡的http客户端。
    Spring Cloud Open Feign对Feign进行了增强,使其支持Spring MVC注解,另外还整合了Ribbon和Nacos,从而使得Feign更加方便.

    1.引入依赖

         
                <dependency>
                    <groupId>org.springframework.cloudgroupId>
                    <artifactId>spring-cloud-dependenciesartifactId>
                    <version>Hoxton.SR12version>
                    <type>pomtype>
                    <scope>importscope>
                dependency>
           
            <dependency>
                <groupId>org.springframework.cloudgroupId>
                <artifactId>spring-cloud-starter-openfeignartifactId>
            dependency>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13

    Feign接口案例

    1.创建一个Feign接口

    /**
     * 创建一个接口加上@FeignClient注解,声明这是一个Feign接口
     * name :指定调用rest接口所对应的服务名
     * path :指定调用的rest接口所在Controller指定的统一映射路径(一般很少用,因为可能多个方法调用的是不同的Controller的接口,可以直接在方法上指定接口的映射路径)
     */
    @FeignClient(name= "manage-client")
    public interface UserFeignServuce {
    
        @RequestMapping(value = "/index/test")
        String  test(String name);
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    2.注入feign接口,在消费端只需要像使用普通的方法一样,通过**@Autowired**注解注入接口,并调用传参就可以了

    
        @Autowired
        UserFeignServuce userFeignServuce;
    
        @RequestMapping(value = "/test",method = RequestMethod.POST)
        private CommonResult test(){
            String name = "OK";
            String key = userFeignServuce.test(name);
            return new CommonResult(key );
        }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    3.开启feign
    添加 @EnableFeignClients 注解

    @SpringBootApplication
    @EnableFeignClients    //开启Feign
    public class ManageApplication {
        public static void main(String[] args) {
            SpringApplication.run(ManageApplication.class,args);
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    OpenFeign的自定义配置和使用

    日志配置
    4种日志基本
    NONE, 不记录(默认)。
    BASIC, 只记录请求方法和 URL 以及响应状态码和执行时间。
    HEADERS, 记录基本信息以及请求和响应标头。
    FULL, 记录请求和响应的标头、正文和元数据。
    全局配置
    定义一个配置类,指定日志级别

    //加@Configuration注解为全局配置
    @Configuration
    public class FooConfiguration {
        @Bean
        Logger.Level feignLoggerLevel() {
            return Logger.Level.FULL;
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    指定feign全局的日志级别

    #SpringBoot默认的日志基本是info,feign的debug日志级别就不会输出
    logging:
      level:
        #feign接口的包路径
        com.saas.manage.feign: debug
    
    • 1
    • 2
    • 3
    • 4
    • 5

    局部配置

    feign:
      client:
        config:
          #需要配置的服务名称
          manage-client:
            #设置日志级别
            loggerLevel: FULL
            #契约配置  一般情况下是不会使用的  由于openFeign 是对 Feign的增强在Spring Cloud 1 里面有独立的一套注解,在Netflix闭源后,SpringCloud才切换到了Openfeign并支持Spring注解,如果在老项母使用Cloud 1架构的项目中是用了feign的原生注解,需要升级可下面的配置进行还原 例如:在feign原生注解中 @requestLine 来对应 @RequestMapping 
    #       contract: feign.Contract.Default #设置为默认的契约 (还原成原生注解)
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    超时时间配置
    配置类方式

    @Configuration
    public class FooConfiguration {
        //超时时间配置
        @Bean
        public Request.Options options() {
            return new Request.Options(5000,10000);
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    yml配置

    feign:
        client:
            config:
                feignName:
                    #防止由于服务器处理时间长而阻塞调用者
                    connectTimeout: 5000
                    #从连接建立时开始应用,在返回响应时间过长时触发
                    readTimeout: 5000
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    feign组件自定义拦截器

    自定义一个拦截器并实现 RequestInterceptor 接口

    import feign.RequestInterceptor;
    import feign.RequestTemplate;
    import lombok.extern.slf4j.Slf4j;
    
    /**
     * feign拦截器
     */
    @Slf4j
    public class CustomFeignInterceptor implements RequestInterceptor {
        @Override
        public void apply(RequestTemplate requestTemplate) {
            //TODO  根据需求可以在拦截器中扩展需要的功能
            log.info("比如记录日志 /增加参数 / 修改路径 / 鉴权");
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16

    源码

    在这里插入图片描述
    配置方式1 注入bean

        @Bean
        public CustomFeignInterceptor customFeignInterceptor(){
            return new CustomFeignInterceptor();
        }
    
    • 1
    • 2
    • 3
    • 4

    配置方式2yaml

    #开启sentinel对feign组件远程调用服务降级的支持
    feign:
      client:
        config:
          #需要配置的服务名称
          manage-client:
            loggerLevel: FULL
            #连接超时时间,默认2s
            connectTimeout: 5000
            #请求处理超时时间,默认5a
            readTimeout: 10000
            #拦截器配置
            requestInterceptors[0]: com.car.hailing.saas.manage.feign.intercptor.CustomFeignInterceptor
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
  • 相关阅读:
    求二叉树中最大的二叉搜索子树的头节点
    网站如何才能不被黑,如何做好网络安全
    NYOJ 106 背包问题 贪心算法
    A1078 Hashing(25分)PAT 甲级(Advanced Level) Practice(C++)满分题解【哈希表】
    进程互斥的实现方法
    LeetCode只出现一次的数字
    什么是CSS的外边距重叠?
    Java循环控制语句
    游戏模拟——Position based dynamics
    代码随想录day50:动态规划
  • 原文地址:https://blog.csdn.net/weixin_44137464/article/details/127325544