Java Code Examples for com.alipay.sofa.rpc.filter.FilterInvoker#invoke()

The following examples show how to use com.alipay.sofa.rpc.filter.FilterInvoker#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: CustomEchoFilter2.java    From sofa-rpc with Apache License 2.0 6 votes vote down vote up
@Override
public SofaResponse invoke(FilterInvoker invoker, SofaRequest request) throws SofaRpcException {
    LOGGER.info("echo2 request : {}, {}", request.getInterfaceName() + "." + request.getMethodName(),
        request.getMethodArgs());

    SofaResponse response = invoker.invoke(request);

    if (response == null) {
        return response;
    } else if (response.isError()) {
        LOGGER.info("server rpc error: {}", response.getErrorMsg());
    } else {
        Object ret = response.getAppResponse();
        if (ret instanceof Throwable) {
            LOGGER.error("server biz error: {}", (Throwable) ret);
        } else {
            LOGGER.info("echo2 response : {}", response.getAppResponse());
        }
    }

    return response;
}
 
Example 2
Source File: CustomEchoFilter.java    From sofa-rpc with Apache License 2.0 6 votes vote down vote up
@Override
public SofaResponse invoke(FilterInvoker invoker, SofaRequest request) throws SofaRpcException {
    LOGGER.info("echo request : {}, {}", request.getInterfaceName() + "." + request.getMethodName(),
        request.getMethodArgs());

    SofaResponse response = invoker.invoke(request);

    if (response == null) {
        return response;
    } else if (response.isError()) {
        LOGGER.info("server rpc error: {}", response.getErrorMsg());
    } else {
        Object ret = response.getAppResponse();
        if (ret instanceof Throwable) {
            LOGGER.error("server biz error: {}", (Throwable) ret);
        } else {
            LOGGER.info("echo response : {}", response.getAppResponse());
        }
    }

    return response;
}
 
Example 3
Source File: ConsumerTracerFilter.java    From sofa-rpc with Apache License 2.0 6 votes vote down vote up
@Override
public SofaResponse invoke(FilterInvoker invoker, SofaRequest request) throws SofaRpcException {

    SofaTraceContext sofaTraceContext = SofaTraceContextHolder.getSofaTraceContext();
    SofaTracerSpan clientSpan = sofaTraceContext.getCurrentSpan();

    clientSpan.setTag(RpcSpanTags.INVOKE_TYPE, request.getInvokeType());

    RpcInternalContext context = RpcInternalContext.getContext();
    clientSpan.setTag(RpcSpanTags.ROUTE_RECORD,
        (String) context.getAttachment(RpcConstants.INTERNAL_KEY_ROUTER_RECORD));

    ProviderInfo providerInfo = context.getProviderInfo();
    if (providerInfo != null) {
        clientSpan.setTag(RpcSpanTags.REMOTE_APP, providerInfo.getStaticAttr(ProviderInfoAttrs.ATTR_APP_NAME));
        clientSpan.setTag(RpcSpanTags.REMOTE_IP, providerInfo.getHost() + ":" + providerInfo.getPort());
    }

    return invoker.invoke(request);
    // 因为异步的场景,所以received不写在这里
}
 
Example 4
Source File: SentinelSofaRpcConsumerFilter.java    From Sentinel with Apache License 2.0 5 votes vote down vote up
@Override
public SofaResponse invoke(FilterInvoker invoker, SofaRequest request) throws SofaRpcException {
    // Now only support sync invoke.
    if (request.getInvokeType() != null && !RpcConstants.INVOKER_TYPE_SYNC.equals(request.getInvokeType())) {
        return invoker.invoke(request);
    }

    String interfaceResourceName = getInterfaceResourceName(request);
    String methodResourceName = getMethodResourceName(request);

    Entry interfaceEntry = null;
    Entry methodEntry = null;
    try {
        interfaceEntry = SphU.entry(interfaceResourceName, ResourceTypeConstants.COMMON_RPC, EntryType.OUT);
        methodEntry = SphU.entry(methodResourceName, ResourceTypeConstants.COMMON_RPC,
            EntryType.OUT, getMethodArguments(request));

        SofaResponse response = invoker.invoke(request);

        traceResponseException(response, interfaceEntry, methodEntry);
        return response;
    } catch (BlockException e) {
        return SofaRpcFallbackRegistry.getConsumerFallback().handle(invoker, request, e);
    } catch (Throwable t) {
        throw traceOtherException(t, interfaceEntry, methodEntry);
    } finally {
        if (methodEntry != null) {
            methodEntry.exit(1, getMethodArguments(request));
        }

        if (interfaceEntry != null) {
            interfaceEntry.exit();
        }
    }
}
 
Example 5
Source File: SentinelSofaRpcProviderFilter.java    From Sentinel with Apache License 2.0 5 votes vote down vote up
@Override
public SofaResponse invoke(FilterInvoker invoker, SofaRequest request) throws SofaRpcException {
    // Now only support sync invoke.
    if (request.getInvokeType() != null && !RpcConstants.INVOKER_TYPE_SYNC.equals(request.getInvokeType())) {
        return invoker.invoke(request);
    }

    String callerApp = getApplicationName(request);
    String interfaceResourceName = getInterfaceResourceName(request);
    String methodResourceName = getMethodResourceName(request);

    Entry interfaceEntry = null;
    Entry methodEntry = null;
    try {
        ContextUtil.enter(methodResourceName, callerApp);

        interfaceEntry = SphU.entry(interfaceResourceName, ResourceTypeConstants.COMMON_RPC, EntryType.IN);
        methodEntry = SphU.entry(methodResourceName, ResourceTypeConstants.COMMON_RPC,
            EntryType.IN, getMethodArguments(request));

        SofaResponse response = invoker.invoke(request);

        traceResponseException(response, interfaceEntry, methodEntry);
        return response;
    } catch (BlockException e) {
        return SofaRpcFallbackRegistry.getProviderFallback().handle(invoker, request, e);
    } catch (Throwable t) {
        throw traceOtherException(t, interfaceEntry, methodEntry);
    } finally {
        if (methodEntry != null) {
            methodEntry.exit(1, getMethodArguments(request));
        }
        if (interfaceEntry != null) {
            interfaceEntry.exit();
        }
        ContextUtil.exit();
    }
}
 
Example 6
Source File: SampleClientFilter.java    From sofa-rpc-boot-projects with Apache License 2.0 5 votes vote down vote up
@Override
public SofaResponse invoke(FilterInvoker invoker, SofaRequest request) throws SofaRpcException {

    System.out.println("SampleFilter before client invoke");

    try {
        return invoker.invoke(request);
    } finally {
        System.out.println("SampleFilter after client invoke");
    }
}
 
Example 7
Source File: SampleServerFilter.java    From sofa-rpc-boot-projects with Apache License 2.0 5 votes vote down vote up
@Override
public SofaResponse invoke(FilterInvoker invoker, SofaRequest request) throws SofaRpcException {

    System.out.println("SampleFilter before server process");

    try {
        return invoker.invoke(request);
    } finally {
        System.out.println("SampleFilter after server process");
    }
}
 
Example 8
Source File: TestGlobalFilter.java    From sofa-rpc-boot-projects with Apache License 2.0 5 votes vote down vote up
@Override
public SofaResponse invoke(FilterInvoker invoker, SofaRequest request) throws SofaRpcException {
    if (request.getInterfaceName() != null &&
        request.getInterfaceName().equals("com.alipay.sofa.rpc.boot.globalfilter.GlobalFilterService")) {
        if (request.getMethodArgs()[0].equals("globalFilter")) {
            request.getMethodArgs()[0] = "globalFilter_change";
        }
    }
    return invoker.invoke(request);
}
 
Example 9
Source File: ParameterFilter.java    From sofa-rpc-boot-projects with Apache License 2.0 5 votes vote down vote up
@Override
public SofaResponse invoke(FilterInvoker invoker, SofaRequest request) throws SofaRpcException {
    if (invoker.getConfig() instanceof ProviderConfig) {
        providerParameters = invoker.getConfig().getParameters();
    } else {
        consumerParameters = invoker.getConfig().getParameters();
    }
    return invoker.invoke(request);
}
 
Example 10
Source File: ProviderTracerFilter.java    From sofa-rpc with Apache License 2.0 5 votes vote down vote up
@Override
public SofaResponse invoke(FilterInvoker invoker, SofaRequest request) throws SofaRpcException {
    SofaTracerSpan serverSpan = null;
    try {
        SofaTraceContext sofaTraceContext = SofaTraceContextHolder.getSofaTraceContext();
        serverSpan = sofaTraceContext.getCurrentSpan();
        if (serverSpan != null) {
            RpcInternalContext context = RpcInternalContext.getContext();
            serverSpan.setTag(RpcSpanTags.SERVICE, request.getTargetServiceUniqueName());
            serverSpan.setTag(RpcSpanTags.METHOD, request.getMethodName());
            serverSpan.setTag(RpcSpanTags.REMOTE_IP, context.getRemoteHostName()); // 客户端地址

            // 从请求里获取ConsumerTracerFilter额外传递的信息
            serverSpan.setTag(RpcSpanTags.REMOTE_APP, (String) request.getRequestProp(HEAD_APP_NAME));
            serverSpan.setTag(RpcSpanTags.PROTOCOL, (String) request.getRequestProp(HEAD_PROTOCOL));
            serverSpan.setTag(RpcSpanTags.INVOKE_TYPE, (String) request.getRequestProp(HEAD_INVOKE_TYPE));

            ProviderConfig providerConfig = (ProviderConfig) invoker.getConfig();
            serverSpan.setTag(RpcSpanTags.LOCAL_APP, providerConfig.getAppName());

            serverSpan.setTag(RpcSpanTags.SERVER_THREAD_POOL_WAIT_TIME,
                (Number) context.getAttachment(RpcConstants.INTERNAL_KEY_PROCESS_WAIT_TIME));
        }
        return invoker.invoke(request);
    } finally {
        if (serverSpan != null) {
            serverSpan.setTag(RpcSpanTags.SERVER_BIZ_TIME,
                (Number) RpcInternalContext.getContext().getAttachment(RpcConstants.INTERNAL_KEY_IMPL_ELAPSE));
        }
    }
}
 
Example 11
Source File: MockTimeoutFilter.java    From sofa-rpc with Apache License 2.0 5 votes vote down vote up
@Override
public SofaResponse invoke(FilterInvoker invoker, SofaRequest request) throws SofaRpcException {
    try {
        Thread.sleep(sleep);
    } catch (InterruptedException e) {
        Thread.currentThread().interrupt();
    }
    return invoker.invoke(request);
}
 
Example 12
Source File: HystrixFilter.java    From sofa-rpc with Apache License 2.0 5 votes vote down vote up
@Override
public SofaResponse invoke(FilterInvoker invoker, SofaRequest request) throws SofaRpcException {
    SofaHystrixInvokable command;
    if (RpcConstants.INVOKER_TYPE_SYNC.equals(request.getInvokeType())) {
        command = new SofaHystrixCommand(invoker, request);
    } else if (RpcConstants.INVOKER_TYPE_FUTURE.equals(request.getInvokeType())) {
        command = new SofaAsyncHystrixCommand(invoker, request);
    } else {
        return invoker.invoke(request);
    }
    return command.invoke();
}
 
Example 13
Source File: CustomizeTestFilter.java    From sofa-rpc with Apache License 2.0 4 votes vote down vote up
@Override
public SofaResponse invoke(FilterInvoker invoker, SofaRequest request) throws SofaRpcException {
    invoked = true;
    return invoker.invoke(request);
}
 
Example 14
Source File: TestAsyncFilter.java    From sofa-rpc with Apache License 2.0 4 votes vote down vote up
@Override
public SofaResponse invoke(FilterInvoker invoker, SofaRequest request) throws SofaRpcException {
    return invoker.invoke(request);
}
 
Example 15
Source File: TestFilter.java    From sofa-rpc-boot-projects with Apache License 2.0 3 votes vote down vote up
@Override
public SofaResponse invoke(FilterInvoker invoker, SofaRequest request) throws SofaRpcException {

    request.getMethodArgs()[0] = "filter_change";
    return invoker.invoke(request);

}