Some small pits in Spring Cloud Feign fuse configuration

Recently, I was studying microservices and found that when using feign to make service calls, using inheritance to call services, and adding Hystrix's fuse to process 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 {

}
@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;
    }
}

The error is 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 reason for the error is that the interface class inherited here is a controller interface, which will be inherited from the parent class when inherited.

@RequestMapping("/demo/api")

SpringMvc found that the mappings of ConsumerFeignService and ConsumerFeignServiceFallBack were duplicated when doing mapping mapping, so an exception was thrown, how to solve it?

 

There are 2 solutions:

One is to 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:

@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;
            }
        };
    }
}


After running, I closed the service provider and found that the fuse did not take effect. It did not enter the fallback method like when @HystrixCommand was used alone. After checking many methods, I found that the configuration switch of hystix, which was originally feign, was not turned on.

As a workaround, add the following configuration to application.yml:

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 a bug. The version I use here is

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

The above are some small pits I found when using feign. If you have any questions, please feel free to shoot bricks~

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324961476&siteId=291194637