spring cloud 通过feign请求动弹设置请求头heades

说明:

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

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

1.通过拦截器配置,如果要区分进行判断就行,然后注册一下拦截器就可以使用了。

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

}

 2.使用

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

PS:

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

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

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

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

3.使用固定headers ,@RequestMapping或其派生注解的header属性中解析Header的,并且Header的key和value需要用“=”进行分割。

    @GetMapping(value = "/api/clusterManager/listTopics",
        headers = {"cache-control=no-cache", "username=wangyong@xxx.com"})
    TopicsResponse listTopics(@RequestParam("clusterName") String clusterName,
                              @RequestParam("clusterArea") String clusterArea);

更多内容请关注:

欢迎您欢迎您icon-default.png?t=L9C2https://qicaitun.com/

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

风水道人

写作不易且行且珍惜

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值