Exemplo n.º 1
0
  protected RpcResponse doExecute(RpcRequest request) {
    Connection c = null;
    ConnectionSpec auth = null;
    try {
      try {
        outputRequestInfo("doExecute: Before validateRequest...", request);
        request = validateRequest(request);
        outputRequestInfo("doExecute: After validateRequest...", request);
        RpcHost host = request.getHost();
        auth = createConnectionSpec(request);
        outputAuthInfo(auth);
        c = this.connectionFactory.getConnection(host, auth);
      } catch (RpcException e) {
        logger.error("doExecute: RpcException occurred.  Error: " + e.getMessage(), e);
        fireRpcExceptionEvent(request, e);
        throw getExceptionTranslator()
            .translate("open", RpcUriUtils.sanitize(request.getUriString(), auth), e);
      }
      try {
        logger.debug("executing {}", RpcUriUtils.sanitize(request.getUriString(), auth));

        RpcResponse response = c.send(request);

        logger.debug("received  {}", response.toString());

        fireRpcResponseEvent(request, response);

        return response;
      } catch (RpcException e) {
        fireRpcExceptionEvent(request, e);
        throw getExceptionTranslator()
            .translate("send", RpcUriUtils.sanitize(request.getUriString(), auth), e);
      }
    } catch (DataAccessException e) {
      if (e instanceof PermissionDeniedDataAccessException) {
        logger.debug(
            "permission denied in " + RpcUriUtils.sanitize(request.getUriString(), auth), e);
      } else {
        logger.error("error in  " + RpcUriUtils.sanitize(request.getUriString(), auth), e);
      }
      throw e;
    } finally {
      try {
        if (c != null) c.close();
      } catch (RpcException e) {
        fireRpcExceptionEvent(request, e);
        throw getExceptionTranslator()
            .translate("close", RpcUriUtils.sanitize(request.getUriString(), auth), e);
      }
    }
  }
 private Result wrapBreakerInvoke(Invoker<?> invoker, Invocation invocation) throws RpcException {
   // 首先检查是否需要进入服务降级流程
   if (checkNeedCircuitBreak(invoker, invocation)) {
     logger.info(
         "[{}] activate the circuit break for url [{}],invoke method [{}]",
         localHost,
         invoker.getUrl(),
         invocation.getMethodName());
     // 进入服务降级
     return doCircuitBreak(invoker, invocation);
   }
   try {
     Result result = invoker.invoke(invocation);
     // 将该服务从服务降级中恢复出来
     toBeNormal(invoker, invocation);
     return result;
   } catch (RpcException e) {
     // 如果是请求超时或者网络异常,进行异常统计
     if (!e.isBiz()) {
       caughtException(invoker, invocation, e);
     }
     throw e;
   }
 }
 @Override
 protected RpcException mapException(Exception ex) {
   return RpcException.mapException(ex);
 }
 @Override
 public void setException(Throwable t) {
   handler.failed(RpcException.mapException(t));
 }