EnableFeignClients扫描了com.yunduo.newbiz.client.feign包
@EnableDiscoveryClient @EnableAspectJAutoProxy(proxyTargetClass=true, exposeProxy=true) @EnableFeignClients(basePackages={"com.yunduo.im.client.feign", "com.yunduo.newbiz.client.feign", "com.yunduo.netty.client.feign"}) @EnableAsync //@EnableScheduling @SpringBootApplication(scanBasePackages = {"com.yunduo.common", "com.yunduo.im.server"}) public class AiOnlineApplication { public static void main(String[] args) { SpringApplication.run(AiOnlineApplication.class, args); } }com.yunduo.newbiz.client.feign包下的代码
@FeignClient( value = "netty-mode-service", fallbackFactory = NettyIMFeignClientFallback.class ) public interface NettyIMFeignClient { @RequestMapping( path = {"/v1/im/{clientId}"}, method = {RequestMethod.POST}, consumes = {"application/json"}, produces = {"application/json"} ) StatusDTO<Boolean> sendMsg(@PathVariable("clientId") String clientId, @RequestBody Object msg); }NettyIMFeignClientFallback下的代码
@Component public class NettyIMFeignClientFallback implements FallbackFactory<NettyIMFeignClient> { public NettyIMFeignClientFallback() { } public NettyIMFeignClient create(Throwable throwable) { return new NettyIMFeignClient() { public StatusDTO<Boolean> sendMsg(String clientId, Object msg) { return StatusDTO.buildFailure("Feign Error"); } }; } }1 SpringBootAppliation启动时会默认扫描主类及当前包及子包
@SpringBootApplication(scanBasePackages = {"com.yunduo.common", "com.yunduo.im.server"})scanBasePackages扫描指定范围内带注解的@Component, @Controller, @Service, @Repository等
/** * Base packages to scan for annotated components. Use {@link #scanBasePackageClasses} * for a type-safe alternative to String-based package names. * @return base packages to scan * @since 1.3.0 */ @AliasFor(annotation = ComponentScan.class, attribute = "basePackages") String[] scanBasePackages() default {};2 @EnableFeignClients 注解默认会扫描注解@FeginClient所在的当前包及子包
@EnableFeignClients(basePackages={"com.yunduo.im.client.feign", "com.yunduo.newbiz.client.feign", "com.yunduo.netty.client.feign"})项目中使用@EnableFeignClients扫描我们指定的包,但是指定包中使用@Component和@FeginClint,而@EnableFeignClients只扫秒@FeignClint,@Component没有被扫描到,所以会报错。
由于项目室springcloud项目,需要通过Feign调用不同项目接口,所以单独创建一个类,用于扫描包,当然你也可以在@SpringBootApplication下进行扫描。
/** * 增加Feign提供端包扫描 */ @Configuration @ComponentScan(basePackages = {"com.yunduo.im.client.feign", "com.yunduo.newbiz.client.feign"}) public class FeignHystrixConfiguation { }