1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75
| @Activate(group = Constants.CONSUMER, value = "consumerFilter", order = -40000) public class ConsumerFilterImpl implements Filter {
@Override @SuppressWarnings({"unchecked", "rawtypes"}) public Result invoke(Invoker<?> invoker, Invocation invocation) throws RpcException { final RpcContext context = RpcContext.getContext(); ......
try { final long startTime = System.nanoTime() / 1000; final Result invoke = invoker.invoke(invocation); final long endTime = System.nanoTime() / 1000; log.info("[{}] 消费DUBBO服务[{}]结束, 凭证: {}, 耗时: [{}]μs", PlatformConstants.APPNAME, invocation.getMethodName(), proof, endTime - startTime); final Object value = invoke.getValue(); if (value instanceof Map || value instanceof ExceptionResult) { ExceptionResult exception = null; if (value instanceof ExceptionResult) { exception = (ExceptionResult) value; } else { final Map map = (Map) value; if ((boolean) map.getOrDefault("failed", false)) { exception = new ExceptionResult((int) map.getOrDefault("type", 1), String.valueOf(map.getOrDefault("code", PlatformExceptionEnum.SYSTEM_BUSY.getCode())), String.valueOf(map.getOrDefault("message", PlatformExceptionEnum.SYSTEM_BUSY.getMessage())), String.valueOf(map.getOrDefault("exception", ""))); } }
if (null != exception) { final int type = exception.getType(); if (type == PlatformConstants.EXCEPTION_CODE_PASSED) { log.warn("[DUBBO] 业务内部校验不通过, 凭证: {}, 原因: {}", proof, exception.getMessage()); return new RpcResult(new PassedException(exception.getCode(), exception.getMessage())); } else if (type == PlatformConstants.EXCEPTION_CODE_REFUND) { log.warn("[DUBBO] 调用了未授权的资源, 凭证: {}, 原因: {}", proof, exception.getMessage()); return new RpcResult(new RefundException(exception.getCode(), exception.getMessage())); } else if (type == PlatformConstants.EXCEPTION_CODE_INNER) { log.warn("[DUBBO] 内部调用异常, 凭证: {}, 原因: {}", proof, exception.getException()); return new RpcResult(new InnerException(exception.getMessage(), exception.getException())); } else { log.error("[DUBBO] 调用未知异常, 凭证: {}, 原因: {}", proof, exception.getException()); return new RpcResult(new InnerException(exception.getMessage(), exception.getException())); } } } RpcContext.getServerContext().setAttachments(invoke.getAttachments()); return invoke; } catch (RpcException ex) { log.warn("[DUBBO] 调用出现已知异常, 凭证: {}, 异常: {}, {}, {}", proof, ex.getCode(), ex.getMessage(), ex.getLocalizedMessage()); if (ex.isSerialization()) { throw new PassedException(PlatformExceptionEnum.SERIALIZE_ERROR); } else if (ex.isForbidded()) { throw new PassedException(PlatformExceptionEnum.FORBIDDEN); } else if (ex.isNetwork()) { throw new PassedException(PlatformExceptionEnum.CLIENT_ERROR); } else if (ex.isTimeout()) { throw new PassedException(PlatformExceptionEnum.CLIENT_TIMEOUT); } else { throw new InnerException(ex.getMessage(), ex.getCause()); } } catch (GenericException ex) { log.error("[DUBBO] 调用内部自定义异常, 凭证: {}, 异常: {}", proof, ex); throw new InnerException(ex.getExceptionMessage(), ex.getCause()); } catch (Exception ex) { log.error("[DUBBO] 调用未知异常, 凭证: {}, 异常: {}", proof, ex); throw new InnerException(ex.getMessage(), ex.getCause()); } } }
|