一、 yml配置文件
二、启动类配置注解
@EnableDiscoveryClient
// 填写服务提供方的client的包名
@EnableFeignClients(basePackages = {"xxx.xxx.xx"})
三、Feign声明式调用接口
@Component @FeignClient(value = "调用服务名", configuration = FeignInterceptor.class) public interface IDemoFeign { @RequestMapping(path = "接口相对地址", method = RequestMethod.GET, produces = "application/json") 返回内容 接口方法(); }
四、自定义请求拦截器,在请求前设置header等内容
public class FeignInterceptor implements RequestInterceptor { @Override public void apply(RequestTemplate requestTemplate) { ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes(); HttpServletRequest request = attributes.getRequest(); Enumeration<String> headerNames = request.getHeaderNames(); while (headerNames.hasMoreElements()){ String headerName = headerNames.nextElement(); String headerValue = request.getHeader(headerName); requestTemplate.header(headerName,headerValue); } } }