feign远程调用丢失请求头
时间: 2023-11-08 16:05:50 浏览: 120
feign远程调用丢失请求头可能是因为Feign默认不会将请求头信息传递给下游服务,需要在Feign配置中开启对请求头的支持。可以通过在FeignClient注解中添加configuration属性,指定Feign的配置类来实现。
示例代码如下:
```java
@FeignClient(name = "example", configuration = FeignConfig.class)
public interface ExampleFeignClient {
// ...
}
```
在FeignConfig类中添加如下配置:
```java
@Configuration
public class FeignConfig {
@Bean
public RequestInterceptor requestInterceptor() {
return new RequestInterceptor() {
@Override
public void apply(RequestTemplate requestTemplate) {
ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
if (attributes != null) {
HttpServletRequest request = attributes.getRequest();
Enumeration<String> headerNames = request.getHeaderNames();
if (headerNames != null) {
while (headerNames.hasMoreElements()) {
String name = headerNames.nextElement();
String value = request.getHeader(name);
requestTemplate.header(name, value);
}
}
}
}
};
}
}
```
这样就可以将请求头信息传递给下游服务了。
阅读全文