use of com.github.df.restypass.exception.execute.FallbackException in project RestyPass by darren-fu.
the class RestyProxyInvokeHandler method invoke.
@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
if (isSpecialMethod(method)) {
return handleSpecialMethod(proxy, method, args);
}
Object result;
// 创建请求载体,RestyCommand
RestyCommand restyCommand = new DefaultRestyCommand(method.getDeclaringClass(), method, method.getGenericReturnType(), args, restyCommandContext);
// 获取过滤器
List<CommandFilter> filterList = commandFilterContext.getFilterList();
// 执行过滤器
for (CommandFilter commandFilter : filterList) {
if (CommandFilterType.BEFOR_EXECUTE.equals(commandFilter.getFilterType()) && commandFilter.shouldFilter(restyCommand)) {
commandFilter.before(restyCommand);
}
}
// 创建负载均衡器
LoadBalancer loadBalancer = LoadBalanceFactory.createLoadBalancerForService(restyCommand.getServiceName(), restyCommand.getRestyCommandConfig().getLoadBalancer());
// 为executor设置服务容器
commandExecutor.setServerContext(serverContext);
try {
if (commandExecutor.executable(restyCommand)) {
result = commandExecutor.execute(loadBalancer, restyCommand);
} else {
throw new IllegalStateException("Resty command is not executable:" + restyCommand);
}
} catch (RestyException ex) {
if (fallbackExecutor.executable(restyCommand)) {
if (log.isDebugEnabled()) {
log.debug("{}使用降级服务", restyCommand.getPath());
}
try {
result = fallbackExecutor.execute(restyCommand);
} catch (FallbackException fe) {
log.warn("服务{}降级发生异常:{}", restyCommand.getPath(), fe.getMessage());
// 抛出原始Resty异常
throw ex;
}
} else {
log.warn("请求{}发生异常:{}", restyCommand.getPath(), ex.getMessage());
throw ex;
}
}
return result;
}
use of com.github.df.restypass.exception.execute.FallbackException in project RestyPass by darren-fu.
the class RestyFallbackExecutor method findAndInvokeMethodInFallbackClass.
private Object findAndInvokeMethodInFallbackClass(RestyCommand restyCommand, Object fallbackObj) throws InvocationTargetException, IllegalAccessException, NoSuchMethodException {
Method serviceMethod = restyCommand.getServiceMethod();
String methodName = serviceMethod.getName();
Class fallbackClass = fallbackObj.getClass();
Method method = getMethod(fallbackClass, methodName, copyParamsWithException(restyCommand));
if (method != null) {
return invokeMethod(method, fallbackObj, copyArgsWithException(restyCommand));
}
method = getMethod(fallbackClass, methodName, serviceMethod.getParameterTypes());
if (method == null) {
throw new FallbackException(fallbackClass.getSimpleName() + "中没有发现没有合适的降级方法:" + methodName);
}
return invokeMethod(method, fallbackObj, restyCommand.getArgs());
}
use of com.github.df.restypass.exception.execute.FallbackException in project RestyPass by darren-fu.
the class RestyFallbackExecutor method execute.
@Override
public Object execute(RestyCommand restyCommand) throws FallbackException {
RestyCommandConfig config = restyCommand.getRestyCommandConfig();
String serviceName = restyCommand.getServiceName();
Class fallbackClass = config.getFallbackClass();
// 获取降级服务实例
Object fallbackObj = fallbackObjMap.get(serviceName);
if (fallbackObj == null) {
Object fo = getFallbackObject(restyCommand);
fallbackObjMap.putIfAbsent(serviceName, fo);
fallbackObj = fallbackObjMap.get(serviceName);
}
if (fallbackObj == null || fallbackObj == RestyService.Noop.noop) {
throw new FallbackException("无法获取指定的降级服务实例:" + fallbackClass);
}
try {
return findAndInvokeMethodInFallbackClass(restyCommand, fallbackObj);
} catch (InvocationTargetException | IllegalAccessException | NoSuchMethodException e) {
throw new FallbackException(e);
}
}
Aggregations