1、使用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认为两个方法重复了,就报错了
2017-04-23 12:04:31.741 INFO 10320 --- [ 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放到实现类中。注意接口中的方法要注明GET/POST,不然是会报错的。
接口:
@FeignClient(value="COMPUTE-SERVICE", fallback=ComputeClientHystrix.class)
public interface ComputerController {
@RequestMapping(method = RequestMethod.GET)
Integer add(@RequestParam(value = "a") Integer a, @RequestParam(value = "b") Integer b);
}
实现:
@Component
@RequestMapping("computer")
public class ComputeClientHystrix implements ComputerController{
@Override
@RequestMapping(method = RequestMethod.GET, value = "/add")
public Integer add(@RequestParam(value = "a") Integer a, @RequestParam(value = "b") Integer b) {
return -9999;
}
}
2、2017-05-18更新
今天学习网关的时候发现原来关于断路器的测试有问题。之前将url的映射放在实现类中,可以做到启动应用,也仅仅只是启动应用。这种情况下,不管computer-service是否可用,都只是按照正常的函数执行流程返回-9999
应该是把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;
}
}
这样子的话,当服务可用是返回正常值,如果服务不可用返回-9999
3、对于以上2所说的情况,可以注意到RequestMapping中的value是“/conputer/add”,一般我们在spring mvc中,前面一致的都会放在class上,但是如果把@RequestMapping(value = “/computer”)放在接口上的话,还是会报错1中所说的错误。
@FeignClient(value="COMPUTE-SERVICE", fallback=ComputeClientHystrix.class)
@RequestMapping(value = "/computer")
public interface ComputerController {
@RequestMapping(method = RequestMethod.GET, value = "/add")
Integer add(@RequestParam(value = "a") Integer a, @RequestParam(value = "b") Integer b);
}
只不过这次是先注册了接口中的方法,后面注册实现中的方法发生了冲突,报错。
20170831更新
对于上述第3点,今天偶然进入FeignClient注解里看了下属性,有一个path属性,是用来表示接口内所有方法请求的前缀的,而不是像springmvc,在接口上配置前缀@RequestMapping
4、设置超时时间
默认全局:
hystrix.command.default.execution.isolation.thread.timeoutInMilliseconds:6000
某个方法:
hystrix.command.“接口#方法名(参数)”.execution.isolation.thread.timeoutInMilliseconds:6000
例如:
hystrix.command.“MessageService#sendMsg(String)”.execution.isolation.thread.timeoutInMilliseconds:6000
参考链接:
https://segmentfault.com/a/1190000009849932
文章中说的是配置文件更灵活一点,其实也可以自己搞个注解,然后从method中获取注解的超时时间,自己拼接commandKey。
禁用超时时间
hystrix.command.default.execution.enabled:false
禁用feign的hystrix
feign.hystrix.enabled:false