SpringCloud之Feign支持降级处理

1.HystrixInvocationHandler

final class HystrixInvocationHandler implements InvocationHandler {
	
	private final Map<Method, MethodHandler> dispatch;
	
    private final FallbackFactory<?> fallbackFactory;
  
	public Object invoke(final Object proxy, final Method method, final Object[] args) throws Throwable {
	    ...
	    HystrixCommand<Object> hystrixCommand =
	        new HystrixCommand<Object>(setterMethodMap.get(method)) {
	          @Override
	          protected Object run() throws Exception {
	             // 属性dispatch的kv值如图所示
	             return HystrixInvocationHandler.this.dispatch.get(method).invoke(args);
	
	          @Override
	          protected Object getFallback() {
	            if (fallbackFactory == null) {
	              return super.getFallback();
	            }
                Object fallback = fallbackFactory.create(getExecutionException());
                Object result = fallbackMethodMap.get(method).invoke(fallback, args);
                if (isReturnsHystrixCommand(method)) {
                  return ((HystrixCommand) result).execute();
                } else if (isReturnsObservable(method)) {
                  return ((Observable) result).toBlocking().first();
                } else if (isReturnsSingle(method)) {
                  return ((Single) result).toObservable().toBlocking().first();
                } else if (isReturnsCompletable(method)) {
                  ((Completable) result).await();
                  return null;
                } else if (isReturnsCompletableFuture(method)) {
                  return ((Future) result).get();
                } else {
                  return result;
                }
	          }
	        };
	     ...
	    return hystrixCommand.execute();
	  }
}

在这里插入图片描述

你可能感兴趣的:(spring,cloud,java,spring)