在做服務(wù)降級的時候,老是報錯
先看一下具體錯誤:
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模塊做的服務(wù)降級
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("該服務(wù)已被降級");
testPOS.add(testPO);
return testPOS;
}
};
}
}
以上是服務(wù)降級的全部代碼,然后我搜上面的報錯,大部分都是在說我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
我仔細看了這個錯誤,發(fā)現(xiàn)是我這個FallbackFactory類,沒有注入到spring中所以它連帶著IServiceProvider沒有創(chuàng)建bean成功。
我沿著這個方向去搜索錯誤,被我找到了一個博主和我一樣的問題。
感謝這位博主寫的博客
https://blog.csdn.net/sdp1103285470/article/details/89084880
最終在你的springCloudConsumer模塊的啟動類中的@SpringBootApplication注解里面加上scanBasePackages問題就解決了,原因就是沒有掃描到那個類嘛,那我們給他加上就好了,@EnableFeignClients中的basePackages只能掃描到Feign的注解。
以下是代碼:文章來源:http://www.zghlxwxcb.cn/news/detail-633150.html
@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是包含F(xiàn)allbackFactory類的包(backage)。com.springCloudConsumerFeiger是你原本這個類的包掃描路徑。如果你只加上com.springCloudApi.service那么你這個原本的掃描路徑就會被覆蓋掉。文章來源地址http://www.zghlxwxcb.cn/news/detail-633150.html
到了這里,關(guān)于springCloudNetFlex hystrix 服務(wù)降級報錯:FactoryBean threw exception on object creation;的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!