本文主要是介绍Feign熔断启动报错Cannot map ‘com.offcn.service.ProviderFeign‘ method和Error creating bean with name ‘reques,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
Feign熔断启动报错Cannot map 'com.offcn.service.ProviderFeign' method和Error creating bean with name 'reques
- 报错信息
- 错误的代码(及修正后的代码)
- Feign熔断的完整过程
报错信息
首先展示我展示一下我的报错信息:Caused by: java.lang.IllegalStateException: Ambiguousmapping. Cannot map 'com.offcn.service.ProviderFeign' method。首先在下文中我们看到这个报错信息,首先我们想到的是 ProvideFeign这个类中有问题。There is already 'provideFeignException' bean method结合这个报错信息我们就可以知道问题出现在了ProvideFeign中。如题目所出现的问题Error creating bean with name 'requestMappingHandlerMapping' defined in class path resource锁定出错位置就是@RequestMapping("xxxx")全局访问路径
2022-06-06 13:24:29.653 ERROR 21088 --- [ main] o.s.boot.SpringApplication : Application run failedorg.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.offcn.service.ProviderFeign' method
com.offcn.service.ProviderFeign#hello()
to { /provider01/hello}: There is already 'provideFeignException' bean method
com.offcn.service.Impl.ProvideFeignException#hello() mapped.at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1794) ~[spring-beans-5.2.12.RELEASE.jar:5.2.12.RELEASE]at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:594) ~[spring-beans-5.2.12.RELEASE.jar:5.2.12.RELEASE]at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:516) ~[spring-beans-5.2.12.RELEASE.jar:5.2.12.RELEASE]at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:324) ~[spring-beans-5.2.12.RELEASE.jar:5.2.12.RELEASE]at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:234) ~[spring-beans-5.2.12.RELEASE.jar:5.2.12.RELEASE]at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:322) ~[spring-beans-5.2.12.RELEASE.jar:5.2.12.RELEASE]at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:202) ~[spring-beans-5.2.12.RELEASE.jar:5.2.12.RELEASE]at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:897) ~[spring-beans-5.2.12.RELEASE.jar:5.2.12.RELEASE]at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:879) ~[spring-context-5.2.12.RELEASE.jar:5.2.12.RELEASE]at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:551) ~[spring-context-5.2.12.RELEASE.jar:5.2.12.RELEASE]at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:143) ~[spring-boot-2.3.8.RELEASE.jar:2.3.8.RELEASE]at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:758) [spring-boot-2.3.8.RELEASE.jar:2.3.8.RELEASE]at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:750) [spring-boot-2.3.8.RELEASE.jar:2.3.8.RELEASE]at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:405) [spring-boot-2.3.8.RELEASE.jar:2.3.8.RELEASE]at org.springframework.boot.SpringApplication.run(SpringApplication.java:315) [spring-boot-2.3.8.RELEASE.jar:2.3.8.RELEASE]at org.springframework.boot.SpringApplication.run(SpringApplication.java:1237) [spring-boot-2.3.8.RELEASE.jar:2.3.8.RELEASE]at org.springframework.boot.SpringApplication.run(SpringApplication.java:1226) [spring-boot-2.3.8.RELEASE.jar:2.3.8.RELEASE]at com.offcn.Consumer01.main(Consumer01.java:13) [classes/:na]
Caused by: java.lang.IllegalStateException: Ambiguous mapping. Cannot map 'com.offcn.service.ProviderFeign' method
com.offcn.service.ProviderFeign#hello()
to { /provider01/hello}: There is already 'provideFeignException' bean method
com.offcn.service.Impl.ProvideFeignException#hello() mapped.at org.springframework.web.servlet.handler.AbstractHandlerMethodMapping$MappingRegistry.validateMethodMapping(AbstractHandlerMethodMapping.java:636) ~[spring-webmvc-5.2.12.RELEASE.jar:5.2.12.RELEASE]at org.springframework.web.servlet.handler.AbstractHandlerMethodMapping$MappingRegistry.register(AbstractHandlerMethodMapping.java:603) ~[spring-webmvc-5.2.12.RELEASE.jar:5.2.12.RELEASE]at org.springframework.web.servlet.handler.AbstractHandlerMethodMapping.registerHandlerMethod(AbstractHandlerMethodMapping.java:318) ~[spring-webmvc-5.2.12.RELEASE.jar:5.2.12.RELEASE]at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping.registerHandlerMethod(RequestMappingHandlerMapping.java:378) ~[spring-webmvc-5.2.12.RELEASE.jar:5.2.12.RELEASE]at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping.registerHandlerMethod(RequestMappingHandlerMapping.java:75) ~[spring-webmvc-5.2.12.RELEASE.jar:5.2.12.RELEASE]at org.springframework.web.servlet.handler.AbstractHandlerMethodMapping.lambda$detectHandlerMethods$1(AbstractHandlerMethodMapping.java:288) ~[spring-webmvc-5.2.12.RELEASE.jar:5.2.12.RELEASE]at java.util.LinkedHashMap.forEach(LinkedHashMap.java:684) ~[na:1.8.0_301]at org.springframework.web.servlet.handler.AbstractHandlerMethodMapping.detectHandlerMethods(AbstractHandlerMethodMapping.java:286) ~[spring-webmvc-5.2.12.RELEASE.jar:5.2.12.RELEASE]at org.springframework.web.servlet.handler.AbstractHandlerMethodMapping.processCandidateBean(AbstractHandlerMethodMapping.java:258) ~[spring-webmvc-5.2.12.RELEASE.jar:5.2.12.RELEASE]at org.springframework.web.servlet.handler.AbstractHandlerMethodMapping.initHandlerMethods(AbstractHandlerMethodMapping.java:217) ~[spring-webmvc-5.2.12.RELEASE.jar:5.2.12.RELEASE]at org.springframework.web.servlet.handler.AbstractHandlerMethodMapping.afterPropertiesSet(AbstractHandlerMethodMapping.java:205) ~[spring-webmvc-5.2.12.RELEASE.jar:5.2.12.RELEASE]at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping.afterPropertiesSet(RequestMappingHandlerMapping.java:189) ~[spring-webmvc-5.2.12.RELEASE.jar:5.2.12.RELEASE]at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1853) ~[spring-beans-5.2.12.RELEASE.jar:5.2.12.RELEASE]at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1790) ~[spring-beans-5.2.12.RELEASE.jar:5.2.12.RELEASE]... 17 common frames omitted
错误的代码(及修正后的代码)
本小结就是指出问题所出现的地方,开始我的ReqestMapping是写在类前面的,可以写是可以写但是对于
不同的springcloud版本可能会出现报错信息,即现在我们出现的报错信息。所以我们需要将类前面的路径
添加到方法前的路径中。这样就可以正常启动了。
@FeignClient(value = "PROVIDER01START",configuration = FeignConfig.class,fallback = ProvideFeignException.class)
//@RequestMapping("provider01")
public interface ProviderFeign {@RequestMapping("/provider01/hello")public String hello();
}
Feign熔断的完整过程
这个是我的项目文件。下面将会介绍完整的Feign熔断的代码编写过程。
- 首先我们需要在pom文件中添加如下所示的依赖。
<dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-netflix-hystrix</artifactId></dependency>
- 接下来我们需要在application.yml中添加,如下所示的配置。
# 开启Feign的熔断功能
feign:hystrix:enabled: true
#总连接超时时间=(切换服务实例次数+1)*(每个实例重试次数+1)*连接超时时间
USERPROVIDER: #服务名称ribbon:#配置指定服务的负载均衡策略NFLoadBalancerRuleClassName: com.netflix.loadbalancer.RoundRobinRule# Ribbon的连接超时时间ConnectTimeout: 250# Ribbon的数据读取超时时间ReadTimeout: 250# 是否对所有操作都进行重试OkToRetryOnAllOperations: true# 切换实例的重试次数MaxAutoRetriesNextServer: 1# 对当前实例的重试次数MaxAutoRetries: 1
#设定Hystrix熔断超时时间 ,理论上熔断时间应该大于总连接超时时间
hystrix:command:default:execution:isolation:thread:timeoutInMilliseconds: 6000
- 再然后我们编写ProvideFeignException 异常类,当我们远程调用Feign时失败时,我们就会执行这个异常类。调用失败时,是怎么执行到这个方法的?下一段代码将会讲解。写这个异常类的时候我们继承ProviderFeign 这个Feign 的远程调用的接口,对于可能调用失败的方法,我们进行编写代码。具体代码如下。
@Service
public class ProvideFeignException implements ProviderFeign {@Overridepublic String hello() {return "调用失败";}
}
- 这个是Feign远程调用的接口类,我们需要在FeignClient后面添加配置fallback = ProvideFeignException.class,如下所示。当我们在进行Feign远程调用时,如果失败则会执行ProvideFeignException类中的方法,这时候熔断功能就实现啦!!!
@FeignClient(value = "PROVIDER01START",configuration = FeignConfig.class,fallback = ProvideFeignException.class)
//@RequestMapping("provider01")
public interface ProviderFeign {@RequestMapping("/provider01/hello")public String hello();
}
最后如果帮助到你了,请点赞,收藏给个评论吧!谢谢!
这篇关于Feign熔断启动报错Cannot map ‘com.offcn.service.ProviderFeign‘ method和Error creating bean with name ‘reques的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!