org.apache.dubbo.rpc.Invocation Java Examples
The following examples show how to use
org.apache.dubbo.rpc.Invocation.
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: DubboUtils.java From Sentinel-Dashboard-Nacos with Apache License 2.0 | 6 votes |
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 #2
Source File: DubboUtilsTest.java From Sentinel with Apache License 2.0 | 6 votes |
@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 #3
Source File: DubboInterceptor.java From skywalking with Apache License 2.0 | 6 votes |
/** * Format operation name. e.g. org.apache.skywalking.apm.plugin.test.Test.test(String) * * @return operation name. */ private String generateOperationName(URL requestURL, Invocation invocation) { StringBuilder operationName = new StringBuilder(); operationName.append(requestURL.getPath()); operationName.append("." + invocation.getMethodName() + "("); for (Class<?> classes : invocation.getParameterTypes()) { operationName.append(classes.getSimpleName() + ","); } if (invocation.getParameterTypes().length > 0) { operationName.delete(operationName.length() - 1, operationName.length()); } operationName.append(")"); return operationName.toString(); }
Example #4
Source File: DubboUtils.java From Sentinel with Apache License 2.0 | 6 votes |
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 #5
Source File: SentinelDubboConsumerFilterTest.java From Sentinel with Apache License 2.0 | 6 votes |
@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 #6
Source File: Resilience4jCircuitBreakerFilter.java From dubbo-samples with Apache License 2.0 | 6 votes |
@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 #7
Source File: UserLoadBalance.java From dubbo-samples with Apache License 2.0 | 6 votes |
@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 #8
Source File: InvocationUtil.java From chronus with Apache License 2.0 | 6 votes |
public static JobConfig getJobConfigByArgs(final Invocation invocation){ Object[] args = invocation.getArguments(); JobConfig jobConfig = null; if (args != null) { for (Object o : args) { if (!(o instanceof JobConfig)) { continue; } jobConfig = (JobConfig) o; if (StringUtils.isBlank(jobConfig.getSysCode())) { log.error("ClientLoadBalance doSelect() error, SysCode isBlank!"); return null; } break; } } return jobConfig; }
Example #9
Source File: SentinelDubboConsumerFilterTest.java From Sentinel with Apache License 2.0 | 6 votes |
@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 #10
Source File: SentinelDubboConsumerFilterTest.java From Sentinel with Apache License 2.0 | 6 votes |
@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 #11
Source File: SentinelDubboConsumerFilterTest.java From Sentinel with Apache License 2.0 | 6 votes |
@Test public void testInterfaceLevelFollowControlAsync() throws InterruptedException { Invoker invoker = DubboTestUtil.getDefaultMockInvoker(); Invocation invocation = DubboTestUtil.getDefaultMockInvocationOne(); when(invocation.getAttachment(ASYNC_KEY)).thenReturn(Boolean.TRUE.toString()); initFlowRule(DubboUtils.getInterfaceName(invoker)); Result result1 = invokeDubboRpc(false, invoker, invocation); assertEquals("normal", result1.getValue()); // should fallback because the qps > 1 Result result2 = invokeDubboRpc(false, invoker, invocation); assertEquals("fallback", result2.getValue()); // sleeping 1000 ms to reset qps Thread.sleep(1000); Result result3 = invokeDubboRpc(false, invoker, invocation); assertEquals("normal", result3.getValue()); verifyInvocationStructureForCallFinish(invoker, invocation); }
Example #12
Source File: SentinelDubboConsumerFilterTest.java From Sentinel with Apache License 2.0 | 5 votes |
@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 #13
Source File: BirdExceptionFilter.java From bird-java with MIT License | 5 votes |
@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 #14
Source File: DubboUtilsTest.java From dubbo-sentinel-support with Apache License 2.0 | 5 votes |
@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 #15
Source File: OnResponseThrowableAsyncFilter.java From dubbo-samples with Apache License 2.0 | 5 votes |
@Override public void onResponse(Result appResponse, Invoker<?> invoker, Invocation invocation) { System.out.println("onResponse received value : " + appResponse.getValue()); if (invocation != null) { throw new RuntimeException("Exception from onResponse"); } }
Example #16
Source File: SentinelDubboConsumerFilterTest.java From Sentinel with Apache License 2.0 | 5 votes |
@Test public void testDegradeSync() throws InterruptedException { Invocation invocation = DubboTestUtil.getDefaultMockInvocationOne(); Invoker invoker = DubboTestUtil.getDefaultMockInvoker(); 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 #17
Source File: DubboUtilsTest.java From Sentinel-Dashboard-Nacos with Apache License 2.0 | 5 votes |
@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 #18
Source File: SentinelDubboProviderFilterTest.java From dubbo-sentinel-support with Apache License 2.0 | 5 votes |
@Test public void testInvoke() { final String originApplication = "consumerA"; 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()); when(invocation.getAttachment(DubboUtils.SENTINEL_DUBBO_APPLICATION_KEY, "")) .thenReturn(originApplication); final Result result = mock(Result.class); when(result.hasException()).thenReturn(false); 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: DubboUtilsTest.java From Sentinel with Apache License 2.0 | 5 votes |
@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 #20
Source File: DubboUtilsTest.java From Sentinel with Apache License 2.0 | 5 votes |
@Test(expected = IllegalArgumentException.class) public void testGetApplicationNoAttachments() { Invocation invocation = mock(Invocation.class); when(invocation.getAttachments()).thenReturn(null); when(invocation.getAttachment(DubboUtils.SENTINEL_DUBBO_APPLICATION_KEY, "")) .thenReturn("consumerA"); DubboUtils.getApplication(invocation, ""); fail("No attachments in invocation, IllegalArgumentException should be thrown!"); }
Example #21
Source File: IpFirstLoadBalance.java From bird-java with MIT License | 5 votes |
@Override protected <T> Invoker<T> doSelect(List<Invoker<T>> invokers, URL url, Invocation invocation) { List<String> ips = SystemHelper.getLocalIps(); if (CollectionUtils.isNotEmpty(ips)) { for (Invoker<T> invoker : invokers) { if (ips.contains(invoker.getUrl().getHost())) { return invoker; } } } return defaultLoadBalance.select(invokers,url,invocation); }
Example #22
Source File: DubboSofaTracerFilter.java From sofa-tracer with Apache License 2.0 | 5 votes |
private SofaTracerSpan serverReceived(Invocation invocation) { Map<String, String> tags = new HashMap<>(); tags.put(Tags.SPAN_KIND.getKey(), Tags.SPAN_KIND_SERVER); String serializeSpanContext = invocation.getAttachments() .get(CommonSpanTags.RPC_TRACE_NAME); SofaTracerSpanContext sofaTracerSpanContext = SofaTracerSpanContext .deserializeFromString(serializeSpanContext); boolean isCalculateSampler = false; boolean isSampled = true; if (sofaTracerSpanContext == null) { SelfLog .error("SpanContext created error when server received and root SpanContext created."); sofaTracerSpanContext = SofaTracerSpanContext.rootStart(); isCalculateSampler = true; } String simpleName = invocation.getInvoker().getInterface().getSimpleName(); SofaTracerSpan serverSpan = new SofaTracerSpan(dubboProviderSofaTracer.getSofaTracer(), System.currentTimeMillis(), simpleName, sofaTracerSpanContext, tags); // calculate sampler if (isCalculateSampler) { Sampler sampler = dubboProviderSofaTracer.getSofaTracer().getSampler(); if (sampler != null) { isSampled = sampler.sample(serverSpan).isSampled(); } sofaTracerSpanContext.setSampled(isSampled); } SofaTraceContext sofaTraceContext = SofaTraceContextHolder.getSofaTraceContext(); // Record server receive event serverSpan.log(LogData.SERVER_RECV_EVENT_VALUE); sofaTraceContext.push(serverSpan); return serverSpan; }
Example #23
Source File: DubboSofaTracerFilter.java From sofa-tracer with Apache License 2.0 | 5 votes |
private void appendElapsedTimeTags(Invocation invocation, SofaTracerSpan sofaTracerSpan, Result result, boolean isClient) { if (sofaTracerSpan == null) { return; } String reqSize; String respSize; String elapsed; String deElapsed; if (isClient) { reqSize = invocation.getAttachment(AttachmentKeyConstants.CLIENT_SERIALIZE_SIZE); elapsed = invocation.getAttachment(AttachmentKeyConstants.CLIENT_SERIALIZE_TIME); respSize = result.getAttachment(AttachmentKeyConstants.CLIENT_DESERIALIZE_SIZE); deElapsed = result.getAttachment(AttachmentKeyConstants.CLIENT_DESERIALIZE_TIME); sofaTracerSpan.setTag(AttachmentKeyConstants.CLIENT_SERIALIZE_TIME, parseAttachment(elapsed, 0)); sofaTracerSpan.setTag(AttachmentKeyConstants.CLIENT_DESERIALIZE_TIME, parseAttachment(deElapsed, 0)); sofaTracerSpan.setTag(AttachmentKeyConstants.CLIENT_SERIALIZE_SIZE, parseAttachment(reqSize, 0)); sofaTracerSpan.setTag(AttachmentKeyConstants.CLIENT_DESERIALIZE_SIZE, parseAttachment(respSize, 0)); } else { reqSize = invocation.getAttachment(AttachmentKeyConstants.SERVER_DESERIALIZE_SIZE); deElapsed = invocation.getAttachment(AttachmentKeyConstants.SERVER_DESERIALIZE_TIME); respSize = result.getAttachment(AttachmentKeyConstants.SERVER_SERIALIZE_SIZE); elapsed = result.getAttachment(AttachmentKeyConstants.SERVER_SERIALIZE_TIME); sofaTracerSpan.setTag(AttachmentKeyConstants.SERVER_DESERIALIZE_SIZE, parseAttachment(reqSize, 0)); sofaTracerSpan.setTag(AttachmentKeyConstants.SERVER_DESERIALIZE_TIME, parseAttachment(deElapsed, 0)); sofaTracerSpan.setTag(AttachmentKeyConstants.SERVER_SERIALIZE_SIZE, parseAttachment(respSize, 0)); sofaTracerSpan.setTag(AttachmentKeyConstants.SERVER_SERIALIZE_TIME, parseAttachment(elapsed, 0)); } }
Example #24
Source File: DubboAppContextFilterTest.java From Sentinel with Apache License 2.0 | 5 votes |
@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 #25
Source File: DubboAppContextFilterTest.java From Sentinel with Apache License 2.0 | 5 votes |
@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 #26
Source File: SentinelDubboProviderFilterTest.java From Sentinel with Apache License 2.0 | 5 votes |
@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 #27
Source File: AsyncPostprocessFilter.java From dubbo-samples with Apache License 2.0 | 5 votes |
@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 #28
Source File: DubboUtils.java From Sentinel with Apache License 2.0 | 5 votes |
public static String getResourceName(Invoker<?> invoker, Invocation invocation, String prefix) { if (StringUtil.isNotBlank(prefix)) { return new StringBuilder(64) .append(prefix) .append(getResourceName(invoker, invocation, DubboConfig.getDubboInterfaceGroupAndVersionEnabled())) .toString(); } else { return getResourceName(invoker, invocation, DubboConfig.getDubboInterfaceGroupAndVersionEnabled()); } }
Example #29
Source File: OnErrorThrowableAsyncFilter.java From dubbo-samples with Apache License 2.0 | 5 votes |
@Override public void onError(Throwable t, Invoker<?> invoker, Invocation invocation) { System.out.println("OnErrorThrowableAsyncFilter onError executed: " + t.getMessage()); if (invocation != null) { throw new RuntimeException("Exception from onError"); } }
Example #30
Source File: SentinelDubboConsumerFilter.java From Sentinel with Apache License 2.0 | 5 votes |
@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); } }