Springboot feign 传递request信息
基础实现
requestInterceptor 实现类中添加信息
public class NativeFeignConf {
@Bean
public RequestInterceptor getRequestInterceptor(){
return new RequestInterceptor() {
@Override
public void apply(RequestTemplate requestTemplate) {
ServletRequestAttributes servletRequestAttributes=(ServletRequestAttributes)RequestContextHolder.currentRequestAttributes();
HttpServletRequest req=servletRequestAttributes.getRequest();
Map<String,Collection<String>> headerMap=new HashMap();
//获取你需要传递的头信息
String myHeaderVal=req.getHeader("myHeader");
headerMap.put("myHeader",Arrays.asList(myHeaderVal));
//feign请求时,便可携带上该信息
requestTempalte.headers(headerMap);
}
}
feign加入该config
@FeignClient(
value="target-service",
configuration=NativeFeignConf.class //加入该配置
)
public interface MyFeign(){
...
}
开启 Hystrix 的情况下
开启hystrix后,feign请求,会运行在hystrix管理的另一线程下。
所以RequestContextHolder.currentRequestAttributes()无法获取值。
解决方法:
创建一个自定义的hystrix 线程策略, 将servletRequestAttributes传入新线程中,并赋给RequestContextHolder:
public class MyHystrixConcurrencyStrategy extends HystrixConcurrencyStrategy{
@Override
public <T> Callable<T> wrapCallable(Callable<T> callable){
ServletRequestAttributes servletRequestAttributes=(ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
return new Callable<T>() {
@Override
public T call() throws Exception {
try {
if (null != servletRequestAttributes) {
RequestContextHolder.setRequestAttributes(servletRequestAttributes);
}
return callable.call();
}finally {
RequestContextHolder.resetRequestAttributes();
}
}
};
}
}
注册该策略
@Configuration
public class HystrixConfig{
@PostConstruct
public void init(){
HystrixPlugins.getInstance().registerConcurrencyStrategy(
new MyHystrixConcurrencyStrategy()
)
}
}
结束。
Springboot feign 传递request信息的更多相关文章
- spring cloud fegin传递request header
本文链接:https://blog.csdn.net/zhongzunfa/article/details/82791903 1.概述 今天一个朋友, 遇到一个如何在使用spring cloud fe ...
- python 抓取request信息,各种cookie,user-agent类的信息,只调试到http可以抓取,https貌似不行。
import pcap # 安装的是pypcap,本博客有安装方法,不过也比较乱,试试吧.import dpktimport socketimport datetime def sniffer(str ...
- ASP.NET Core 如何记录每次请求的Request信息 - sky 胡萝卜星星 - CSDN博客
原文:ASP.NET Core 如何记录每次请求的Request信息 - sky 胡萝卜星星 - CSDN博客 版权声明:本文为starfd原创文章,转载请标明出处. https://blog.csd ...
- springboot 统一管理异常信息
新建ResponseEntityExceptionHandler的继承类:(依然,需要入口类扫描) /** * @author sky * @version 1.0 */ @ControllerAdv ...
- 用List传递学生信息
集合在程序开发中经常用到,例如,在业务方法中将学生信息.商品信息等存储到集合中,然后作为方法的返回值返回给调用者,以此传递大量的有序数据. 本实例将使用List集合在方法之间传递学生的信息.实例效果如 ...
- Springboot 2.返回cookies信息的get接口开发 和 带cookis去请求
首先要有一个启动类,默认的启动类的名字就是Application.java.启动的时候直接右键点击run就可以 Application.java: import org.springframework ...
- jsonp跨域实现单点登录,跨域传递用户信息以及保存cookie注意事项
网站A:代码:网站a的login.html页面刷新,使用jsonp方式将信息传递给b.com的login.php中去,只需要在b.com中设置一下跨域以及接收参数,然后存到cookei即可, 注意:网 ...
- recess----2.Controller里面取用request信息
事实上,第一个APP里面除了没有model,其它的都有用过了,但是需要单独拎出来看看清楚. Recess框架里面的controller就是一个典型的MVC框架的controller,它负责处理从浏览器 ...
- 洛谷P2661 信息传递==coedevs4511 信息传递 NOIP2015 day1 T2
P2661 信息传递 题目描述 有n个同学(编号为1到n)正在玩一个信息传递的游戏.在游戏里每人都有一个固定的信息传递对象,其中,编号为i的同学的信息传递对象是编号为Ti同学. 游戏开始时,每人都只知 ...
随机推荐
- python的requests用法详解
Requests是一个Python语言写的http相关设置或者请求的一个库 安装:pip install Requests或者pip3 install requests 使用的时候要import re ...
- redis 其他操作
1.设定服务端密码 1.1.编辑 redis的配置文件 [root@centos7 ~]# vim /usr/local/redis/etc/redis.conf requirepass 123 # ...
- 三分钟网络基础-ARP协议
什么是 ARP 协议 地址解析协议 ARP (Address Resolution Protocal):在同一局域网下,根据已知道的主机或路由器的 IP 地址,找出其相应的硬件地址. 高速缓存 每一个 ...
- 国产CPU 申威1621 异数OS基础组件理论性能测试报告
国产CPU 申威1621 异数OS基础组件理论性能测试报告 文章目录 国产CPU 申威1621 异数OS基础组件理论性能测试报告 前言 测试平台 测试项目 SW1621 异数OS 容器虚拟交换机模拟性 ...
- Creating Form Elements --Using BeginForm and EndForm 使用内建的Form辅助器方法 精通ASP.NET MVC 5
Using the BeginForm and EndForm Helper Methods in the CreatePerson.cshtml File
- 19_08_26校内训练[Max]
题意 求$max_{l \leq r}{\{min{\{a_l,a_{l+1},...,a_r\}}*(r-l+1)\}}$ 思考 分治,考虑一个区间跨过某个点的贡献即可. 代码 #include&l ...
- 爬虫之pyspider 安装
解决方法: 利用wheel安装 S1: pip install wheelS2: 进入www.lfd.uci.edu/~gohlke/pythonlibs/,Ctrl + F查找pycurl S3:这 ...
- linux下误清用户/home下的文件怎么办?
2016-08-19 10:38:10 有时候我们不小心把home目录下的用户目录删除了,出现上图情况,每行开头直接变成-bash-3.2$这种形式而不是[lyp@centos7 ~]$这种,这时 ...
- H5 + WebGL 展示的3D无人机
前言 近年来,无人机的发展越发迅速,既可民用于航拍,又可军用于侦察,涉及行业广泛,也被称为“会飞的照相机”.但作为军事使用,无人机的各项性能要求更加严格.重要.本系统则是通过 Hightopo 的 ...
- Oracle数据库、实例、用户、表空间、表之间的关系
完整的Oracle数据库通常由两部分组成:Oracle数据库和数据库实例. 1) 数据库是一系列物理文件的集合(数据文件,控制文件,联机日志,参数文件等): 2) Oracle数据库实例则是一组Ora ...