• Spring Cloud Feign 调用微服务传递header请求头


    package com.chitic.module.core.config;
    
    import feign.RequestInterceptor;
    import org.springframework.context.annotation.Bean;
    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 FeignConfig {
    
        @Bean
        public RequestInterceptor headerInterceptor() {
            return template -> {
                ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
                if (null != attributes) {
                    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);
                        }
                    }
                }
            };
        }
    
    }

    需注意,feign调用时不能调用含有HttpServletResponse参数(比如常用的数据导出),以下就不能远程调用,目前没找到解决办法

  • 相关阅读:
    7,MongoDB 之 Limit 选取 Skip 跳过 Sort 排序
    Python的while else
    linux route
    NCO
    rand和randn
    vi常用操作
    Linux常用命令
    Jmeter的NON-GUI模式
    Linux下安装jdk&Jmeter
    MySql安装完成后,Navicat连接不上的问题
  • 原文地址:https://www.cnblogs.com/gaomanito/p/12515526.html
Copyright © 2020-2023  润新知