Feign調用Hystrix報錯There is already ‘computeClientHystrix’ bean method mapped

使用Feign調用Hystrix進行斷路器的學習,一直報錯There is already ‘computeClientHystrix’ bean method mapped 
相關類以下: 
調用遠程服務的接口類java

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

實現接口類的實現類:web

@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認爲兩個方法重複了,就報錯了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映射放在接口中。 
接口:mvc

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

實現:app

@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」)放在接口上的話,仍是會報錯。ide

進入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);

這樣就能夠了ui

相關文章
相關標籤/搜索