org.apache.dubbo.rpc.Invoker Java Examples

The following examples show how to use org.apache.dubbo.rpc.Invoker. 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: DubboUtilsTest.java    From Sentinel with Apache License 2.0 6 votes vote down vote up
@Test
public void testGetInterfaceName() {

    URL url = URL.valueOf("dubbo://127.0.0.1:2181")
            .addParameter(CommonConstants.VERSION_KEY, "1.0.0")
            .addParameter(CommonConstants.GROUP_KEY, "grp1")
            .addParameter(CommonConstants.INTERFACE_KEY, DemoService.class.getName());
    Invoker invoker = mock(Invoker.class);
    when(invoker.getUrl()).thenReturn(url);
    when(invoker.getInterface()).thenReturn(DemoService.class);

    SentinelConfig.setConfig(DUBBO_INTERFACE_GROUP_VERSION_ENABLED, "false");
    assertEquals("com.alibaba.csp.sentinel.adapter.dubbo.provider.DemoService", DubboUtils.getInterfaceName(invoker));

    SentinelConfig.setConfig(DUBBO_INTERFACE_GROUP_VERSION_ENABLED, "true");
    assertEquals("com.alibaba.csp.sentinel.adapter.dubbo.provider.DemoService:1.0.0:grp1", DubboUtils.getInterfaceName(invoker));

}
 
Example #2
Source File: DubboUtils.java    From dubbo-sentinel-support with Apache License 2.0 6 votes vote down vote up
public static String getResourceName(Invoker<?> invoker, Invocation invocation) {
    StringBuilder buf = new StringBuilder(64);
    buf.append(invoker.getInterface().getName())
        .append(":")
        .append(invocation.getMethodName())
        .append("(");
    boolean isFirst = true;
    for (Class<?> clazz : invocation.getParameterTypes()) {
        if (!isFirst) {
            buf.append(",");
        }
        buf.append(clazz.getName());
        isFirst = false;
    }
    buf.append(")");
    return buf.toString();
}
 
Example #3
Source File: SentinelDubboConsumerFilterTest.java    From Sentinel-Dashboard-Nacos with Apache License 2.0 6 votes vote down vote up
@Test
public void testInvoke() {
    final Invoker invoker = mock(Invoker.class);
    when(invoker.getInterface()).thenReturn(DemoService.class);

    final Invocation invocation = mock(Invocation.class);
    Method method = DemoService.class.getMethods()[0];
    when(invocation.getMethodName()).thenReturn(method.getName());
    when(invocation.getParameterTypes()).thenReturn(method.getParameterTypes());

    final Result result = mock(Result.class);
    when(result.hasException()).thenReturn(false);
    when(invoker.invoke(invocation)).thenAnswer(invocationOnMock -> {
        verifyInvocationStructure(invoker, invocation);
        return result;
    });

    filter.invoke(invoker, invocation);
    verify(invoker).invoke(invocation);

    Context context = ContextUtil.getContext();
    assertNull(context);
}
 
Example #4
Source File: DubboUtils.java    From Sentinel-Dashboard-Nacos with Apache License 2.0 6 votes vote down vote up
public static String getResourceName(Invoker<?> invoker, Invocation invocation) {
    StringBuilder buf = new StringBuilder(64);
    buf.append(invoker.getInterface().getName())
        .append(":")
        .append(invocation.getMethodName())
        .append("(");
    boolean isFirst = true;
    for (Class<?> clazz : invocation.getParameterTypes()) {
        if (!isFirst) {
            buf.append(",");
        }
        buf.append(clazz.getName());
        isFirst = false;
    }
    buf.append(")");
    return buf.toString();
}
 
Example #5
Source File: SentinelDubboConsumerFilterTest.java    From dubbo-sentinel-support with Apache License 2.0 6 votes vote down vote up
@Test
public void testInvoke() {
    final Invoker invoker = mock(Invoker.class);
    when(invoker.getInterface()).thenReturn(DemoService.class);

    final Invocation invocation = mock(Invocation.class);
    Method method = DemoService.class.getMethods()[0];
    when(invocation.getMethodName()).thenReturn(method.getName());
    when(invocation.getParameterTypes()).thenReturn(method.getParameterTypes());

    final Result result = mock(Result.class);
    when(result.hasException()).thenReturn(false);
    when(invoker.invoke(invocation)).thenAnswer(invocationOnMock -> {
        verifyInvocationStructure(invoker, invocation);
        return result;
    });

    filter.invoke(invoker, invocation);
    verify(invoker).invoke(invocation);

    Context context = ContextUtil.getContext();
    assertNull(context);
}
 
Example #6
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 #7
Source File: LegacyBlockFilter.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 {
    RpcContext context = RpcContext.getContext();
    String filters = (String) context.getAttachment("filters");
    if (StringUtils.isEmpty(filters)) {
        filters = "";
    }
    filters += " legacy-block-filter";
    context.setAttachment("filters", filters);

    Result result = invoker.invoke(invocation);

    logger.info("This is the default return value: " + result.getValue());

    if (result.hasException()) {
        System.out.println("LegacyBlockFilter: This will only happen when the real exception returns: " + result.getException());
        logger.warn("This will only happen when the real exception returns", result.getException());
    }

    logger.info("LegacyBlockFilter: This msg should not be blocked.");
    return result;
}
 
Example #8
Source File: DubboSofaTracerFilter.java    From sofa-tracer with Apache License 2.0 6 votes vote down vote up
/**
 * set rpc server span tags
 * @param invoker
 * @param sofaTracerSpan
 */
private void appendRpcServerSpanTags(Invoker<?> invoker, SofaTracerSpan sofaTracerSpan) {
    if (sofaTracerSpan == null) {
        return;
    }
    RpcContext rpcContext = RpcContext.getContext();
    Map<String, String> tagsStr = sofaTracerSpan.getTagsWithStr();
    tagsStr.put(Tags.SPAN_KIND.getKey(), spanKind(rpcContext));
    String service = invoker.getInterface().getName();
    tagsStr.put(CommonSpanTags.SERVICE, service == null ? BLANK : service);
    String methodName = rpcContext.getMethodName();
    tagsStr.put(CommonSpanTags.METHOD, methodName == null ? BLANK : methodName);
    String app = rpcContext.getUrl().getParameter(CommonConstants.APPLICATION_KEY);
    tagsStr.put(CommonSpanTags.REMOTE_HOST, rpcContext.getRemoteHost());
    tagsStr.put(CommonSpanTags.LOCAL_APP, app == null ? BLANK : app);
    tagsStr.put(CommonSpanTags.CURRENT_THREAD_NAME, Thread.currentThread().getName());
    String protocol = rpcContext.getUrl().getProtocol();
    tagsStr.put(CommonSpanTags.PROTOCOL, protocol == null ? BLANK : protocol);
    tagsStr.put(CommonSpanTags.LOCAL_HOST, rpcContext.getRemoteHost());
    tagsStr.put(CommonSpanTags.LOCAL_PORT, String.valueOf(rpcContext.getRemotePort()));
}
 
Example #9
Source File: DubboSofaTracerFilter.java    From sofa-tracer with Apache License 2.0 6 votes vote down vote up
private void appendRpcClientSpanTags(Invoker<?> invoker, SofaTracerSpan sofaTracerSpan) {
    if (sofaTracerSpan == null) {
        return;
    }
    RpcContext rpcContext = RpcContext.getContext();
    Map<String, String> tagsStr = sofaTracerSpan.getTagsWithStr();
    tagsStr.put(Tags.SPAN_KIND.getKey(), spanKind(rpcContext));
    String protocol = rpcContext.getUrl().getProtocol();
    tagsStr.put(CommonSpanTags.PROTOCOL, protocol == null ? BLANK : protocol);
    String service = invoker.getInterface().getName();
    tagsStr.put(CommonSpanTags.SERVICE, service == null ? BLANK : service);
    String methodName = rpcContext.getMethodName();
    tagsStr.put(CommonSpanTags.METHOD, methodName == null ? BLANK : methodName);
    tagsStr.put(CommonSpanTags.CURRENT_THREAD_NAME, Thread.currentThread().getName());
    String app = rpcContext.getUrl().getParameter(CommonConstants.APPLICATION_KEY);
    tagsStr.put(CommonSpanTags.LOCAL_APP, app == null ? BLANK : app);
    tagsStr.put(CommonSpanTags.REMOTE_HOST, rpcContext.getRemoteHost());
    tagsStr.put(CommonSpanTags.REMOTE_PORT, String.valueOf(rpcContext.getRemotePort()));
    tagsStr.put(CommonSpanTags.LOCAL_HOST, rpcContext.getLocalHost());
}
 
Example #10
Source File: DubboUtils.java    From Sentinel with Apache License 2.0 6 votes vote down vote up
public static String getResourceName(Invoker<?> invoker, Invocation invocation, Boolean useGroupAndVersion) {
    StringBuilder buf = new StringBuilder(64);
    String interfaceResource = useGroupAndVersion ? invoker.getUrl().getColonSeparatedKey() : invoker.getInterface().getName();
    buf.append(interfaceResource)
        .append(":")
        .append(invocation.getMethodName())
        .append("(");
    boolean isFirst = true;
    for (Class<?> clazz : invocation.getParameterTypes()) {
        if (!isFirst) {
            buf.append(",");
        }
        buf.append(clazz.getName());
        isFirst = false;
    }
    buf.append(")");
    return buf.toString();
}
 
Example #11
Source File: UserLoadBalance.java    From dubbo-samples with Apache License 2.0 6 votes vote down vote up
@Override
public <T> Invoker<T> doSelect(List<Invoker<T>> invokers, URL url, Invocation invocation) throws RpcException {
    for (Invoker t : invokers) {
        try {
            InetAddress addr = InetAddress.getLocalHost();
            String ip = addr.getHostAddress().toString();
            URL u = t.getUrl();
            if (u.getIp().equals(ip)) {
                return t;
            }
        } catch (Exception e) {
            // no op
        }
    }
    return super.doSelect(invokers, url, invocation);
}
 
Example #12
Source File: SentinelDubboConsumerFilterTest.java    From Sentinel with Apache License 2.0 6 votes vote down vote up
@Test
public void testInvokeSync() {

    Invocation invocation = DubboTestUtil.getDefaultMockInvocationOne();
    Invoker invoker = DubboTestUtil.getDefaultMockInvoker();

    final Result result = mock(Result.class);
    when(result.hasException()).thenReturn(false);
    when(result.getException()).thenReturn(new Exception());
    when(invoker.invoke(invocation)).thenAnswer(invocationOnMock -> {
        verifyInvocationStructure(invoker, invocation);
        return result;
    });

    consumerFilter.invoke(invoker, invocation);
    verify(invoker).invoke(invocation);

    Context context = ContextUtil.getContext();
    assertNull(context);
}
 
Example #13
Source File: SentinelDubboConsumerFilterTest.java    From Sentinel with Apache License 2.0 6 votes vote down vote up
@Test
public void testInvokeAsync() {

    Invocation invocation = DubboTestUtil.getDefaultMockInvocationOne();
    Invoker invoker = DubboTestUtil.getDefaultMockInvoker();

    when(invocation.getAttachment(ASYNC_KEY)).thenReturn(Boolean.TRUE.toString());
    final Result result = mock(Result.class);
    when(result.hasException()).thenReturn(false);
    when(invoker.invoke(invocation)).thenAnswer(invocationOnMock -> {
        verifyInvocationStructureForAsyncCall(invoker, invocation);
         return result;
    });
    consumerFilter.invoke(invoker, invocation);
    verify(invoker).invoke(invocation);

    Context context = ContextUtil.getContext();
    assertNotNull(context);
}
 
Example #14
Source File: SentinelDubboConsumerFilterTest.java    From Sentinel with Apache License 2.0 6 votes vote down vote up
@Test
public void testMethodFlowControlAsync() {

    Invocation invocation = DubboTestUtil.getDefaultMockInvocationOne();
    Invoker invoker = DubboTestUtil.getDefaultMockInvoker();

    when(invocation.getAttachment(ASYNC_KEY)).thenReturn(Boolean.TRUE.toString());
    initFlowRule(consumerFilter.getMethodName(invoker, invocation));
    invokeDubboRpc(false, invoker, invocation);
    invokeDubboRpc(false, invoker, invocation);

    Invocation invocation2 = DubboTestUtil.getDefaultMockInvocationTwo();
    Result result2 = invokeDubboRpc(false, invoker, invocation2);
    verifyInvocationStructureForCallFinish(invoker, invocation2);
    assertEquals("normal", result2.getValue());

    // the method of invocation should be blocked
    Result fallback = invokeDubboRpc(false, invoker, invocation);
    assertEquals("fallback", fallback.getValue());
    verifyInvocationStructureForCallFinish(invoker, invocation);


}
 
Example #15
Source File: DubboUtilsTest.java    From Sentinel with Apache License 2.0 6 votes vote down vote up
@Test
public void testGetResourceNameWithGroupAndVersion() throws NoSuchMethodException {
    Invoker invoker = mock(Invoker.class);
    URL url = URL.valueOf("dubbo://127.0.0.1:2181")
            .addParameter(CommonConstants.VERSION_KEY, "1.0.0")
            .addParameter(CommonConstants.GROUP_KEY, "grp1")
            .addParameter(CommonConstants.INTERFACE_KEY, DemoService.class.getName());
    when(invoker.getUrl()).thenReturn(url);
    when(invoker.getInterface()).thenReturn(DemoService.class);

    Invocation invocation = mock(Invocation.class);
    Method method = DemoService.class.getDeclaredMethod("sayHello", String.class, int.class);
    when(invocation.getMethodName()).thenReturn(method.getName());
    when(invocation.getParameterTypes()).thenReturn(method.getParameterTypes());

    String resourceNameUseGroupAndVersion = DubboUtils.getResourceName(invoker, invocation, true);

    assertEquals("com.alibaba.csp.sentinel.adapter.dubbo.provider.DemoService:1.0.0:grp1:sayHello(java.lang.String,int)", resourceNameUseGroupAndVersion);
}
 
Example #16
Source File: DubboUtilsTest.java    From Sentinel with Apache License 2.0 6 votes vote down vote up
@Test
public void testGetResourceNameWithPrefix() throws NoSuchMethodException {
    Invoker invoker = mock(Invoker.class);
    when(invoker.getInterface()).thenReturn(DemoService.class);

    Invocation invocation = mock(Invocation.class);
    Method method = DemoService.class.getDeclaredMethod("sayHello", String.class, int.class);
    when(invocation.getMethodName()).thenReturn(method.getName());
    when(invocation.getParameterTypes()).thenReturn(method.getParameterTypes());

    //test with default prefix
    String resourceName = DubboUtils.getResourceName(invoker, invocation, DubboConfig.getDubboProviderPrefix());
    assertEquals("dubbo:provider:com.alibaba.csp.sentinel.adapter.dubbo.provider.DemoService:sayHello(java.lang.String,int)", resourceName);
    resourceName = DubboUtils.getResourceName(invoker, invocation, DubboConfig.getDubboConsumerPrefix());
    assertEquals("dubbo:consumer:com.alibaba.csp.sentinel.adapter.dubbo.provider.DemoService:sayHello(java.lang.String,int)", resourceName);


    //test with custom prefix
    SentinelConfig.setConfig(DubboConfig.DUBBO_PROVIDER_PREFIX, "my:dubbo:provider:");
    SentinelConfig.setConfig(DubboConfig.DUBBO_CONSUMER_PREFIX, "my:dubbo:consumer:");
    resourceName = DubboUtils.getResourceName(invoker, invocation, DubboConfig.getDubboProviderPrefix());
    assertEquals("my:dubbo:provider:com.alibaba.csp.sentinel.adapter.dubbo.provider.DemoService:sayHello(java.lang.String,int)", resourceName);
    resourceName = DubboUtils.getResourceName(invoker, invocation, DubboConfig.getDubboConsumerPrefix());
    assertEquals("my:dubbo:consumer:com.alibaba.csp.sentinel.adapter.dubbo.provider.DemoService:sayHello(java.lang.String,int)", resourceName);

}
 
Example #17
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 #18
Source File: SentinelDubboProviderFilterTest.java    From Sentinel with Apache License 2.0 5 votes vote down vote up
@Test
public void testInvoke() {

    final String originApplication = "consumerA";

    URL url = DubboTestUtil.getDefaultTestURL();
    url = url.addParameter(CommonConstants.SIDE_KEY, CommonConstants.PROVIDER_SIDE);
    Invoker invoker = DubboTestUtil.getMockInvoker(url, DemoService.class);

    Invocation invocation = DubboTestUtil.getMockInvocation(DemoService.class.getMethods()[0]);
    when(invocation.getAttachment(DubboUtils.SENTINEL_DUBBO_APPLICATION_KEY, ""))
            .thenReturn(originApplication);

    final Result result = mock(Result.class);
    when(result.hasException()).thenReturn(false);
    when(result.getException()).thenReturn(new Exception());

    when(invoker.invoke(invocation)).thenAnswer(invocationOnMock -> {
        verifyInvocationStructure(originApplication, invoker, invocation);
        return result;
    });

    filter.invoke(invoker, invocation);
    verify(invoker).invoke(invocation);

    Context context = ContextUtil.getContext();
    assertNull(context);
}
 
Example #19
Source File: DubboAppContextFilterTest.java    From Sentinel with Apache License 2.0 5 votes vote down vote up
@Test
public void testInvokeNullApplicationKey() {
    Invoker invoker = mock(Invoker.class);
    Invocation invocation = mock(Invocation.class);
    URL url = URL.valueOf("test://test:111/test?application=");
    when(invoker.getUrl()).thenReturn(url);

    filter.invoke(invoker, invocation);
    verify(invoker).invoke(invocation);

    String application = RpcContext.getContext().getAttachment(DubboUtils.SENTINEL_DUBBO_APPLICATION_KEY);
    assertEquals(application, "");
}
 
Example #20
Source File: DubboUtilsTest.java    From Sentinel with Apache License 2.0 5 votes vote down vote up
@Test
public void testGetResourceName() throws NoSuchMethodException {
    Invoker invoker = mock(Invoker.class);
    when(invoker.getInterface()).thenReturn(DemoService.class);

    Invocation invocation = mock(Invocation.class);
    Method method = DemoService.class.getDeclaredMethod("sayHello", String.class, int.class);
    when(invocation.getMethodName()).thenReturn(method.getName());
    when(invocation.getParameterTypes()).thenReturn(method.getParameterTypes());

    String resourceName = DubboUtils.getResourceName(invoker, invocation);

    assertEquals("com.alibaba.csp.sentinel.adapter.dubbo.provider.DemoService:sayHello(java.lang.String,int)", resourceName);

}
 
Example #21
Source File: SentinelDubboConsumerFilterTest.java    From Sentinel with Apache License 2.0 5 votes vote down vote up
@Test
public void testDegradeAsync() throws InterruptedException {

    Invocation invocation = DubboTestUtil.getDefaultMockInvocationOne();
    Invoker invoker = DubboTestUtil.getDefaultMockInvoker();

    when(invocation.getAttachment(ASYNC_KEY)).thenReturn(Boolean.TRUE.toString());
    initDegradeRule(DubboUtils.getInterfaceName(invoker));

    Result result = invokeDubboRpc(false, invoker, invocation);
    verifyInvocationStructureForCallFinish(invoker, invocation);
    assertEquals("normal", result.getValue());


    // inc the clusterNode's exception to trigger the fallback
    for (int i = 0; i < 5; i++) {
        invokeDubboRpc(true, invoker, invocation);
        verifyInvocationStructureForCallFinish(invoker, invocation);
    }

    Result result2 = invokeDubboRpc(false, invoker, invocation);
    assertEquals("fallback", result2.getValue());

    // sleeping 1000 ms to reset exception
    Thread.sleep(1000);
    Result result3 = invokeDubboRpc(false, invoker, invocation);
    assertEquals("normal", result3.getValue());

    Context context = ContextUtil.getContext();
    assertNull(context);
}
 
Example #22
Source File: SentinelDubboConsumerFilter.java    From Sentinel with Apache License 2.0 5 votes vote down vote up
@Override
public Result invoke(Invoker<?> invoker, Invocation invocation) throws RpcException {
    InvokeMode invokeMode = RpcUtils.getInvokeMode(invoker.getUrl(), invocation);
    if (InvokeMode.SYNC == invokeMode) {
        return syncInvoke(invoker, invocation);
    } else {
        return asyncInvoke(invoker, invocation);
    }

}
 
Example #23
Source File: DubboAppContextFilterTest.java    From Sentinel with Apache License 2.0 5 votes vote down vote up
@Test
public void testInvokeApplicationKey() {
    Invoker invoker = mock(Invoker.class);
    Invocation invocation = mock(Invocation.class);
    URL url = URL.valueOf("test://test:111/test?application=serviceA");
    when(invoker.getUrl()).thenReturn(url);

    filter.invoke(invoker, invocation);
    verify(invoker).invoke(invocation);

    String application = RpcContext.getContext().getAttachment(DubboUtils.SENTINEL_DUBBO_APPLICATION_KEY);
    assertEquals("serviceA", application);
}
 
Example #24
Source File: DubboAppContextFilterTest.java    From Sentinel-Dashboard-Nacos with Apache License 2.0 5 votes vote down vote up
@Test
public void testInvokeNullApplicationKey() {
    Invoker invoker = mock(Invoker.class);
    Invocation invocation = mock(Invocation.class);
    URL url = URL.valueOf("test://test:111/test?application=");
    when(invoker.getUrl()).thenReturn(url);

    filter.invoke(invoker, invocation);
    verify(invoker).invoke(invocation);

    String application = RpcContext.getContext().getAttachment(DubboUtils.SENTINEL_DUBBO_APPLICATION_KEY);
    assertNull(application);
}
 
Example #25
Source File: SentinelDubboConsumerFilterTest.java    From Sentinel with Apache License 2.0 5 votes vote down vote up
private void verifyInvocationStructureForCallFinish(Invoker invoker, Invocation invocation) {
    Context context = ContextUtil.getContext();
    assertNull(context);
    String methodResourceName = consumerFilter.getMethodName(invoker, invocation);
    Entry[] entries = (Entry[]) RpcContext.getContext().get(methodResourceName);
    assertNull(entries);
}
 
Example #26
Source File: DubboUtilsTest.java    From Sentinel-Dashboard-Nacos with Apache License 2.0 5 votes vote down vote up
@Test
public void testGetResourceName() {
    Invoker invoker = mock(Invoker.class);
    when(invoker.getInterface()).thenReturn(DemoService.class);

    Invocation invocation = mock(Invocation.class);
    Method method = DemoService.class.getMethods()[0];
    when(invocation.getMethodName()).thenReturn(method.getName());
    when(invocation.getParameterTypes()).thenReturn(method.getParameterTypes());

    String resourceName = DubboUtils.getResourceName(invoker, invocation);

    assertEquals("com.alibaba.csp.sentinel.adapter.dubbo.provider.DemoService:sayHello(java.lang.String,int)", resourceName);
}
 
Example #27
Source File: SentinelDubboConsumerFilterTest.java    From Sentinel with Apache License 2.0 5 votes vote down vote up
private void initFallback() {
    DubboFallbackRegistry.setConsumerFallback(new DubboFallback() {
        @Override
        public Result handle(Invoker<?> invoker, Invocation invocation, BlockException ex) {
            boolean async = RpcUtils.isAsync(invoker.getUrl(), invocation);
            Result fallbackResult = null;
            fallbackResult = AsyncRpcResult.newDefaultAsyncResult("fallback", invocation);
            return fallbackResult;
        }
    });
}
 
Example #28
Source File: SentinelDubboConsumerFilterTest.java    From Sentinel with Apache License 2.0 5 votes vote down vote up
private Result invokeDubboRpc(boolean exception, Invoker invoker, Invocation invocation) {
    Result result = null;
    InvokeMode invokeMode = RpcUtils.getInvokeMode(invoker.getUrl(), invocation);
    if (InvokeMode.SYNC == invokeMode) {
        result = exception ? new AppResponse(new Exception("error")) : new AppResponse("normal");
    } else {
        result = exception ? AsyncRpcResult.newDefaultAsyncResult(new Exception("error"), invocation) : AsyncRpcResult.newDefaultAsyncResult("normal", invocation);
    }
    when(invoker.invoke(invocation)).thenReturn(result);
    return consumerFilter.invoke(invoker, invocation);
}
 
Example #29
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 #30
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);
}