详解Spring Cloud Feign 熔断配置的一些小坑
1.在使用feign做服务调用时,使用继承的方式调用服务,加入hystrix的熔断处理fallback配置时,会报错,已解决。 2.使用feign默认配置,熔断不生效,已解决。
最近在做微服务的学习,发现在使用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时发现的一些小坑,希望对大家的学习有所帮助,也希望大家多多支持。