zoukankan      html  css  js  c++  java
  • spring cloud 通过feign请求设置请求头

    本文为博主原创,转载请注明出处:

      spring cloud 服务组件之间通过feign 的方式请求,会携带很少的基础类型的消息头参数,比如Content-Type等,但不会携带自定义或指定的请求头参数,

    在实际的开发过程中,需要对从网关或其他服务组件使用feign请求时,携带原始请求的请求头,并做一些基础校验和业务校验等。

      1.如果要在服务使用feign请求过程中,携带请求的原始请求头信息时,需要是请求处于同一个线程,这样才能在使用feign请求时,才能获取到当前请求的

    原始请求头。在使用feignClient请求时,默认是会新建线程,去执行服务请求,如果新建线程,则会解析不到原始请求的请求头。需要对hystrix 进行一下的配置,

    才能保证处于同一个线程当中。

    hystrix默认使用多线程管理请求连接池,从主线程到发送基于hystrix的feign请求线程已不在同一个线程内

    hystrix:
      command:
        default:
          execution:
            timeout:
              enabled: true
            isolation:
              strategy: SEMAPHORE
              thread:
                timeoutInMilliseconds: 60000

    添加以上配置,可以对feignClient 进行消息头的配置,

      2.由于feign 请求底层是通过 RestTemplate 进行请求,Feign 支持请求拦截器,在发送请求前,可以对发送的模板进行操作,例如设置请求头等属性,

    自定请求拦截器需要实现 feign.RequestInterceptor 接口,该接口的方法 apply 有参数 template ,该参数类型为 RequestTemplate,我们可以根据实际情况对请求信息进行调整,

     对消息头的封装,可以使用拦截器进行一下的封装:

    package com.imooc.homepage.config;
    import feign.RequestInterceptor;
    import feign.RequestTemplate;
    import org.springframework.context.annotation.Configuration;
    import org.springframework.web.context.request.RequestContextHolder;
    import org.springframework.web.context.request.ServletRequestAttributes;
    
    import javax.servlet.http.HttpServletRequest;
    import java.util.Enumeration;
    
    @Configuration
    public class FeignConfiguration  implements RequestInterceptor{
        @Override
        public void apply(RequestTemplate template) {
            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);
                    template.header(name, values);
                }
            }
            // 对请求体进行配置
            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());
            }
        }
    
    }

    3.对FeignClient添加以上配置,进行请求:

    @FeignClient(value = "client-homepage-course", fallback = CourseClientHystrix.class,configuration = FeignConfiguration.class)
    public interface CourseClient {
    
        @RequestMapping(value = "/homepage-course/get/courses", method = RequestMethod.POST)
        List<CourseInfo> getCourseInfos(@RequestBody CourseInfosRequest request);
    }

      在服务中配置 FeignConfiguration 时,上面的代码示例是通过 RequestContextHolder.getRequestAttributes() 解析到当前请求头的参数,

    RequestContextHolder是基于ThreadLocal实现的,所以需要保证请求是一直处于同一个线程当中,hystrix强大在于是支持此扩展操作的。

      另一种解决方案:由于所解析的模块没有添加spring-web相关的依赖配置,无法使用 RequestContextHolder ,使用 过滤器过滤出当前

    线程中的请求头,并保存到ThreadLocal 中,在 FeignConfiguration 中解析 保存在ThreadLocal中的消息头参数,然后设置消息头给RestTemplate.  

  • 相关阅读:
    打破国外垄断,开发中国人自己的编程语言(2):使用监听器实现计算器
    寒假arcpy arcgis python培训通知
    分户图制作工具
    分户图制作工具
    Python3操作AutoCAD
    arcgis更新注记要素类
    arcgis field for cad
    为什么中小学培训这么火,怎么打击也收效甚微?
    arcgis arcmap使用 Python 加载 CAD 数据
    神仙渡劫
  • 原文地址:https://www.cnblogs.com/zjdxr-up/p/14152438.html
Copyright © 2011-2022 走看看