Feign传递请求头信息(Finchley版本)

2020-04-08  本文已影响0人  jeffrey_hjf

例子一:
Feign如何统一 添加header参数


@Configuration
public class FeignConfiguration {
    /**
     * 日志级别
     * @return
     */
    @Bean
    Logger.Level feignLoggerLevel() {
        return Logger.Level.FULL;
    }
 
    /**
     * 创建Feign请求拦截器,在发送请求前设置认证的token,各个微服务将token设置到环境变量中来达到通用
     * @return
     */
    @Bean
    public FeignBasicAuthRequestInterceptor basicAuthRequestInterceptor() {
        return new FeignBasicAuthRequestInterceptor();
    }
 
}
 
/**
 * Feign请求拦截器
 * @author jeffrey
 * @create 2018-11-10
 **/
public class FeignBasicAuthRequestInterceptor  implements RequestInterceptor {
 
    public FeignBasicAuthRequestInterceptor() {
 
    }
 
    @Override
    public void apply(RequestTemplate template) {
        template.header("Authorization", System.getProperty("fangjia.auth.token"));
    }
}

例子二:

public class HmilyFeignInterceptor implements RequestInterceptor {
 
    @Override
    public void apply(final RequestTemplate requestTemplate) {
        RpcMediator.getInstance().transmit(requestTemplate::header, HmilyTransactionContextLocal.getInstance().get());
    }
 
}
public void transmit(final RpcTransmit rpcTransmit, final HmilyTransactionContext context) {
        if (context.getRole() == HmilyRoleEnum.LOCAL.getCode()) {
            context.setRole(HmilyRoleEnum.INLINE.getCode());
        }
        rpcTransmit.transmit(CommonConstant.HMILY_TRANSACTION_CONTEXT,
                GsonUtils.getInstance().toJson(context));
    }

例子三:
设置请求头和请求body

public class FeignBasicAuthRequestInterceptor implements RequestInterceptor {
  @Override
  public void apply(RequestTemplate requestTemplate) {
    ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder
            .getRequestAttributes();
    HttpServletRequest request = attributes.getRequest();
    //设置请求头
    Enumeration<String> headerNames = request.getHeaderNames();
    if (headerNames != null) {
      while (headerNames.hasMoreElements()) {
        String name = headerNames.nextElement();
        String values = request.getHeader(name);
        requestTemplate.header(name, values);
      }
    }
    //设置请求body
    Enumeration<String> bodyNames = request.getParameterNames();
      StringBuffer body =new StringBuffer();
      if (bodyNames != null) {
          while (bodyNames.hasMoreElements()) {
            String name = bodyNames.nextElement();
            String values = request.getParameter(name);
            body.append(name).append("=").append(values).append("&");
          }
      }
     if(body.length()!=0) {
        body.deleteCharAt(body.length()-1);
        template.body(body.toString());
        logger.info("feign interceptor body:{}",body.toString());
    }
  }
}

注册配置

package com.jeffrey.ibase.personalConfigure.config;

import com.jeffrey.ibase.utils.FeignBasicAuthRequestInterceptor;
import com.jeffrey.ibase.utils.FeignSpringFormEncoder;
import feign.RequestInterceptor;
import feign.codec.Encoder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * Feign配置注册(全局)
 *
 * @author jeffrey
 * @create 2019-08-20 11:44
 **/
@Configuration
public class FeignSupportConfig {
  /**
   * feign请求拦截器
   *
   * @return
   */
  @Bean
  public RequestInterceptor requestInterceptor(){
    return new FeignBasicAuthRequestInterceptor();
  }
}

这个文件放在项目的扫描目录下,所有的feign调用都会使用此配置。如果只有某个feign调用则可以这样设置(但配置类不能在扫描目录下):

@FeignClient(name = "organ",path = "/organ/OrganInfo",configuration = FeignSupportConfig.class)
上一篇下一篇

猜你喜欢

热点阅读