这篇文章主要介绍了Spring Cloud Feign报错问题解决,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下

如果我们使用Spring Cloud的Feign实现熔断,首先需要自定义一个熔断类,实现你的feign接口,然后实现方法,这些方法就是熔断方法,最后需要在你的feign接口中指定fallback为自定义类

但是启动过程中却出现了

  1. org.springframework.beans.factory.BeanCreationException: Error creating bean with name ‘requestMappingHandlerMapping’ defined in class path resource
  1. Error starting ApplicationContext. To display the conditions report rerun your application with ‘debug’ enabled.
  2. 20191201 10:47:44.584 ERROR 10548 [ main] o.s.boot.SpringApplication : Application run failed
  3.  
  4. org.springframework.beans.factory.BeanCreationException: Error creating bean with name ‘requestMappingHandlerMapping’ defined in class path resource [org/springframework/boot/autoconfigure/web/servlet/WebMvcAutoConfiguration$EnableWebMvcConfiguration.class]: Invocation of init method failed; nested exception is java.lang.IllegalStateException: Ambiguous mapping. Cannot map ‘com.sjl.consumer.client.UserClient’ method
  5. public abstract java.lang.String com.sjl.consumer.client.UserClient.getUser(java.lang.Integer)
  6. to {[/user/{id}],methods=[GET]}: There is already ‘userClientFallback’ bean method
  7. public java.lang.String com.sjl.consumer.client.UserClientFallback.getUser(java.lang.Integer) mapped.
  8. at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1694) ~[springbeans5.0.10.RELEASE.jar:5.0.10.RELEASE]
  9. at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:573) ~[springbeans5.0.10.RELEASE.jar:5.0.10.RELEASE]
  10. at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:495) ~[springbeans5.0.10.RELEASE.jar:5.0.10.RELEASE]
  11. at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:317) ~[springbeans5.0.10.RELEASE.jar:5.0.10.RELEASE]
  12. at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222) ~[springbeans5.0.10.RELEASE.jar:5.0.10.RELEASE]
  13. at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:315) ~[springbeans5.0.10.RELEASE.jar:5.0.10.RELEASE]
  14. at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:199) ~[springbeans5.0.10.RELEASE.jar:5.0.10.RELEASE]
  15. at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:759) ~[springbeans5.0.10.RELEASE.jar:5.0.10.RELEASE]
  16. at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:867) ~[springcontext5.0.10.RELEASE.jar:5.0.10.RELEASE]
  17. at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:548) ~[springcontext5.0.10.RELEASE.jar:5.0.10.RELEASE]
  18. at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:140) ~[springboot2.0.6.RELEASE.jar:2.0.6.RELEASE]
  19. at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:754) [springboot2.0.6.RELEASE.jar:2.0.6.RELEASE]
  20. at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:386) [springboot2.0.6.RELEASE.jar:2.0.6.RELEASE]
  21. at org.springframework.boot.SpringApplication.run(SpringApplication.java:307) [springboot2.0.6.RELEASE.jar:2.0.6.RELEASE]
  22. at org.springframework.boot.SpringApplication.run(SpringApplication.java:1242) [springboot2.0.6.RELEASE.jar:2.0.6.RELEASE]
  23. at org.springframework.boot.SpringApplication.run(SpringApplication.java:1230) [springboot2.0.6.RELEASE.jar:2.0.6.RELEASE]
  24. at com.sjl.consumer.ServiceConsumerApplication.main(ServiceConsumerApplication.java:27) [classes/:na]
  25. Caused by: java.lang.IllegalStateException: Ambiguous mapping. Cannot map ‘com.sjl.consumer.client.UserClient’ method
  26. public abstract java.lang.String com.sjl.consumer.client.UserClient.getUser(java.lang.Integer)
  27. to {[/user/{id}],methods=[GET]}: There is already ‘userClientFallback’ bean method
  28. public java.lang.String com.sjl.consumer.client.UserClientFallback.getUser(java.lang.Integer) mapped.
  29. at org.springframework.web.servlet.handler.AbstractHandlerMethodMapping$MappingRegistry.assertUniqueMethodMapping(AbstractHandlerMethodMapping.java:581) ~[springwebmvc5.0.10.RELEASE.jar:5.0.10.RELEASE]
  30. at org.springframework.web.servlet.handler.AbstractHandlerMethodMapping$MappingRegistry.register(AbstractHandlerMethodMapping.java:545) ~[springwebmvc5.0.10.RELEASE.jar:5.0.10.RELEASE]
  31. at org.springframework.web.servlet.handler.AbstractHandlerMethodMapping.registerHandlerMethod(AbstractHandlerMethodMapping.java:267) ~[springwebmvc5.0.10.RELEASE.jar:5.0.10.RELEASE]
  32. at org.springframework.web.servlet.handler.AbstractHandlerMethodMapping.lambda$detectHandlerMethods$1(AbstractHandlerMethodMapping.java:252) ~[springwebmvc5.0.10.RELEASE.jar:5.0.10.RELEASE]
  33. at java.util.LinkedHashMap.forEach(LinkedHashMap.java:684) ~[na:1.8.0_131]
  34. at org.springframework.web.servlet.handler.AbstractHandlerMethodMapping.detectHandlerMethods(AbstractHandlerMethodMapping.java:250) ~[springwebmvc5.0.10.RELEASE.jar:5.0.10.RELEASE]
  35. at org.springframework.web.servlet.handler.AbstractHandlerMethodMapping.initHandlerMethods(AbstractHandlerMethodMapping.java:219) ~[springwebmvc5.0.10.RELEASE.jar:5.0.10.RELEASE]
  36. at org.springframework.web.servlet.handler.AbstractHandlerMethodMapping.afterPropertiesSet(AbstractHandlerMethodMapping.java:189) ~[springwebmvc5.0.10.RELEASE.jar:5.0.10.RELEASE]
  37. at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping.afterPropertiesSet(RequestMappingHandlerMapping.java:136) ~[springwebmvc5.0.10.RELEASE.jar:5.0.10.RELEASE]
  38. at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1753) ~[springbeans5.0.10.RELEASE.jar:5.0.10.RELEASE]
  39. at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1690) ~[springbeans5.0.10.RELEASE.jar:5.0.10.RELEASE]
  40. 16 common frames omitted

如果启动服务类出现了上述的错误,并且你的feign接口上定义了@RequestMapping(“xxxx”)全局访问路径,你只需要将其中的路径移到方法的路径上

例如我定义的

  1. @FeignClient(value = “service-provider”, fallback = UserClientFallback.class)//声明这是一个Feign接口
  2. @RequestMapping(“/user”)
  3. public interface UserClient {
  4.  
  5. @GetMapping(“/{id}”)
  6. String getUser(@PathVariable(“id”) Integer id);
  7. }

将其改为

  1. @FeignClient(value = “service-provider”, fallback = UserClientFallback.class)//声明这是一个Feign接口
  2. //@RequestMapping(“/user”)
  3. public interface UserClient {
  4.  
  5. @GetMapping(“/user/{id}”)
  6. String getUser(@PathVariable(“id”) Integer id);
  7. }

即可,启动成功

 转自:https://www.w3xue.com/exp/article/201912/66796.html