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

相关文章

Nginx启动失败:端口80被占用问题的解决方案

《Nginx启动失败:端口80被占用问题的解决方案》在Linux服务器上部署Nginx时,可能会遇到Nginx启动失败的情况,尤其是错误提示bind()to0.0.0.0:80failed,这种问题通... 目录引言问题描述问题分析解决方案1. 检查占用端口 80 的进程使用 netstat 命令使用 ss

IDEA编译报错“java: 常量字符串过长”的原因及解决方法

《IDEA编译报错“java:常量字符串过长”的原因及解决方法》今天在开发过程中,由于尝试将一个文件的Base64字符串设置为常量,结果导致IDEA编译的时候出现了如下报错java:常量字符串过长,... 目录一、问题描述二、问题原因2.1 理论角度2.2 源码角度三、解决方案解决方案①:StringBui

在 Spring Boot 中使用 @Autowired和 @Bean注解的示例详解

《在SpringBoot中使用@Autowired和@Bean注解的示例详解》本文通过一个示例演示了如何在SpringBoot中使用@Autowired和@Bean注解进行依赖注入和Bean... 目录在 Spring Boot 中使用 @Autowired 和 @Bean 注解示例背景1. 定义 Stud

Python Jupyter Notebook导包报错问题及解决

《PythonJupyterNotebook导包报错问题及解决》在conda环境中安装包后,JupyterNotebook导入时出现ImportError,可能是由于包版本不对应或版本太高,解决方... 目录问题解决方法重新安装Jupyter NoteBook 更改Kernel总结问题在conda上安装了

Python安装时常见报错以及解决方案

《Python安装时常见报错以及解决方案》:本文主要介绍在安装Python、配置环境变量、使用pip以及运行Python脚本时常见的错误及其解决方案,文中介绍的非常详细,需要的朋友可以参考下... 目录一、安装 python 时常见报错及解决方案(一)安装包下载失败(二)权限不足二、配置环境变量时常见报错及

Android里面的Service种类以及启动方式

《Android里面的Service种类以及启动方式》Android中的Service分为前台服务和后台服务,前台服务需要亮身份牌并显示通知,后台服务则有启动方式选择,包括startService和b... 目录一句话总结:一、Service 的两种类型:1. 前台服务(必须亮身份牌)2. 后台服务(偷偷干

关于Spring @Bean 相同加载顺序不同结果不同的问题记录

《关于Spring@Bean相同加载顺序不同结果不同的问题记录》本文主要探讨了在Spring5.1.3.RELEASE版本下,当有两个全注解类定义相同类型的Bean时,由于加载顺序不同,最终生成的... 目录问题说明测试输出1测试输出2@Bean注解的BeanDefiChina编程nition加入时机总结问题说明

在Java中使用ModelMapper简化Shapefile属性转JavaBean实战过程

《在Java中使用ModelMapper简化Shapefile属性转JavaBean实战过程》本文介绍了在Java中使用ModelMapper库简化Shapefile属性转JavaBean的过程,对比... 目录前言一、原始的处理办法1、使用Set方法来转换2、使用构造方法转换二、基于ModelMapper

Windows设置nginx启动端口的方法

《Windows设置nginx启动端口的方法》在服务器配置与开发过程中,nginx作为一款高效的HTTP和反向代理服务器,被广泛应用,而在Windows系统中,合理设置nginx的启动端口,是确保其正... 目录一、为什么要设置 nginx 启动端口二、设置步骤三、常见问题及解决一、为什么要设置 nginx

springboot启动流程过程

《springboot启动流程过程》SpringBoot简化了Spring框架的使用,通过创建`SpringApplication`对象,判断应用类型并设置初始化器和监听器,在`run`方法中,读取配... 目录springboot启动流程springboot程序启动入口1.创建SpringApplicat