当前位置:网站首页>通过Feign在服务之间传递header请求头信息
通过Feign在服务之间传递header请求头信息
2022-04-23 15:57:00 【aserendipper】
问题背景
paas服务发生异常日志报警时,需要在paas服务中把对应header信息打印出来,由于接口请求的是saas服务,所以saas调用paas时需要把hearer传递过去。
解决办法
新增Feign的配置类,实现RequestInterceptor接口中的apply方法。
@Slf4j
public class FeignConfig implements RequestInterceptor {
@Override
public void apply(RequestTemplate requestTemplate) {
HttpServletRequest httpServletRequest = this.getHttpServletRequest();
log.info("RequestTemplate.headers:" + JSON.toJSONString(requestTemplate.headers()));
if (null != httpServletRequest) {
Map<String, String> headers = getHeaders(httpServletRequest);
for (Map.Entry<String, String> entry : headers.entrySet()) {
requestTemplate.header(entry.getKey(), entry.getValue());
}
}
}
private HttpServletRequest getHttpServletRequest() {
try {
return ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
private Map<String, String> getHeaders(HttpServletRequest request) {
Map<String, String> map = new LinkedHashMap<>();
Enumeration<String> enumeration = request.getHeaderNames();
if (enumeration != null) {
while (enumeration.hasMoreElements()) {
String key = enumeration.nextElement();
String value = request.getHeader(key);
map.put(key, value);
}
}
return map;
}
}
版权声明
本文为[aserendipper]所创,转载请带上原文链接,感谢
https://blog.csdn.net/qq_39234967/article/details/116995505
边栏推荐
- How do you think the fund is REITs? Is it safe to buy the fund through the bank
- MySQL Cluster Mode and application scenario
- s16. One click installation of containerd script based on image warehouse
- R语言中绘制ROC曲线方法二:pROC包
- [self entertainment] construction notes week 2
- MySQL - execution process of MySQL query statement
- IronPDF for . NET 2022.4.5455
- CAP定理
- 运维流程有多重要,听说一年能省下200万?
- 5分钟,把你的Excel变成在线数据库,神奇的魔方网表excel数据库
猜你喜欢
随机推荐
s16. One click installation of containerd script based on image warehouse
Partitionby of spark operator
Tencent offer has been taken. Don't miss the 99 algorithm high-frequency interview questions. 80% of them are lost in the algorithm
One brush 314 sword finger offer 09 Implement queue (E) with two stacks
Vision of building interstellar computing network
Day (8) of picking up matlab
撿起MATLAB的第(9)天
Application of Bloom filter in 100 million flow e-commerce system
Implement default page
Sortby use of spark operator
C language --- advanced pointer
Spark 算子之coalesce与repartition
Redis主从复制过程
MySQL optimistic lock to solve concurrency conflict
Fastjon2 here he is, the performance is significantly improved, and he can fight for another ten years
dlopen/dlsym/dlclose的简单用法
Large factory technology implementation | industry solution series tutorials
王启亨谈Web3.0与价值互联网“通证交换”
Leetcode-374 guess the size of the number
Day (9) of picking up matlab