No fallbackFactory instance of type class com.ruoyi.system.api.factory.RemoteOperationFallbackFacto

我的微服务项目里面有个远程服务调用的API模块,里面没有启动类:
在这里插入图片描述

我在其中建了一个接口RemoteOperationService

import com.alibaba.fastjson2.JSONObject;
import com.ruoyi.common.core.constant.ServiceNameConstants;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.system.api.factory.RemoteOperationFallbackFactory;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.PostMapping;

/**
 * @ClassName RemoteOperationService * @Description TODO
 * @Author lgn
 * @Date 15:53 2022/8/18
 * @Version 1.0
 **/
@FeignClient(contextId = "remoteOperationService", value = ServiceNameConstants.LAB_SERVICE, fallbackFactory = RemoteOperationFallbackFactory.class)
public interface RemoteOperationService {

    @PostMapping("/support/pollingOperationTask")
    public R<JSONObject> pollingOperationTask();


}
import com.alibaba.fastjson2.JSONObject;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.system.api.RemoteOperationService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.cloud.openfeign.FallbackFactory;
import org.springframework.stereotype.Component;

/**
 * @ClassName RemoteOperationFallbackFactory * @Description TODO
 * @Author lgn
 * @Date 15:56 2022/8/18
 * @Version 1.0
 **/
@Component
public class RemoteOperationFallbackFactory implements FallbackFactory<RemoteOperationService> {
    private static final Logger log = LoggerFactory.getLogger(RemoteOperationFallbackFactory.class);

    @Override
    public RemoteOperationService create(Throwable throwable) {
        log.error("日志服务调用失败:{}", throwable.getMessage());

        return new RemoteOperationService(){

            @Override
            public R<JSONObject> pollingOperationTask() {
                return R.fail("轮询运维任务失败:" + throwable.getMessage());
            }
        };
    }
}

其中一个微服务引用了这个远程Service的接口 这个微服务启动的时候报错:

17:28:28.427 [main] ERROR o.s.b.SpringApplication - [reportFailure,830] - Application run failed
org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'ryTask': Unsatisfied dependency expressed through field 'remoteOperationService'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'com.ruoyi.system.api.RemoteOperationService': Unexpected exception during bean creation; nested exception is java.lang.IllegalStateException:
 No fallbackFactory instance of type class com.ruoyi.system.api.factory.RemoteOperationFallbackFactory found for feign client remoteOperationService

SpringBoot加载bean的过程:

@ComponentScan 注解的作用是扫描 @SpringBootApplication 所在的 Application 类所在的包(basepackage)下所有的 @component 注解(或拓展了 @component 的注解)标记的 bean,并注册到 spring 容器中。

我那个远程调用的api模块里面都没有启动类,我怎么把生命的bean注入到springboot里面呢。
解决 Spring Boot 中不能被默认路径扫描的配置类:
在Spring中也有一种类似与Java SPI的加载机制。它在META-INF/spring.factories文件中配置接口的实现类名称,然后在程序中读取这些配置文件并实例化。
在这里插入图片描述
把这个RemoteOperationFallbackFactory写进去就好了。
如果一个接口希望配置多个实现类,可以用","分割。

  • 7
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 5
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值