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

SpringCloud 之OpenFeign 自定义配置和使用/自定义拦截器-图1

什么是Feign

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

1.引入依赖
     
            
                org.springframework.cloud
                spring-cloud-dependencies
                Hoxton.SR12
                pom
                import
            
       
        
            org.springframework.cloud
            spring-cloud-starter-openfeign
        
Feign接口案例

1.创建一个Feign接口

@FeignClient(name= "manage-client")
public interface UserFeignServuce {

    @RequestMapping(value = "/index/test")
    String  test(String name);
}

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 );
    }

3.开启feign
添加 @EnableFeignClients 注解

@SpringBootApplication
@EnableFeignClients    //开启Feign
public class ManageApplication {
    public static void main(String[] args) {
        SpringApplication.run(ManageApplication.class,args);
    }
}
OpenFeign的自定义配置和使用

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

//加@Configuration注解为全局配置
@Configuration
public class FooConfiguration {
    @Bean
    Logger.Level feignLoggerLevel() {
        return Logger.Level.FULL;
    }
}

指定feign全局的日志级别

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

局部配置

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 #设置为默认的契约 (还原成原生注解)

超时时间配置
配置类方式

@Configuration
public class FooConfiguration {
    //超时时间配置
    @Bean
    public Request.Options options() {
        return new Request.Options(5000,10000);
    }
}

yml配置

feign:
    client:
        config:
            feignName:
                #防止由于服务器处理时间长而阻塞调用者
                connectTimeout: 5000
                #从连接建立时开始应用,在返回响应时间过长时触发
                readTimeout: 5000
feign组件自定义拦截器

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

import feign.RequestInterceptor;
import feign.RequestTemplate;
import lombok.extern.slf4j.Slf4j;


@Slf4j
public class CustomFeignInterceptor implements RequestInterceptor {
    @Override
    public void apply(RequestTemplate requestTemplate) {
        //TODO  根据需求可以在拦截器中扩展需要的功能
        log.info("比如记录日志 /增加参数 / 修改路径 / 鉴权");
    }
}

源码


配置方式1 注入bean

    @Bean
    public CustomFeignInterceptor customFeignInterceptor(){
        return new CustomFeignInterceptor();
    }

配置方式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

转载请说明出处 内容投诉内容投诉
南趣百科 » SpringCloud 之OpenFeign 自定义配置和使用/自定义拦截器

南趣百科分享生活经验知识,是您实用的生活科普指南。

查看演示 官网购买