栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

6、openFeign自定义拦截器

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

6、openFeign自定义拦截器

这里的拦截器不是我们mvc的拦截器,springmvc的拦截器是在我们客户端发送请求到服务端消费端,在这个发送中起的作用。而Feign是当服务端去调用我们服务提供方的时候,在这个请求过程中起的作用。

全局配置 第一步:新建拦截器
package com.example.order.interceptor;

import feign.RequestInterceptor;
import feign.RequestTemplate;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class CustomFeignInterceptor implements RequestInterceptor {
    Logger logger = LoggerFactory.getLogger(this.getClass());
    public void apply(RequestTemplate requestTemplate) {
        //这里可以记录日志,授权认证
        requestTemplate.header("xxxx","xxxx");
        logger.info("feign拦截器");
    }
}

第二步:修改FeignConfig
package com.example.order.config;

import com.example.order.interceptor.CustomFeignInterceptor;
import feign.Logger;
import feign.Request;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;


@Configuration
public class FeignConfig {

    @Bean
    public Logger.Level feignLoggerLevel(){
        return Logger.Level.FULL;
    }

    
//    @Bean
//    public Contract feignContract(){
//        return new Contract.Default();
//    }


    
    @Bean
    public Request.Options options(){
        return new Request.Options(5000,10000);
    }

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


第三步:修改配置文件application.yml

主要是修改了product的请求日志级别

server:
  port: 8086
#应用名称(nacos会将该名称当做服务名称)
spring:
  application:
    name: order-service
  cloud:
    nacos:
      server-addr: 127.0.0.1:8848
      discovery:
        username: nacos
        password: nacos
        namespace: public
  # 因为feign调试日志是debug级别输出,springboot默认的日志级别是info,所以feign的debug日志级别就不会输出
  # logging.level=debug这样配置是对所有的日志级别进行配置
  # 该场景只需要对feign接口进行debug配置,所以是这样配置logging.level.com.example.order.feign=debug
logging:
  level:
    com.example.order.feign: debug
feign:
  client:
    config:
      # 提供方的服务名
      product-service:
        #请求日志级别
        loggerLevel: FULL
        contract: feign.Contract.Default #设置为默认的契约(还原成原生注解)
        # 连接超时时间,默认2s,设置单位为毫秒
#        connectTimeout: 5000
#        # 请求处理超时时间,默认5s,设置单位为毫秒。
#        readTimeout: 3000

第四步:启动测试

访问:http://localhost:8086/order/add

局部配置 第一种方式:通过配置类 第二种方式:通过配置文件
server:
  port: 8086
#应用名称(nacos会将该名称当做服务名称)
spring:
  application:
    name: order-service
  cloud:
    nacos:
      server-addr: 127.0.0.1:8848
      discovery:
        username: nacos
        password: nacos
        namespace: public
  # 因为feign调试日志是debug级别输出,springboot默认的日志级别是info,所以feign的debug日志级别就不会输出
  # logging.level=debug这样配置是对所有的日志级别进行配置
  # 该场景只需要对feign接口进行debug配置,所以是这样配置logging.level.com.example.order.feign=debug
logging:
  level:
    com.example.order.feign: debug
feign:
  client:
    config:
      # 提供方的服务名
      product-service:
        #请求日志级别
        loggerLevel: FULL
        contract: feign.Contract.Default #设置为默认的契约(还原成原生注解)
        #配置拦截器
        requestInterceptors[0]: com.example.order.interceptor.CustomFeignInterceptor
        # 连接超时时间,默认2s,设置单位为毫秒
#        connectTimeout: 5000
#        # 请求处理超时时间,默认5s,设置单位为毫秒。
#        readTimeout: 3000

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/697240.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号