springCloudNetFlex hystrix 服务降级报错:FactoryBean threw exception on object creation;

在做服务降级的时候,老是报错
先看一下具体错误:

FactoryBean threw exception on object creation; nested exception is java.lang.IllegalStateException: No fallbackFactory instance of type class com.springCloudApi.service.testFallBackService found for feign client springCloudProvider

我是在api模块做的服务降级
springCloudApi

IServiceProvider

import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;

@Service
@FeignClient(name = "springCloudProvider", fallbackFactory = testFallBackService.class)
public interface IServiceProvider {

    @GetMapping("/listTest")
    List<testPO> listTest(@RequestParam("name") String name);

}

testFallBackService

import feign.hystrix.FallbackFactory;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

@Component
public class testFallBackService implements FallbackFactory<IServiceProvider> {

    @Override
    public IServiceProvider create(Throwable throwable) {
        return new IServiceProvider() {
            @Override
            public List<testPO> listTest(String name) {
                List<testPO> testPOS = new ArrayList<>();
                testPO testPO = new testPO();
                testPO.setName("该服务已被降级");
                testPOS.add(testPO);
                return testPOS;
            }
        };
    }
}

以上是服务降级的全部代码,然后我搜上面的报错,大部分都是在说我FallbackFactory类,没有加@Component这个注解,但我加了还是报这个错

 Error creating bean with name 'consumerController': Unsatisfied dependency expressed through field 'iServiceProvider'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'com.springCloudApi.service.IServiceProvider': FactoryBean threw exception on object creation; nested exception is java.lang.IllegalStateException: No fallbackFactory instance of type class com.springCloudApi.service.testFallBackService found for feign client springCloudProvider

我仔细看了这个错误,发现是我这个FallbackFactory类,没有注入到spring中所以它连带着IServiceProvider没有创建bean成功。
我沿着这个方向去搜索错误,被我找到了一个博主和我一样的问题。
感谢这位博主写的博客
https://blog.csdn.net/sdp1103285470/article/details/89084880
最终在你的springCloudConsumer模块的启动类中的@SpringBootApplication注解里面加上scanBasePackages问题就解决了,原因就是没有扫描到那个类嘛,那我们给他加上就好了,@EnableFeignClients中的basePackages只能扫描到Feign的注解。
以下是代码:

@EnableEurekaClient
@EnableFeignClients(basePackages = "com.springCloudApi")
@SpringBootApplication(exclude= {DataSourceAutoConfiguration.class}, scanBasePackages = {"com.springCloudApi.service", "com.springCloudConsumerFeiger"})
public class consumerFeignApplication {
    public static void main(String[] args) {
        SpringApplication.run(consumerFeignApplication.class, args);
    }
}

注意:com.springCloudApi.service是包含FallbackFactory类的包(backage)。com.springCloudConsumerFeiger是你原本这个类的包扫描路径。如果你只加上com.springCloudApi.service那么你这个原本的扫描路径就会被覆盖掉。

你可能感兴趣的:(hystrix,java,spring)