使用步骤
- 启动类中添加注解
@EnableFeignClients
开启feign扫描
@Slf4j
@SpringBootApplication
@EnableFeignClients
@MapperScan(basePackages = "cn.zhouxr.work.test001.mapper")
@ServletComponentScan
@EnableDiscoveryClient
@Import(SwaggerConf.class)
public class Test2Application {
public static void main(String[] args) {
SpringApplication.run(Test2Application.class,args);
log.info("\n ************************* Test2Application 启动成功!! *************************\n");
}
}
- 添加feign,
@FeignClient
的讲解往下看.
/**
* feignClient 参数说明
* url: 如果url不为空的话, 则会进行http调用 , 否则是微服务之间的内部调用
* path: 前缀
* configuration: 为当前feign添加的配置类
*
*/
@FeignClient(value = "testFeignClient",url = "http://127.0.0.1" , path = "/center/api")
public interface TestFeignClient {
@GetMapping("/evaluate/list")
Object list(@RequestParam("category") String category);
}
- 添加拦截器 实现
RequestInterceptor
接口
@Slf4j
public class MyInterceptor2 implements RequestInterceptor {
private static final String interceptionFeignName = "testFeignClient";
@Override
public void apply(RequestTemplate template) {
log.info("拦截器:{}",this.getClass().getName());
log.info(JSON.toJSONString(template));
String feignName = template.feignTarget().name();
if (StringUtils.equals(interceptionFeignName,feignName)){
template.header("aa","aaa");
}
}
}
多个拦截器之间的执行顺序
可以通过 @Order(2)
注解控制
@Slf4j
@Component
@Order(2)
public class myInterceptor2 implements RequestInterceptor {
@Override
public void apply(RequestTemplate template) {
log.info("拦截器:{}",this.getClass().getName());
log.info(JSON.toJSONString(template));
}
}