【发布时间】:2021-03-15 08:39:48
【问题描述】:
我是 Spring-cloud-openfeign 的新手。我正在尝试构建一个基于微服务的系统。为了让事情看起来更简单,只需使用其中的 3 个服务,即网关、服务 A、服务 B。当我通过网关从我的前端向 Service-A 发出请求时,网关将验证请求中的 JWT 令牌,并从令牌中提取用户信息(userId)并将作为请求头放入请求并将其转发给服务-A。现在 Service-A 将通过 feign-client 调用 service-B。现在,在通过 feign-client 进行的服务间调用期间,我正尝试通过 Feign RequestIterceptor 将用户 ID 从 Serice-A 中的当前请求转发到服务 B 的传出请求。但我无法在拦截器中检索当前请求。我尝试了stack_over_question 中的解决方案,但它似乎不起作用。我想我面临与github_issue 相同的问题。我可以看到一些博客和文章建议使用 RequestContextListner 或 RequestContextFilter 从 Dispatcher-Servlet 中获取当前请求,但我不知道如何使用或实现它。以下是我目前使用的代码
@Component
public class ProxyInterceptor implements RequestInterceptor {
private final static Logger LOGGER = LoggerFactory.getLogger(ProxyInterceptor.class);
@Override
public void apply(RequestTemplate requestTemplate) {
HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes())
.getRequest();
String userId = request.getHeader("userId");
LOGGER.info("userId {}", userId);
if (Optional.ofNullable(userId).isPresent()) {
requestTemplate.header(USER_ID_REQUEST_HEADER_VARIABLE, userId);
}
}
}
依赖关系
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-circuitbreaker-resilience4j</artifactId>
</dependency>
Hoxton.SR8
此代码引发 NullPointer 异常,因为 RequestContextHolder.getRequestAttributes() 返回 null。知道如何解决这个问题吗?
【问题讨论】:
标签: java spring spring-boot microservices openfeign