我正在编写的微服务需要与我们平台中的其他微服务进行通信。通过这种尝试,对我们来说理想的解决方案是 Spring Cloud Netflix Feign ,实现一个 @FeignClient 。
@FeignClient
但是,当我尝试使用以下代码时,我将面临以下异常@Autowired ReviewProvider:
@Autowired ReviewProvider
异常(原因)
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'org.springframework.cloud.netflix.feign.FeignContext' available at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBean(DefaultListableBeanFactory.java:353) at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBean(DefaultListableBeanFactory.java:351) at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBean(DefaultListableBeanFactory.java:340) at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:1093) at org.springframework.cloud.netflix.feign.FeignClientFactoryBean.getObject(FeignClientFactoryBean.java:155) at org.springframework.beans.factory.support.FactoryBeanRegistrySupport.doGetObjectFromFactoryBean(FactoryBeanRegistrySupport.java:168)
ReviewProvider.java
@FeignClient("http://metadata-reviews") public interface ReviewProvider { @RequestMapping(path = "sessions", method = POST) ReviewSessionDTO createSession(); }
@RunWith(SpringRunner.class) @ActiveProfiles(INTEGRATION) @ContextConfiguration(classes = AppEntry.class) @AutoConfigureTestDatabase(replace = Replace.NONE) @DataJpaTest public class ReviewProviderTest { @Autowired private ReviewProvider provider; private Class<? extends ReviewProvider> providerClass; @Before public void setup() { providerClass = provider.getClass(); } @Test public void classAnnotations() { assertTrue(providerClass.isAnnotationPresent(FeignClient.class)); assertEquals("http://metadata-reviews", providerClass.getAnnotation(FeignClient.class).value()); } @Test public void createSession() throws Exception { final Method method = providerClass.getDeclaredMethod("createSession"); assertTrue(method.isAnnotationPresent(RequestMapping.class)); final RequestMapping mapping = method.getAnnotation(RequestMapping.class); assertEquals("sessions", mapping.path()); assertEquals(0, method.getParameters().toString()); } }
似乎还没有关于此问题的解决方案的任何信息…
这是我为解决此问题所做的工作:1.将此注释添加到测试类中:
@ImportAutoConfiguration({RibbonAutoConfiguration.class, FeignRibbonClientAutoConfiguration.class, FeignAutoConfiguration.class})
尝试一下,如果它不起作用,则可能需要在@EnableFeignClients主程序配置上添加注释
@EnableFeignClients