标签:bean 一个 问题 except end com class client nts
在fallback时,发现重复注册了两次同一uri,报错:
Invocation of init method failed; nested exception is java.lang.IllegalStateException: Ambiguous mapping. Cannot map ‘com.yiming.user_consumer.service.UserConsumerService‘ method com.yiming.user_consumer.service.UserConsumerService#isAlive() to {GET /user/isAlive}: There is already ‘userProviderBack‘ bean method com.yiming.user_consumer.componant.UserProviderBack#isAlive() mapped.
这是Feign的一个Bug。在api中添加了@RequestMapping(“/xxx”)注解后,SpringMVC、Hystrix、Feign都要检查uri。如果单是Feign,那么加@RequestMapping没问题,但是如果再配上Hystrix,就会重复注册两次url。
方法1:去掉@RequestMapping(“/xxx”)
方法2:在每个方法前手写uri(如果不嫌麻烦的话…),或者定义一个常量去拼接,例如:@GetMapping("/xxx/xxx")
以下有三个服务,分别为user-api、user-provider:、user-consumer,在调用user-provider服务时,发生异常(请求超时等)就会调用user-consumer中的UserProviderBack类中相应的方法。
user-api:
//@RequestMapping("/user")
public interface RegisterApi {
@GetMapping("/isAlive")
String isAlive();
}
或者
public interface RegisterApi {
@GetMapping("/user/isAlive")
String isAlive();
}
user-provider:
@RestController
public class UserController implements RegisterApi {
@Override
public String isAlive() {
return "OK";
}
}
user-consumer:
@FeignClient(name = "user-provider", fallback = UserProviderBack.class)
public interface UserConsumerService extends RegisterApi {
@GetMapping("/isAlive")
String isAlive();
}
@Component
public class UserProviderBack implements UserConsumerService {
@Override
public String isAlive() {
return "降级了";
}
}
标签:bean 一个 问题 except end com class client nts
原文地址:https://www.cnblogs.com/Ming-Yi/p/14119436.html