Spring Cloud Feign 熔断配置的一些小坑

原创
2018/04/20 10:18
阅读数 2.6W

最近在做微服务的学习,发现在使用feign做服务调用时,使用继承的方式调用服务,加入Hystrix的熔断处理fallback配置时,会报错,代码如下:

@RequestMapping("/demo/api")
public interface HelloApi {

    @GetMapping("user/{id}")
    User getUserById(@PathVariable("id") long id);

    @GetMapping("hello")
    String echo(@RequestParam("name") String name);
}
@FeignClient(value = "ms-server", fallback = ConsumerFeignServiceFallBack.class)
public interface ConsumerFeignService extends HelloApi {

}
@Component
public class ConsumerFeignServiceFallBack implements ConsumerFeignService {
    @Override
    public User getUserById(long id) {
        return new User();
    }

    @Override
    public String echo(String name) {
        return "echo error: " + name;
    }
}

报错如下:

Caused by: java.lang.IllegalStateException: Ambiguous mapping. Cannot map 'com.thoughtworks.demo.consumer.service.ConsumerFeignService' method 
public abstract java.lang.String com.thoughtworks.demo.api.HelloApi.echo(java.lang.String)
to {[/demo/api/hello],methods=[GET]}: There is already 'consumerFeignServiceFallBack' bean method
public java.lang.String com.thoughtworks.demo.consumer.service.ConsumerFeignServiceFallBack.echo(java.lang.String) mapped.
	at org.springframework.web.servlet.handler.AbstractHandlerMethodMapping$MappingRegistry.assertUniqueMethodMapping(AbstractHandlerMethodMapping.java:576) ~[spring-webmvc-4.3.14.RELEASE.jar:4.3.14.RELEASE]
	at org.springframework.web.servlet.handler.AbstractHandlerMethodMapping$MappingRegistry.register(AbstractHandlerMethodMapping.java:540) ~[spring-webmvc-4.3.14.RELEASE.jar:4.3.14.RELEASE]
	at org.springframework.web.servlet.handler.AbstractHandlerMethodMapping.registerHandlerMethod(AbstractHandlerMethodMapping.java:264) ~[spring-webmvc-4.3.14.RELEASE.jar:4.3.14.RELEASE]
	at org.springframework.web.servlet.handler.AbstractHandlerMethodMapping.detectHandlerMethods(AbstractHandlerMethodMapping.java:250) ~[spring-webmvc-4.3.14.RELEASE.jar:4.3.14.RELEASE]
	at org.springframework.web.servlet.handler.AbstractHandlerMethodMapping.initHandlerMethods(AbstractHandlerMethodMapping.java:214) ~[spring-webmvc-4.3.14.RELEASE.jar:4.3.14.RELEASE]
	at org.springframework.web.servlet.handler.AbstractHandlerMethodMapping.afterPropertiesSet(AbstractHandlerMethodMapping.java:184) ~[spring-webmvc-4.3.14.RELEASE.jar:4.3.14.RELEASE]
	at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping.afterPropertiesSet(RequestMappingHandlerMapping.java:127) ~[spring-webmvc-4.3.14.RELEASE.jar:4.3.14.RELEASE]
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1687) ~[spring-beans-4.3.14.RELEASE.jar:4.3.14.RELEASE]
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1624) ~[spring-beans-4.3.14.RELEASE.jar:4.3.14.RELEASE]
	... 21 common frames omitted

错误原因是这里继承的接口类是一个controller接口,继承时会继承到父类的

@RequestMapping("/demo/api")

SpringMvc在做mapping映射的时候发现ConsumerFeignService和ConsumerFeignServiceFallBack的mapping重复了,所以抛出异常,如何解决呢?

 

解决的方法有2个:

一是更改ConsumerFeignServiceFallBack的mapping配置,代码如下:

@Component
@RequestMapping("fallback/demo/api")
public class ConsumerFeignServiceFallBack implements ConsumerFeignService {
    @Override
    public User getUserById(long id) {
        return new User();
    }

    @Override
    public String echo(String name) {
        return "echo error: " + name;
    }
}

二是使用fallbackFactory,代码如下:

@Component
public class ConsumerFeignServiceFallBack implements FallbackFactory<ConsumerFeignService> {

    @Override
    public ConsumerFeignService create(Throwable cause) {
        return new ConsumerFeignService() {
            @Override
            public User getUserById(long id) {
                return new User();
            }

            @Override
            public String echo(String name) {
                return "echo error: " + name;
            }
        };
    }
}


运行后,关闭服务提供者,发现熔断并没有生效,没有像单独使用@HystrixCommand时进入fallback方法,查了很多方式,发现原来是feign的hystix的配置开关没有打开

解决方法,在application.yml中增加配置如下:

feign:
  hystrix:
    enabled: true

在IntelliJ IDEA里也没有这个配置的提示,还报告警告,不知道算不算是个BUG,这里我使用的版本是

springBootVersion = '1.5.10.RELEASE'
springCloudVersion = 'Edgware.SR3'

以上就是我在使用feign时发现的一些小坑,如有问题,欢迎随时拍砖~

展开阅读全文
打赏
0
12 收藏
分享
加载中
也可以这样处理:
http://blog.didispace.com/spring-cloud-feignclient-problem/?utm_source=tuicool&utm_medium=referral
2018/04/22 08:29
回复
举报
更多评论
打赏
1 评论
12 收藏
0
分享
返回顶部
顶部