当前位置:网站首页>通过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
边栏推荐
- One brush 314 sword finger offer 09 Implement queue (E) with two stacks
- 捡起MATLAB的第(6)天
- Single architecture system re architecture
- 捡起MATLAB的第(10)天
- Why disable foreign key constraints
- Filter usage of spark operator
- IronPDF for . NET 2022.4.5455
- C language self compiled string processing function - string segmentation, string filling, etc
- One brush 313 sword finger offer 06 Print linked list from end to end (E)
- C语言自编字符串处理函数——字符串分割、字符串填充等
猜你喜欢

建设星际计算网络的愿景

mysql乐观锁解决并发冲突

MySQL - MySQL查询语句的执行过程

Best practices of Apache APIs IX high availability configuration center based on tidb

Cap theorem

Do we media make money now? After reading this article, you will understand

MySQL optimistic lock to solve concurrency conflict

王启亨谈Web3.0与价值互联网“通证交换”

Treatment of idempotency

Distinct use of spark operator
随机推荐
Compile, connect -- Notes
Merging of Shanzhai version [i]
gps北斗高精度卫星时间同步系统应用案例
Leetcode-396 rotation function
String sorting
多生成树MSTP的配置
捡起MATLAB的第(2)天
mysql乐观锁解决并发冲突
Spark 算子之filter使用
捡起MATLAB的第(10)天
Method 2 of drawing ROC curve in R language: proc package
IronPDF for . NET 2022.4.5455
MySQL Cluster Mode and application scenario
Simple usage of dlopen / dlsym / dlclose
Spark 算子之交集、并集、差集
The principle and common methods of multithreading and the difference between thread and runnable
MetaLife与ESTV建立战略合作伙伴关系并任命其首席执行官Eric Yoon为顾问
【开源工具分享】单片机调试助手(示波/改值/日志) - LinkScope
JVM - Chapter 2 - class loader subsystem
Cap theorem