Feign 支持请求拦截器,在发送请求前,能够对发送的模板进行操做,例如设置请求头等属性,自定请求拦截器须要实现 feign.RequestInterceptor 接口,该接口的方法 apply 有参数 template ,该参数类型为 RequestTemplate,咱们能够根据实际状况对请求信息进行调整,示例以下:java
建立自定义请求拦截器,在发送请求前增长了一个请求头信息,进行身份校验。app
具体代码参考以下:微服务
import feign.RequestInterceptor; import feign.RequestTemplate; public class MyRequestInterceptor implements RequestInterceptor{ public void apply(RequestTemplatetemplate){ template.header("Authorization","123"); } }
服务端能够经过HttpServletRequest获取到前面传递的参数,具体获取逻辑以下:code
RequestAttributes requestAttributes = RequestContextHolder.currentRequestAttributes(); if (requestAttributes != null) { HttpServletRequest request = ((ServletRequestAttributes) requestAttributes).getRequest(); request.getHeader("Authorization"); }
就实现了各个微服务之间参数的传递。 blog