使用Feign调用Hystrix进行断路器的学习,一直报错There is already ‘computeClientHystrix’ bean method mapped
相关类如下:
调用远程服务的接口类
@FeignClient(value="COMPUTE-SERVICE", fallback=ComputeClientHystrix.class)
@RequestMapping("computer")
public interface ComputerController {
@RequestMapping(method = RequestMethod.GET, value = "/add")
Integer add(@RequestParam(value = "a") Integer a, @RequestParam(value = "b") Integer b);
}
实现接口类的实现类:
@Component
public class ComputeClientHystrix implements ComputerController{
@Override
public Integer add(@RequestParam(value = "a") Integer a, @RequestParam(value = "b") Integer b) {
return -9999;
}
}
看了下日志,先map了实现类的add方法,然后又去map了接口中的add方法,因为两者的继承关系,所以add方法的url是相同的,所以spring认为两个方法重复了,就报错了
[ main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/computer/add],methods=[GET]}" onto public java.lang.Integer com.zeng.learn.spring.cloud.consumer.server.ComputeClientHystrix.add(java.lang.Integer,java.lang.Integer)
2017-04-23 12:04:31.744 WARN 10320 --- [ main] ationConfigEmbeddedWebApplicationContext : Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'requestMappingHandlerMapping' defined in class path resource [org/springframework/boot/autoconfigure/web/WebMvcAutoConfiguration$EnableWebMvcConfiguration.class]: Invocation of init method failed; nested exception is java.lang.IllegalStateException: Ambiguous mapping. Cannot map 'com.zeng.learn.spring.cloud.consumer.ServerController.ComputerController' method
public abstract java.lang.Integer com.zeng.learn.spring.cloud.consumer.ServerController.ComputerController.add(java.lang.Integer,java.lang.Integer)
to {[/computer/add],methods=[GET]}: There is already 'computeClientHystrix' bean method
public java.lang.Integer com.zeng.learn.spring.cloud.consumer.server.ComputeClientHystrix.add(java.lang.Integer,java.lang.Integer) mapped.
改成如下的形式就ok了,应该是把url映射放在接口中。
接口:
@FeignClient(value="COMPUTE-SERVICE", fallback=ComputeClientHystrix.class)
public interface ComputerController {
@RequestMapping(method = RequestMethod.GET, value = "/computer/add")
Integer add(@RequestParam(value = "a") Integer a, @RequestParam(value = "b") Integer b);
}
实现:
@Component
public class ComputeClientHystrix implements ComputerController{
@Override
public Integer add(@RequestParam(value = "a") Integer a, @RequestParam(value = "b") Integer b) {
return -9999;
}
}
可以注意到RequestMapping中的value是“/conputer/add”,一般我们在spring mvc中,前面一致的都会放在class上,但是如果把@RequestMapping(value = “/computer”)放在接口上的话,还是会报错。
进入FeignClient注解里看了下属性,有一个path属性,是用来表示接口内所有方法请求的前缀的,而不是像springmvc,在接口上配置前缀@RequestMapping
@FeignClient(value = "spring-cloud-producer-chidao",fallback = StudentRemoteHystrix.class,path = "/student")
//@RequestMapping(value = "/student")
public interface StudentRemoteService {
@GetMapping(value = "/selectAll")
public Result<List<Student>> selectAll();
@GetMapping(value = "/findByName")
public Result<Student> findByName(@RequestParam(value = "name", required = true) String name);
这样就可以了