自定义Feign拦截器
简介
Feign的拦截器RequestInterceptor
SpringCloud的微服务使用Feign进行服务间调用的时候可以使用RequestInterceptor统一拦截请求来完成设置header等相关请求,但RequestInterceptor和ClientHttpRequestInterceptor有点不同,它拿不到原本的请求,所以要通过其他方法来获取原本的请求
首先创建自定义的RequestInterceptor
1.搭建步骤
@Slf4j
@Component
public class MyFeignInterceptor implements RequestInterceptor {
@Override
public void apply(RequestTemplate template) {
ServletRequestAttributes servletRequestAttributes = (ServletRequestAttributes)RequestContextHolder.getRequestAttributes();
log.info("===request: {}", template.url());
template.header("aaaa", "bbbb");
}
}
1.1查看结果
INFO 1 --- [nio-8801-exec-6] c.m.d.client.controller.DemoController : header: aaaa, bbbb
INFO 1 --- [nio-8801-exec-6] c.m.d.client.controller.DemoController : header: accept, */*