Spring Cloud Alibaba OpenFeign 自定义拦截

2021/11/8 23:14:28

本文主要是介绍Spring Cloud Alibaba OpenFeign 自定义拦截,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

 

package com.wsm.order.interceptor.feign;

import feign.RequestInterceptor;
import feign.RequestTemplate;

import java.util.UUID;

public class FeignAuthRequestInterceptor implements RequestInterceptor {
    @Override
    public void apply(RequestTemplate requestTemplate) {
        //业务逻辑
        String access_token = UUID.randomUUID().toString();
        requestTemplate.header("Authorization",access_token);
    }
}

 

package com.wsm.order.interceptor.feign;

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

    @Override
    public void apply(RequestTemplate requestTemplate) {
//        requestTemplate.header("xxx","xxx");
//        requestTemplate.query("id","11");
        requestTemplate.uri("/9");
        logger.info("feign拦截器!");
    }
}

 

package com.wsm.order.config;

import com.wsm.order.interceptor.feign.FeignAuthRequestInterceptor;
import feign.Contract;
import feign.Logger;
import feign.Request;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * 全局配置: 当使用@Configuration会将配置作用所有的报务提供方
 * 局部配置: 1 通过配置类:如果只针对个别服务进行配置,就不要加@Configuration
 *            2 通过配置文件
 */
//@Configuration
public class FeignConfig {

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

//    /**
//     * 修改契约配置,支持Feign原生的注解
//     * @return
//     */
//    @Bean
//    public Contract feignContract(){
//        return new Contract.Default();
//    }

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

//    /**
//     * 自定义拦截器
//     * @return
//     */
//    @Bean
//    public FeignAuthRequestInterceptor feignAuthRequestInterceptor() {
//        return new FeignAuthRequestInterceptor();
//    }

}

 

server:
  port: 8040
  #应用名称  (nacos 会将该名称当作服务名称)
spring:
  application:
    name: order-openfeign-service
  cloud:
    nacos:
#      server-addr: 127.0.0.1:8848
      server-addr: 192.168.133.128:8847  #集群 nginx 负载均衡访问 nacos
      discovery:
        username: nacos
        password: nacos
        namespace: public
#springboot 默认的日志级别是info,feign的debug日志级别就不会输出
logging:
  level:
#    com.wsm.order.feign: debug
    com.wsm.order.feign.StockFeignService: debug
# Feign 日志局部配置
feign:
  client:
    config:
      product-service:
        loggerLevel: BASIC
#        contract: feign.Contract.Default #设置为默认的契约 (还原成原生注解)
        connectTimeout: 5000 #连接超时时间,默认2s
        readTimeout: 10000 #请求处理超时时间,默认5s
        requestInterceptors[0]: com.wsm.order.interceptor.feign.CustomFeignInterceptor

 

 

 

 



这篇关于Spring Cloud Alibaba OpenFeign 自定义拦截的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程