Use Feign to call Hystrix to learn the circuit breaker, and always report the error There is already 'computed client Hystrix' bean method mapped
Related classes are as follows:
Interface class for calling remote service
@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); }
Implementation class of implementation interface class:
@Component public class ComputeClientHystrix implements ComputerController{ @Override public Integer add(@RequestParam(value = "a") Integer a, @RequestParam(value = "b") Integer b) { return -9999; } }
After reading the log, first map the add method of the implementation class, then map the add method in the interface. Because of the inheritance relationship between the two, the url of the add method is the same, so spring thinks that the two methods are repeated, and the error is reported
[ 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.
It's ok to change it to the following form, which is to put the url mapping in the interface.
Interface:
@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); }
Realization:
@Component public class ComputeClientHystrix implements ComputerController{ @Override public Integer add(@RequestParam(value = "a") Integer a, @RequestParam(value = "b") Integer b) { return -9999; } }
It can be noted that the value in RequestMapping is "/ consumer / add". Generally, in spring mvc, all the previous values will be placed on the class, but if @ RequestMapping(value = "/ computer") is placed on the interface, an error will still be reported.
Enter the FeignClient annotation and look at the following properties. There is a path property, which is used to represent the prefixes of all method requests in the interface, not like spring MVC. Configure the prefix @ RequestMapping on the interface
@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);
That's it