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

简介: 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);

更多内容请关注:

https://qicaitun.com/

相关文章
|
6月前
|
Java Spring
Spring Cloud OpenFeign 远程调用传递请求头信息
import feign.RequestInterceptor; import feign.RequestTemplate; import lombok.extern.slf4j.Slf4j; import org.springframework.util.Assert; import org.springframework.web.context.request.RequestContextHolder; import org.springframework.web.context.request.ServletRequestAttributes;
189 0
|
11月前
|
API 微服务 容器
微服务组件之OpenFeign配置信息及RequestInterceptor请求拦截器
OpenFeign配置信息及RequestInterceptor请求拦截器
|
Java Spring
Spring Cloud feign GET请求无法用实体传参的解决方法
Spring Cloud feign GET请求无法用实体传参的解决方法
147 0
|
Java Spring
Spring Cloud学习 之 Spring Cloud Ribbon 重试机制及超时设置不生效
Spring Cloud学习 之 Spring Cloud Ribbon 重试机制及超时设置不生效
531 1
|
存储 资源调度 SpringCloudAlibaba
Openfeign客户端与feign客户端区别|学习笔记
快速学习Openfeign客户端与feign客户端区别
372 0
|
缓存 负载均衡 前端开发
SpringCloud之OpenFeign实现服务间请求头数据传递(OpenFeign拦截器RequestInterceptor的使用)
SpringCloud之OpenFeign实现服务间请求头数据传递(OpenFeign拦截器RequestInterceptor的使用)
2334 0
SpringCloud之OpenFeign实现服务间请求头数据传递(OpenFeign拦截器RequestInterceptor的使用)
|
负载均衡 前端开发 Java
Spring Cloud Alibaba-Feign实现远程HTTP调用
Spring Cloud Alibaba-Feign实现远程HTTP调用
Spring Cloud Alibaba-Feign实现远程HTTP调用
|
Java Spring
Spring Cloud Ribbon 全解 (7) - SpringCloud环境下纯Ribbon(不包含Eureka)重试配置 干货满满张哈希
Spring Cloud Ribbon 全解 (7) - SpringCloud环境下纯Ribbon(不包含Eureka)重试配置 干货满满张哈希
Spring Cloud Ribbon 全解 (7) - SpringCloud环境下纯Ribbon(不包含Eureka)重试配置 干货满满张哈希
|
负载均衡 Java Apache
Spring Cloud Gateway-ServerWebExchange核心方法与请求或者响应内容的修改(下)
本文编写的时候使用的Spring Cloud Gateway版本为当时最新的版本Greenwich.SR1。
932 0
Spring Cloud Gateway-ServerWebExchange核心方法与请求或者响应内容的修改(下)
|
存储 JSON 负载均衡
Spring Cloud Gateway-ServerWebExchange核心方法与请求或者响应内容的修改(上)
本文编写的时候使用的Spring Cloud Gateway版本为当时最新的版本Greenwich.SR1。
1949 1