一、maven配置
引入feign默认会依赖hystrix,只要不排除就行。
org.springframework.cloud
spring-cloud-starter-feign
application配置开启feign的hystrix配置。
feign.hystrix.enabled=true
二、还是上文那个例子,测试
看到代理对象变为HystrixInvocationHandler,
接着往下调,后面跟单独的feign一样。只是在feignHandler上做了一个包装。
这里要注意,这个线程不是TOMCAT的工作线程,而是使用hystrix中的隔离任务线程池中执行。
正常的线程如下为nio-execute-
真正的feign远程接口调用是在hystrix的线程池中。
HystrixInvocationHandler.invoke方法为代理方法,最终调用到hystrixCommand.execute()
@Override
public Object invoke(final Object proxy, final Method method, final Object[] args)
throws Throwable {
// early exit if the invoked method is from java.lang.Object
// code is the same as ReflectiveFeign.FeignInvocationHandler
if ("equals".equals(method.getName())) {
try {
Object otherHandler =
args.length > 0 && args[0] != null ? Proxy.getInvocationHandler(args[0]) : null;
return equals(otherHandler);
} catch (IllegalArgumentException e) {
return false;
}
} else if ("hashCode".equals(method.getName())) {
return hashCode();
} else if ("toString".equals(method.getName())) {
return toString();
}
HystrixCommand
hystrixCommand.execute(),这个就是进入队列,然后在队列进行执行,同步等待。