Feign fuse startup error Cannot map 'com.offcn.service.ProviderFeign' method and Error creating bean with name 'reques

Feign fuse startup error Cannot map 'com.offcn.service.ProviderFeign' method and Error creating bean with name 'reques

error message

	首先展示我展示一下我的报错信息:Caused by: java.lang.IllegalStateException: Ambiguous
	 mapping. Cannot map 'com.offcn.service.ProviderFeign' method。首先在下文中我们看到这个
	 报错信息,首先我们想到的是 ProvideFeign这个类中有问题。There is already 'provideFeign
	 Exception' 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 failed

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.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

Bad code (and corrected code)

	本小结就是指出问题所出现的地方,开始我的ReqestMapping是写在类前面的,可以写是可以写但是对于
不同的springcloud版本可能会出现报错信息,即现在我们出现的报错信息。所以我们需要将类前面的路径
添加到方法前的路径中。这样就可以正常启动了。

@FeignClient(value = "PROVIDER01START",configuration = FeignConfig.class,fallback = ProvideFeignException.class)
//@RequestMapping("provider01")
public interface ProviderFeign {
    
    
    @RequestMapping("/provider01/hello")
    public String hello();
}

The complete process of Feign fusing

这个是我的项目文件。下面将会介绍完整的Feign熔断的代码编写过程。

insert image description here

  • First we need to add the dependencies shown below in the pom file.
<dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
    </dependency>
  • Next we need to add in application.yml the configuration shown below.
# 开启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
  • Then we write the ProvideFeignException exception class. When we fail to call Feign remotely, we will execute this exception class. How did you get to this method when the call failed? The next piece of code will explain. When writing this exception class, we inherit ProviderFeign, the remote call interface of Feign, and we write codes for methods that may fail to be called. The specific code is as follows.
@Service
public class ProvideFeignException implements ProviderFeign {
    
    
    @Override
    public String hello() {
    
    
        return "调用失败";
    }
}

  • This is the interface class for Feign remote calls. We need to add configuration fallback = ProvideFeignException.class after FeignClient, as shown below. When we are making a Feign remote call, if it fails, the method in the ProvideFeignException class will be executed, and the fuse function will be realized at this time! ! !
@FeignClient(value = "PROVIDER01START",configuration = FeignConfig.class,fallback = ProvideFeignException.class)
//@RequestMapping("provider01")
public interface ProviderFeign {
    
    
    @RequestMapping("/provider01/hello")
    public String hello();
}

Finally, if it helps you, please like it, bookmark it and leave a comment! Thanks!

Guess you like

Origin blog.csdn.net/qq_51753851/article/details/125144999