当前位置:网站首页>通过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
边栏推荐
猜你喜欢

Spark 算子之交集、并集、差集

Unity Shader学习

Website pressure measurement tools Apache AB, webbench, Apache jemeter

Jour (9) de ramassage de MATLAB

The principle and common methods of multithreading and the difference between thread and runnable

Codejock Suite Pro v20.3.0

C#,贝尔数(Bell Number)的计算方法与源程序

C, calculation method and source program of bell number

贫困的无网地区怎么有钱建设网络?

Load Balancer
随机推荐
MetaLife与ESTV建立战略合作伙伴关系并任命其首席执行官Eric Yoon为顾问
Jour (9) de ramassage de MATLAB
One brush 312 - simple repetition set - Sword finger offer 03 Duplicate number in array (E)
Deletes the least frequently occurring character in the string
Do we media make money now? After reading this article, you will understand
IronPDF for . NET 2022.4.5455
捡起MATLAB的第(4)天
Interview questions of a blue team of Beijing Information Protection Network
捡起MATLAB的第(8)天
Implement default page
leetcode-396 旋转函数
String sorting
Date date calculation in shell script
捡起MATLAB的第(10)天
多级缓存使用
5 minutes, turn your excel into an online database, the magic cube net table Excel database
[split of recursive number] n points K, split of limited range
捡起MATLAB的第(2)天
时序模型:门控循环单元网络(GRU)
保姆级Anaconda安装教程