Java Code Examples for org.apache.dubbo.rpc.Invoker#invoke()

The following examples show how to use org.apache.dubbo.rpc.Invoker#invoke() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: Resilience4jCircuitBreakerFilter.java    From dubbo-samples with Apache License 2.0 6 votes vote down vote up
@Override
public Result invoke(Invoker<?> invoker, Invocation invocation) throws RpcException {
    System.out.println("**************** Enter CircuitBreaker ****************");
    long countLong = count.incrementAndGet();
    long start = 0;
    try {
        CircuitBreakerUtils.isCallPermitted(circuitBreaker);
        start = System.nanoTime();
        Result result = invoker.invoke(invocation);
        if (result.hasException()) {
            doThrowException(result.getException(), start);
            return result;
        }
        long durationInNanos = System.nanoTime() - start;
        circuitBreaker.onSuccess(durationInNanos);
        return result;
    } catch (CircuitBreakerOpenException cbo) {

        doCircuitBreakerOpenException(cbo, countLong, breakCount.incrementAndGet());
        throw cbo;
    } catch (Throwable throwable) {
        doThrowException(throwable, start);
        throw throwable;
    }
}
 
Example 2
Source File: DubboSofaTracerFilter.java    From sofa-tracer with Apache License 2.0 6 votes vote down vote up
@Override
public Result invoke(Invoker<?> invoker, Invocation invocation) throws RpcException {
    // do not record
    if ("$echo".equals(invocation.getMethodName())) {
        return invoker.invoke(invocation);
    }

    RpcContext rpcContext = RpcContext.getContext();
    // get appName
    if (StringUtils.isBlank(this.appName)) {
        this.appName = SofaTracerConfiguration
            .getProperty(SofaTracerConfiguration.TRACER_APPNAME_KEY);
    }
    // get span kind by rpc request type
    String spanKind = spanKind(rpcContext);
    Result result;
    if (spanKind.equals(Tags.SPAN_KIND_SERVER)) {
        result = doServerFilter(invoker, invocation);
    } else {
        result = doClientFilter(rpcContext, invoker, invocation);
    }
    return result;
}
 
Example 3
Source File: DubboAppContextFilter.java    From dubbo-sentinel-support with Apache License 2.0 5 votes vote down vote up
@Override
public Result invoke(Invoker<?> invoker, Invocation invocation) throws RpcException {
    String application = invoker.getUrl().getParameter(Constants.APPLICATION_KEY);
    if (application != null) {
        RpcContext.getContext().setAttachment(DubboUtils.SENTINEL_DUBBO_APPLICATION_KEY, application);
    }
    return invoker.invoke(invocation);
}
 
Example 4
Source File: Resilience4jRateLimiterFilter.java    From dubbo-samples with Apache License 2.0 5 votes vote down vote up
@Override
public Result invoke(Invoker<?> invoker, Invocation invocation) throws RpcException {
    try {
        System.out.println("**************** Enter RateLimiter ****************");
        RateLimiter.waitForPermission(rateLimiter);
        return invoker.invoke(invocation);
    } catch (RequestNotPermitted rnp) {
        System.err.println("---------------- Rate Limiter! Try it later! ----------------");
        throw rnp;
    } catch (Throwable throwable) {
        System.err.println("........" + throwable.getMessage());
        throw throwable;
    }
}
 
Example 5
Source File: AsyncPostprocessFilter.java    From dubbo-samples with Apache License 2.0 5 votes vote down vote up
@Override
public Result invoke(Invoker<?> invoker, Invocation invocation) throws RpcException {
    RpcContext context = RpcContext.getContext();
    String filters = (String) context.getAttachment("filters");
    if (StringUtils.isEmpty(filters)) {
        filters = "";
    }
    filters += " async-post-process-filter";
    context.setAttachment("filters", filters);

    return invoker.invoke(invocation);
}
 
Example 6
Source File: LegacyListenableFilter.java    From dubbo-samples with Apache License 2.0 5 votes vote down vote up
@Override
public Result invoke(Invoker<?> invoker, Invocation invocation) throws RpcException {
    RpcContext context = RpcContext.getContext();
    String filters = (String) context.getAttachment("filters");
    if (StringUtils.isEmpty(filters)) {
        filters = "";
    }
    filters += " legacy-block-filter";
    context.setAttachment("filters", filters);

    return invoker.invoke(invocation);
}
 
Example 7
Source File: BirdExceptionFilter.java    From bird-java with MIT License 5 votes vote down vote up
@Override
public Result invoke(Invoker<?> invoker, Invocation invocation) throws RpcException {
    try {
        return invoker.invoke(invocation);
    } catch (RuntimeException e) {
        logger.error("Got unchecked and undeclared exception which called by " + RpcContext.getContext().getRemoteHost()
                + ". service: " + invoker.getInterface().getName() + ", method: " + invocation.getMethodName()
                + ", exception: " + e.getClass().getName() + ": " + e.getMessage(), e);
        throw e;
    }
}
 
Example 8
Source File: ThrowableAsyncFilter.java    From dubbo-samples with Apache License 2.0 5 votes vote down vote up
@Override
public Result invoke(Invoker<?> invoker, Invocation invocation) throws RpcException {
    if (invocation != null) {
        throw new RuntimeException("exception before invoke()");
    }
    return invoker.invoke(invocation);
}
 
Example 9
Source File: DubboAppContextFilter.java    From Sentinel with Apache License 2.0 5 votes vote down vote up
@Override
public Result invoke(Invoker<?> invoker, Invocation invocation) throws RpcException {
    String application = invoker.getUrl().getParameter(CommonConstants.APPLICATION_KEY);
    if (application != null) {
        RpcContext.getContext().setAttachment(DubboUtils.SENTINEL_DUBBO_APPLICATION_KEY, application);
    }
    return invoker.invoke(invocation);
}
 
Example 10
Source File: DubboAppContextFilter.java    From Sentinel-Dashboard-Nacos with Apache License 2.0 5 votes vote down vote up
@Override
public Result invoke(Invoker<?> invoker, Invocation invocation) throws RpcException {
    String application = invoker.getUrl().getParameter(Constants.APPLICATION_KEY);
    if (application != null) {
        RpcContext.getContext().setAttachment(DubboUtils.SENTINEL_DUBBO_APPLICATION_KEY, application);
    }
    return invoker.invoke(invocation);
}
 
Example 11
Source File: OnErrorThrowableAsyncFilter.java    From dubbo-samples with Apache License 2.0 4 votes vote down vote up
@Override
public Result invoke(Invoker<?> invoker, Invocation invocation) throws RpcException {
    return invoker.invoke(invocation);
}
 
Example 12
Source File: NormalAsyncFilter.java    From dubbo-samples with Apache License 2.0 4 votes vote down vote up
@Override
public Result invoke(Invoker<?> invoker, Invocation invocation) throws RpcException {
    return invoker.invoke(invocation);
}
 
Example 13
Source File: NormalSyncFilter.java    From dubbo-samples with Apache License 2.0 4 votes vote down vote up
@Override
public Result invoke(Invoker<?> invoker, Invocation invocation) throws RpcException {
    Result result = invoker.invoke(invocation);
    System.out.println("Normal sync filter, the value may be null: " + result.getValue());
    return result;
}
 
Example 14
Source File: ProviderErrorAsyncFilter.java    From dubbo-samples with Apache License 2.0 4 votes vote down vote up
@Override
public Result invoke(Invoker<?> invoker, Invocation invocation) throws RpcException {
    return invoker.invoke(invocation);
}
 
Example 15
Source File: OnResponseThrowableAsyncFilter.java    From dubbo-samples with Apache License 2.0 4 votes vote down vote up
@Override
public Result invoke(Invoker<?> invoker, Invocation invocation) throws RpcException {
    return invoker.invoke(invocation);
}
 
Example 16
Source File: DubboSofaTracerFilter.java    From sofa-tracer with Apache License 2.0 4 votes vote down vote up
/**
 * rpc client handler
 * @param invoker
 * @param invocation
 * @return
 */
private Result doServerFilter(Invoker<?> invoker, Invocation invocation) {
    if (dubboProviderSofaTracer == null) {
        this.dubboProviderSofaTracer = DubboProviderSofaTracer
            .getDubboProviderSofaTracerSingleton();
    }
    SofaTracerSpan sofaTracerSpan = serverReceived(invocation);
    appendRpcServerSpanTags(invoker, sofaTracerSpan);
    Result result;
    Throwable exception = null;
    try {
        result = invoker.invoke(invocation);
        if (result == null) {
            return null;
        } else {
            appendElapsedTimeTags(invocation, sofaTracerSpan, result, false);
        }
        if (result.hasException()) {
            exception = result.getException();
        }
        return result;
    } catch (RpcException e) {
        exception = e;
        throw e;
    } catch (Throwable t) {
        exception = t;
        throw new RpcException(t);
    } finally {
        String resultCode = SofaTracerConstant.RESULT_CODE_SUCCESS;
        if (exception != null) {
            if (exception instanceof RpcException) {
                sofaTracerSpan.setTag(Tags.ERROR.getKey(), exception.getMessage());
                RpcException rpcException = (RpcException) exception;
                if (rpcException.isBiz()) {
                    resultCode = String.valueOf(rpcException.getCode());
                }
            } else {
                resultCode = SofaTracerConstant.RESULT_CODE_ERROR;
            }
        }
        dubboProviderSofaTracer.serverSend(resultCode);
    }
}
 
Example 17
Source File: DubboSofaTracerFilter.java    From sofa-tracer with Apache License 2.0 4 votes vote down vote up
/**
 * rpc client handler
 * @param rpcContext
 * @param invoker
 * @param invocation
 * @return
 */
private Result doClientFilter(RpcContext rpcContext, Invoker<?> invoker, Invocation invocation) {
    // to build tracer instance
    if (dubboConsumerSofaTracer == null) {
        this.dubboConsumerSofaTracer = DubboConsumerSofaTracer
            .getDubboConsumerSofaTracerSingleton();
    }
    // get methodName
    String methodName = rpcContext.getMethodName();
    // get service interface
    String service = invoker.getInterface().getSimpleName();
    // build a dubbo rpc span
    SofaTracerSpan sofaTracerSpan = dubboConsumerSofaTracer.clientSend(service + "#"
                                                                       + methodName);
    // set tags to span
    appendRpcClientSpanTags(invoker, sofaTracerSpan);
    // do serialized and then transparent transmission to the rpc server
    String serializedSpanContext = sofaTracerSpan.getSofaTracerSpanContext()
        .serializeSpanContext();
    //put into attachments
    invocation.getAttachments().put(CommonSpanTags.RPC_TRACE_NAME, serializedSpanContext);
    // check invoke type
    boolean isAsync = RpcUtils.isAsync(invoker.getUrl(), invocation);
    boolean isOneWay = false;
    if (isAsync) {
        sofaTracerSpan.setTag(CommonSpanTags.INVOKE_TYPE, "future");
    } else {
        isOneWay = RpcUtils.isOneway(invoker.getUrl(), invocation);
        if (isOneWay) {
            sofaTracerSpan.setTag(CommonSpanTags.INVOKE_TYPE, "oneway");
        } else {
            sofaTracerSpan.setTag(CommonSpanTags.INVOKE_TYPE, "sync");
        }
    }
    Result result;
    Throwable exception = null;
    String resultCode = SofaTracerConstant.RESULT_CODE_SUCCESS;
    try {
        // do invoke
        result = invoker.invoke(invocation);
        // check result
        if (result == null) {
            // isOneWay, we think that the current request is successful
            if (isOneWay) {
                sofaTracerSpan.setTag(CommonSpanTags.RESP_SIZE, 0);
            }
        } else {
            // add elapsed time
            appendElapsedTimeTags(invocation, sofaTracerSpan, result,true);
        }
    } catch (RpcException e) {
        exception = e;
        throw e;
    } catch (Throwable t) {
        exception = t;
        throw new RpcException(t);
    } finally {
        if (exception != null) {
            if (exception instanceof RpcException) {
                sofaTracerSpan.setTag(Tags.ERROR.getKey(),exception.getMessage());
                RpcException rpcException = (RpcException) exception;
                resultCode = String.valueOf(rpcException.getCode());
            } else {
                resultCode = SofaTracerConstant.RESULT_CODE_ERROR;
            }
        }

        if (!isAsync) {
            dubboConsumerSofaTracer.clientReceive(resultCode);
        } else {
            SofaTraceContext sofaTraceContext = SofaTraceContextHolder.getSofaTraceContext();
            SofaTracerSpan clientSpan = sofaTraceContext.pop();
            if (clientSpan != null) {
                // Record client send event
                sofaTracerSpan.log(LogData.CLIENT_SEND_EVENT_VALUE);
            }
            // cache the current span
            TracerSpanMap.put(getTracerSpanMapKey(invoker), sofaTracerSpan);
            if (clientSpan != null && clientSpan.getParentSofaTracerSpan() != null) {
                //restore parent
                sofaTraceContext.push(clientSpan.getParentSofaTracerSpan());
            }
            CompletableFuture<Object> future = (CompletableFuture<Object>) RpcContext.getContext().getFuture();
            future.whenComplete((object, throwable)-> {
                if (throwable instanceof TimeoutException) {
                    sofaTracerSpan.setTag(Tags.ERROR.getKey(),throwable.getMessage());
                    dubboConsumerSofaTracer.clientReceiveTagFinish(sofaTracerSpan, SofaTracerConstant.RESULT_CODE_TIME_OUT);
                }
            });
        }
    }
    return result;
}
 
Example 18
Source File: TestClientFilter.java    From dubbo-samples with Apache License 2.0 4 votes vote down vote up
@Override
public Result invoke(Invoker<?> invoker, Invocation invocation) throws RpcException {
    return invoker.invoke(invocation);
}
 
Example 19
Source File: LogFilter.java    From dubbo-samples with Apache License 2.0 4 votes vote down vote up
@Override
public Result invoke(Invoker<?> invoker, Invocation invocation) throws RpcException {
    System.out.println(invocation.getMethodName() + "is invoked");
    return invoker.invoke(invocation);
}
 
Example 20
Source File: DubboHmilyTransactionFilter.java    From hmily with Apache License 2.0 4 votes vote down vote up
@Override
public Result invoke(final Invoker<?> invoker, final Invocation invocation) throws RpcException {
    String methodName = invocation.getMethodName();
    Class clazz = invoker.getInterface();
    Class[] args = invocation.getParameterTypes();
    final Object[] arguments = invocation.getArguments();
    Method method = null;
    Hmily hmily = null;
    try {
        converterParamsClass(args, arguments);
        method = clazz.getMethod(methodName, args);
        hmily = method.getAnnotation(Hmily.class);
    } catch (Exception ex) {
        LOGGER.error("hmily find method error {} ", ex);
    }
    if (Objects.nonNull(hmily)) {
        try {
            final HmilyTransactionContext hmilyTransactionContext = HmilyTransactionContextLocal.getInstance().get();
            if (Objects.nonNull(hmilyTransactionContext)) {
                RpcMediator.getInstance().transmit(RpcContext.getContext()::setAttachment, hmilyTransactionContext);
                final Result result = invoker.invoke(invocation);
                //if result has not exception
                if (!result.hasException()) {
                    final HmilyParticipant hmilyParticipant = buildParticipant(hmilyTransactionContext, hmily, method, clazz, arguments, args);
                    if (hmilyTransactionContext.getRole() == HmilyRoleEnum.INLINE.getCode()) {
                        hmilyTransactionExecutor.registerByNested(hmilyTransactionContext.getTransId(),
                                hmilyParticipant);
                    } else {
                        hmilyTransactionExecutor.enlistParticipant(hmilyParticipant);
                    }
                } else {
                    throw new HmilyRuntimeException("rpc invoke exception{}", result.getException());
                }
                return result;
            }
            return invoker.invoke(invocation);
        } catch (RpcException e) {
            e.printStackTrace();
            throw e;
        }
    } else {
        return invoker.invoke(invocation);
    }
}