最近在作微服務的學習,發如今使用feign作服務調用時,使用繼承的方式調用服務,加入Hystrix的熔斷處理fallback配置時,會報錯,代碼以下:java
@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; } }
報錯以下:web
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接口,繼承時會繼承到父類的spring
@RequestMapping("/demo/api")
SpringMvc在作mapping映射的時候發現ConsumerFeignService和ConsumerFeignServiceFallBack的mapping重複了,因此拋出異常,如何解決呢?api
解決的方法有2個:mvc
一是更改ConsumerFeignServiceFallBack的mapping配置,代碼以下:app
@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,代碼以下:ide
@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,這裏我使用的版本是code
springBootVersion = '1.5.10.RELEASE' springCloudVersion = 'Edgware.SR3'
以上就是我在使用feign時發現的一些小坑,若有問題,歡迎隨時拍磚~