com.alipay.sofa.rpc.filter.FilterInvoker Java Examples

The following examples show how to use com.alipay.sofa.rpc.filter.FilterInvoker. 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: HystrixFilter.java    From sofa-rpc with Apache License 2.0 6 votes vote down vote up
@Override
public boolean needToLoad(FilterInvoker invoker) {
    AbstractInterfaceConfig config = invoker.getConfig();
    // 只支持 consumer 侧
    if (!isConsumerSide(config)) {
        if (LOGGER.isWarnEnabled(config.getAppName())) {
            LOGGER.warnWithApp(config.getAppName(), "HystrixFilter is not allowed on provider, interfaceId: {}",
                config.getInterfaceId());
        }
        return false;
    }
    if (!isHystrixEnabled(config)) {
        return false;
    }
    if (!isHystrixOnClasspath()) {
        if (LOGGER.isWarnEnabled(config.getAppName())) {
            LOGGER
                .warnWithApp(config.getAppName(),
                    "HystrixFilter is disabled because 'com.netflix.hystrix:hystrix-core' does not exist on the classpath");
        }
        return false;
    }
    return true;
}
 
Example #2
Source File: DefaultSetterFactory.java    From sofa-rpc with Apache License 2.0 6 votes vote down vote up
@Override
public HystrixCommand.Setter createSetter(FilterInvoker invoker, SofaRequest request) {
    Method clientMethod = request.getMethod();
    if (!SETTER_CACHE.containsKey(clientMethod)) {
        synchronized (DefaultSetterFactory.class) {
            if (!SETTER_CACHE.containsKey(clientMethod)) {
                String interfaceId = invoker.getConfig().getInterfaceId();
                String commandKey = generateCommandKey(interfaceId, request.getMethod());
                HystrixCommand.Setter setter = HystrixCommand.Setter
                    .withGroupKey(HystrixCommandGroupKey.Factory.asKey(interfaceId))
                    .andCommandKey(HystrixCommandKey.Factory.asKey(commandKey));
                SETTER_CACHE.put(clientMethod, setter);
            }
        }
    }
    return SETTER_CACHE.get(clientMethod);
}
 
Example #3
Source File: AbstractSofaRpcFilterTest.java    From Sentinel with Apache License 2.0 6 votes vote down vote up
@Test
public void testNeedToLoadProvider() {
    SentinelSofaRpcProviderFilter providerFilter = new SentinelSofaRpcProviderFilter();
    ProviderConfig providerConfig = new ProviderConfig();
    providerConfig.setInterfaceId(Serializer.class.getName());
    providerConfig.setId("AAA");
    FilterInvoker invoker = new FilterInvoker(null, null, providerConfig);
    assertTrue(providerFilter.needToLoad(invoker));

    providerConfig.setParameter(SentinelConstants.SOFA_RPC_SENTINEL_ENABLED, "false");
    assertFalse(providerFilter.needToLoad(invoker));

    providerConfig.setParameter(SentinelConstants.SOFA_RPC_SENTINEL_ENABLED, "");
    assertTrue(providerFilter.needToLoad(invoker));

    RpcConfigs.putValue(SentinelConstants.SOFA_RPC_SENTINEL_ENABLED, "false");
    assertFalse(providerFilter.needToLoad(invoker));
}
 
Example #4
Source File: AbstractSofaRpcFilterTest.java    From Sentinel with Apache License 2.0 6 votes vote down vote up
@Test
public void testNeedToLoadConsumer() {
    SentinelSofaRpcConsumerFilter consumerFilter = new SentinelSofaRpcConsumerFilter();
    ConsumerConfig consumerConfig = new ConsumerConfig();
    consumerConfig.setInterfaceId(Serializer.class.getName());
    consumerConfig.setId("BBB");
    FilterInvoker invoker = new FilterInvoker(null, null, consumerConfig);
    assertTrue(consumerFilter.needToLoad(invoker));

    consumerConfig.setParameter(SentinelConstants.SOFA_RPC_SENTINEL_ENABLED, "false");
    assertFalse(consumerFilter.needToLoad(invoker));

    consumerConfig.setParameter(SentinelConstants.SOFA_RPC_SENTINEL_ENABLED, "");
    assertTrue(consumerFilter.needToLoad(invoker));

    RpcConfigs.putValue(SentinelConstants.SOFA_RPC_SENTINEL_ENABLED, "false");
    assertFalse(consumerFilter.needToLoad(invoker));
}
 
Example #5
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 #6
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 #7
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 #8
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 #9
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 #10
Source File: SofaAsyncHystrixCommand.java    From sofa-rpc with Apache License 2.0 5 votes vote down vote up
public SofaAsyncHystrixCommand(FilterInvoker invoker, SofaRequest request) {
    super(SofaHystrixConfig.loadSetterFactory((ConsumerConfig) invoker.getConfig()).createSetter(invoker,
        request));
    this.rpcInternalContext = RpcInternalContext.peekContext();
    this.rpcInvokeContext = RpcInvokeContext.peekContext();
    this.invoker = invoker;
    this.request = request;
}
 
Example #11
Source File: SofaHystrixCommand.java    From sofa-rpc with Apache License 2.0 5 votes vote down vote up
public SofaHystrixCommand(FilterInvoker invoker, SofaRequest request) {
    super(SofaHystrixConfig.loadSetterFactory((ConsumerConfig) invoker.getConfig()).createSetter(invoker, request));
    this.rpcInternalContext = RpcInternalContext.peekContext();
    this.rpcInvokeContext = RpcInvokeContext.peekContext();
    this.invoker = invoker;
    this.request = request;
}
 
Example #12
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 #13
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 #14
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 #15
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 #16
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 #17
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 #18
Source File: AbstractSofaRpcFilter.java    From Sentinel with Apache License 2.0 5 votes vote down vote up
@Override
public boolean needToLoad(FilterInvoker invoker) {
    AbstractInterfaceConfig<?, ?> config = invoker.getConfig();

    String enabled = config.getParameter(SentinelConstants.SOFA_RPC_SENTINEL_ENABLED);
    if (StringUtils.isNotBlank(enabled)) {
        return Boolean.parseBoolean(enabled);
    }

    return RpcConfigs.getOrDefaultValue(SentinelConstants.SOFA_RPC_SENTINEL_ENABLED, true);
}
 
Example #19
Source File: AbstractSofaRpcFilterTest.java    From Sentinel with Apache License 2.0 5 votes vote down vote up
@Test
public void testNeedToLoadProviderAndConsumer() {
    SentinelSofaRpcProviderFilter providerFilter = new SentinelSofaRpcProviderFilter();
    ProviderConfig providerConfig = new ProviderConfig();
    providerConfig.setInterfaceId(Serializer.class.getName());
    providerConfig.setId("AAA");
    FilterInvoker providerInvoker = new FilterInvoker(null, null, providerConfig);
    assertTrue(providerFilter.needToLoad(providerInvoker));

    SentinelSofaRpcConsumerFilter consumerFilter = new SentinelSofaRpcConsumerFilter();
    ConsumerConfig consumerConfig = new ConsumerConfig();
    consumerConfig.setInterfaceId(Serializer.class.getName());
    consumerConfig.setId("BBB");
    FilterInvoker consumerInvoker = new FilterInvoker(null, null, consumerConfig);
    assertTrue(consumerFilter.needToLoad(consumerInvoker));

    providerConfig.setParameter(SentinelConstants.SOFA_RPC_SENTINEL_ENABLED, "false");
    assertFalse(providerFilter.needToLoad(providerInvoker));
    assertTrue(consumerFilter.needToLoad(consumerInvoker));

    providerConfig.setParameter(SentinelConstants.SOFA_RPC_SENTINEL_ENABLED, "");
    assertTrue(providerFilter.needToLoad(providerInvoker));

    RpcConfigs.putValue(SentinelConstants.SOFA_RPC_SENTINEL_ENABLED, "false");
    assertFalse(providerFilter.needToLoad(providerInvoker));
    assertFalse(consumerFilter.needToLoad(consumerInvoker));
}
 
Example #20
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 #21
Source File: RejectionFilter2.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 null;
}
 
Example #22
Source File: RejectionFilter4.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 null;
}
 
Example #23
Source File: RejectionFilter1.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 null;
}
 
Example #24
Source File: OverrideFilter1.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 null;
}
 
Example #25
Source File: RejectionFilter5.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 null;
}
 
Example #26
Source File: OverrideFilter0.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 null;
}
 
Example #27
Source File: OverrideFilter2.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 null;
}
 
Example #28
Source File: RejectionFilter.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 null;
}
 
Example #29
Source File: WrongFilter2.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 null;
}
 
Example #30
Source File: RejectionFilter3.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 null;
}