FeignClint之后扫描包报错

配置FeignClint之后扫描包报错

Caused by: java.lang.IllegalStateException: No fallbackFactory instance of type class com.yunduo.netty.client.feign.fallback.NettyIMFeignClientFallback found for feign client netty-mode-service

首先错误描述

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 {
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

王雪芬-ghqr-264962

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值