当前位置:网站首页>通过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
边栏推荐
猜你喜欢
Sortby use of spark operator
WPS brand was upgraded to focus on China. The other two domestic software were banned from going abroad with a low profile
Master vscode remote GDB debugging
Method 2 of drawing ROC curve in R language: proc package
Filter usage of spark operator
Ice -- source code analysis
负载均衡器
Redis master-slave replication process
Config learning notes component
Treatment of idempotency
随机推荐
Coalesce and repartition of spark operators
Accumulation of applet knowledge points
MySQL Cluster Mode and application scenario
【现代电子装联期末复习要点】
mysql乐观锁解决并发冲突
Sortby use of spark operator
One brush 312 - simple repetition set - Sword finger offer 03 Duplicate number in array (E)
Application of Bloom filter in 100 million flow e-commerce system
vim指定行注释和解注释
Upgrade MySQL 5.1 to 5.611
Neodynamic Barcode Professional for WPF V11.0
多级缓存使用
Spark 算子之distinct使用
Import address table analysis (calculated according to the library file name: number of imported functions, function serial number and function name)
[self entertainment] construction notes week 2
Spark 算子之filter使用
Jour (9) de ramassage de MATLAB
Day (9) of picking up matlab
ESP32编译环境的搭建
实现缺省页面