1. When using feign to make service calls, use inherited methods to call the service. When joining Hystrix's circuit breaker processing fallback configuration, an error will be reported and has been resolved. 2. Use feign default configuration, the circuit breaker does not take effect, and has been resolved.
Recently, I was learning about microservices and found that when using feign to make service calls, I used inheritance to call the service. When adding Hystrix's circuit breaker processing fallback configuration, an error will be reported. The code is as follows:
@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 {} @Componentpublic class ConsumerFeignServiceFallBack implements ConsumerFeignService { @Override public User getUserById(long id) { return new User(); } @Override public String echo(String name) { return "echo error: " + name; }}The error is reported as follows:
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
The error reason is that the interface class inherited here is a controller interface, which will inherit from the parent class when inheriting.
@RequestMapping("/demo/api")When SpringMvc was doing mapping, it was found that the mapping between ConsumerFeignService and ConsumerFeignServiceFallBack was repeated, so an exception was thrown. How to solve it?
There are 2 solutions:
First, change the mapping configuration of ConsumerFeignServiceFallBack, the code is as follows:
@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; }}The second is to use fallbackFactory, the code is as follows:
@Componentpublic 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; } }; }}After running, I turned off the service provider and found that the fuse did not take effect. I did not enter the fallback method like using @HystrixCommand alone. I checked many methods and found that the configuration switch of hystix, which was originally feign, was not turned on.
Solution: Add configuration in application.yml as follows:
feign: hystrix: enabled: true
There is no prompt for this configuration in IntelliJ IDEA, and a warning is also reported. I don’t know if it is considered a bug. The version I use here is
springBootVersion = '1.5.10.RELEASE'springCloudVersion = 'Edgware.SR3'
The above are some small pitfalls I found when using feign. I hope it will be helpful to everyone's learning and I hope everyone will support Wulin.com more.