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熔断启动报错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的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/373276

相关文章

qt5cored.dll报错怎么解决? 电脑qt5cored.dll文件丢失修复技巧

《qt5cored.dll报错怎么解决?电脑qt5cored.dll文件丢失修复技巧》在进行软件安装或运行程序时,有时会遇到由于找不到qt5core.dll,无法继续执行代码,这个问题可能是由于该文... 遇到qt5cored.dll文件错误时,可能会导致基于 Qt 开发的应用程序无法正常运行或启动。这种错

Nexus安装和启动的实现教程

《Nexus安装和启动的实现教程》:本文主要介绍Nexus安装和启动的实现教程,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录一、Nexus下载二、Nexus安装和启动三、关闭Nexus总结一、Nexus下载官方下载链接:DownloadWindows系统根

Java中Map.Entry()含义及方法使用代码

《Java中Map.Entry()含义及方法使用代码》:本文主要介绍Java中Map.Entry()含义及方法使用的相关资料,Map.Entry是Java中Map的静态内部接口,用于表示键值对,其... 目录前言 Map.Entry作用核心方法常见使用场景1. 遍历 Map 的所有键值对2. 直接修改 Ma

MybatisPlus service接口功能介绍

《MybatisPlusservice接口功能介绍》:本文主要介绍MybatisPlusservice接口功能介绍,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友... 目录Service接口基本用法进阶用法总结:Lambda方法Service接口基本用法MyBATisP

Java中实现线程的创建和启动的方法

《Java中实现线程的创建和启动的方法》在Java中,实现线程的创建和启动是两个不同但紧密相关的概念,理解为什么要启动线程(调用start()方法)而非直接调用run()方法,是掌握多线程编程的关键,... 目录1. 线程的生命周期2. start() vs run() 的本质区别3. 为什么必须通过 st

Oracle修改端口号之后无法启动的解决方案

《Oracle修改端口号之后无法启动的解决方案》Oracle数据库更改端口后出现监听器无法启动的问题确实较为常见,但并非必然发生,这一问题通常源于​​配置错误或环境冲突​​,而非端口修改本身,以下是系... 目录一、问题根源分析​​​二、保姆级解决方案​​​​步骤1:修正监听器配置文件 (listener.

MySQL版本问题导致项目无法启动问题的解决方案

《MySQL版本问题导致项目无法启动问题的解决方案》本文记录了一次因MySQL版本不一致导致项目启动失败的经历,详细解析了连接错误的原因,并提供了两种解决方案:调整连接字符串禁用SSL或统一MySQL... 目录本地项目启动报错报错原因:解决方案第一个:第二种:容器启动mysql的坑两种修改时区的方法:本地

Java中JSON格式反序列化为Map且保证存取顺序一致的问题

《Java中JSON格式反序列化为Map且保证存取顺序一致的问题》:本文主要介绍Java中JSON格式反序列化为Map且保证存取顺序一致的问题,具有很好的参考价值,希望对大家有所帮助,如有错误或未... 目录背景问题解决方法总结背景做项目涉及两个微服务之间传数据时,需要提供方将Map类型的数据序列化为co

Spring中管理bean对象的方式(专业级说明)

《Spring中管理bean对象的方式(专业级说明)》在Spring框架中,Bean的管理是核心功能,主要通过IoC(控制反转)容器实现,下面给大家介绍Spring中管理bean对象的方式,感兴趣的朋... 目录1.Bean的声明与注册1.1 基于XML配置1.2 基于注解(主流方式)1.3 基于Java

MySQL启动报错:InnoDB表空间丢失问题及解决方法

《MySQL启动报错:InnoDB表空间丢失问题及解决方法》在启动MySQL时,遇到了InnoDB:Tablespace5975wasnotfound,该错误表明MySQL在启动过程中无法找到指定的s... 目录mysql 启动报错:InnoDB 表空间丢失问题及解决方法错误分析解决方案1. 启用 inno