com.alibaba.dubbo.rpc.Result Java Examples

The following examples show how to use com.alibaba.dubbo.rpc.Result. 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: 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 #2
Source File: TransactionServiceFilter.java    From ByteJTA with GNU Lesser General Public License v3.0 6 votes vote down vote up
public Result consumerInvokeForJTA(Invoker<?> invoker, Invocation invocation) throws RpcException {
	TransactionBeanRegistry beanRegistry = TransactionBeanRegistry.getInstance();
	TransactionBeanFactory beanFactory = beanRegistry.getBeanFactory();
	RemoteCoordinator transactionCoordinator = (RemoteCoordinator) beanFactory.getNativeParticipant();

	Map<String, String> attachments = invocation.getAttachments();
	attachments.put(RemoteCoordinator.class.getName(), transactionCoordinator.getIdentifier());
	RpcResult result = (RpcResult) invoker.invoke(invocation);
	Object value = result.getValue();
	if (InvocationResult.class.isInstance(value)) {
		InvocationResult wrapped = (InvocationResult) value;
		result.setValue(null);
		result.setException(null);

		if (wrapped.isFailure()) {
			result.setException(wrapped.getError());
		} else {
			result.setValue(wrapped.getValue());
		}

	} // end-if (InvocationResult.class.isInstance(value))

	return result;
}
 
Example #3
Source File: ContextFilter.java    From dubbo3 with Apache License 2.0 6 votes vote down vote up
public Result invoke(Invoker<?> invoker, Invocation invocation) throws RpcException {
    Map<String, String> attachments = invocation.getAttachments();
    if (attachments != null) {
        attachments = new HashMap<>(attachments);
        attachments.remove(Constants.PATH_KEY);
        attachments.remove(Constants.GROUP_KEY);
        attachments.remove(Constants.VERSION_KEY);
        attachments.remove(Constants.DUBBO_VERSION_KEY);
        attachments.remove(Constants.TOKEN_KEY);
        attachments.remove(Constants.TIMEOUT_KEY);
    }
    RpcContext.getContext()
            .setInvoker(invoker)
            .setInvocation(invocation)
            .setAttachments(attachments)
            .setLocalAddress(invoker.getUrl().getHost(), 
                             invoker.getUrl().getPort());
    if (invocation instanceof RpcInvocation) {
        ((RpcInvocation)invocation).setInvoker(invoker);
    }
    try {
        return invoker.invoke(invocation);
    } finally {
        RpcContext.removeContext();
    }
}
 
Example #4
Source File: AvailableCluster.java    From dubbo-2.6.5 with Apache License 2.0 6 votes vote down vote up
@Override
public <T> Invoker<T> join(Directory<T> directory) throws RpcException {

    return new AbstractClusterInvoker<T>(directory) {
        @Override
        public Result doInvoke(Invocation invocation, List<Invoker<T>> invokers, LoadBalance loadbalance) throws RpcException {
            for (Invoker<T> invoker : invokers) {
                if (invoker.isAvailable()) {
                    return invoker.invoke(invocation);
                }
            }
            throw new RpcException("No provider available in " + invokers);
        }
    };

}
 
Example #5
Source File: MockProtocol.java    From dubbox with Apache License 2.0 6 votes vote down vote up
public <T> Invoker<T> refer(Class<T> type, URL url) throws RpcException {
    
    final URL u = url;
    
    return new Invoker<T>(){
        public Class<T> getInterface(){
            return null;
        }
        public URL getUrl(){
            return u;
        }
        public boolean isAvailable(){
            return true;
        }
        public Result invoke(Invocation invocation) throws RpcException{
            return null;
        }
        
        public void destroy(){
            
        }            
    };
}
 
Example #6
Source File: CompatibleFilterFilterTest.java    From dubbox with Apache License 2.0 6 votes vote down vote up
@Test
public void testInvokerNonJsonPojoSerialization() {
    invocation = EasyMock.createMock(Invocation.class);
    EasyMock.expect(invocation.getMethodName()).andReturn("echo").anyTimes();
    EasyMock.expect(invocation.getParameterTypes()).andReturn(new Class<?>[] { String.class }).anyTimes();
    EasyMock.expect(invocation.getArguments()).andReturn(new Object[] { "hello" }).anyTimes();
    EasyMock.replay(invocation);
    invoker = EasyMock.createMock(Invoker.class);
    EasyMock.expect(invoker.isAvailable()).andReturn(true).anyTimes();
    EasyMock.expect(invoker.getInterface()).andReturn(DemoService.class).anyTimes();
    RpcResult result = new RpcResult();
    result.setValue("hello");
    EasyMock.expect(invoker.invoke(invocation)).andReturn(result).anyTimes();
    URL url = URL.valueOf("test://test:11/test?group=dubbo&version=1.1");
    EasyMock.expect(invoker.getUrl()).andReturn(url).anyTimes();
    EasyMock.replay(invoker);
    Result filterResult = compatibleFilter.invoke(invoker, invocation);
    assertEquals("hello", filterResult.getValue());
}
 
Example #7
Source File: CompatibleFilterFilterTest.java    From dubbox with Apache License 2.0 6 votes vote down vote up
@Test
public void testInvokerNonJsonNonPojoSerialization() {
    invocation = EasyMock.createMock(Invocation.class);
    EasyMock.expect(invocation.getMethodName()).andReturn("echo").anyTimes();
    EasyMock.expect(invocation.getParameterTypes()).andReturn(new Class<?>[] {String.class }).anyTimes();
    EasyMock.expect(invocation.getArguments()).andReturn(new Object[] { "hello" }).anyTimes();
    EasyMock.replay(invocation);
    invoker = EasyMock.createMock(Invoker.class);
    EasyMock.expect(invoker.isAvailable()).andReturn(true).anyTimes();
    EasyMock.expect(invoker.getInterface()).andReturn(DemoService.class).anyTimes();
    RpcResult result = new RpcResult();
    result.setValue(new String[]{"High"});
    EasyMock.expect(invoker.invoke(invocation)).andReturn(result).anyTimes();
    URL url = URL.valueOf("test://test:11/test?group=dubbo&version=1.1");
    EasyMock.expect(invoker.getUrl()).andReturn(url).anyTimes();
    EasyMock.replay(invoker);
    Result filterResult = compatibleFilter.invoke(invoker, invocation);
    assertArrayEquals(new String[]{"High"}, (String[])filterResult.getValue());
}
 
Example #8
Source File: DubboCodec.java    From dubbo-2.6.5 with Apache License 2.0 6 votes vote down vote up
@Override
protected void encodeResponseData(Channel channel, ObjectOutput out, Object data, String version) throws IOException {
    Result result = (Result) data;
    // currently, the version value in Response records the version of Request
    boolean attach = Version.isSupportResponseAttatchment(version);
    Throwable th = result.getException();
    if (th == null) {
        Object ret = result.getValue();
        if (ret == null) {
            out.writeByte(attach ? RESPONSE_NULL_VALUE_WITH_ATTACHMENTS : RESPONSE_NULL_VALUE);
        } else {
            out.writeByte(attach ? RESPONSE_VALUE_WITH_ATTACHMENTS : RESPONSE_VALUE);
            out.writeObject(ret);
        }
    } else {
        out.writeByte(attach ? RESPONSE_WITH_EXCEPTION_WITH_ATTACHMENTS : RESPONSE_WITH_EXCEPTION);
        out.writeObject(th);
    }

    if (attach) {
        // returns current version of Response to consumer side.
        result.getAttachments().put(Constants.DUBBO_VERSION_KEY, Version.getProtocolVersion());
        out.writeObject(result.getAttachments());
    }
}
 
Example #9
Source File: CompatibleFilterFilterTest.java    From dubbox with Apache License 2.0 6 votes vote down vote up
@Test
public void testInvokerNonJsonEnumSerialization() {
    invocation = EasyMock.createMock(Invocation.class);
    EasyMock.expect(invocation.getMethodName()).andReturn("enumlength").anyTimes();
    EasyMock.expect(invocation.getParameterTypes()).andReturn(new Class<?>[] { Type[].class }).anyTimes();
    EasyMock.expect(invocation.getArguments()).andReturn(new Object[] { "hello" }).anyTimes();
    EasyMock.replay(invocation);
    invoker = EasyMock.createMock(Invoker.class);
    EasyMock.expect(invoker.isAvailable()).andReturn(true).anyTimes();
    EasyMock.expect(invoker.getInterface()).andReturn(DemoService.class).anyTimes();
    RpcResult result = new RpcResult();
    result.setValue("High");
    EasyMock.expect(invoker.invoke(invocation)).andReturn(result).anyTimes();
    URL url = URL.valueOf("test://test:11/test?group=dubbo&version=1.1");
    EasyMock.expect(invoker.getUrl()).andReturn(url).anyTimes();
    EasyMock.replay(invoker);
    Result filterResult = compatibleFilter.invoke(invoker, invocation);
    assertEquals(Type.High, filterResult.getValue());
}
 
Example #10
Source File: FailoverClusterInvokerTest.java    From dubbo3 with Apache License 2.0 6 votes vote down vote up
@Test()
public void testInvokeWithRPCException() {
    
    EasyMock.reset(invoker1);
    EasyMock.expect(invoker1.invoke(invocation)).andThrow(new RpcException()).anyTimes();
    EasyMock.expect(invoker1.isAvailable()).andReturn(true).anyTimes();
    EasyMock.expect(invoker1.getUrl()).andReturn(url).anyTimes();
    EasyMock.expect(invoker1.getInterface()).andReturn(FailoverClusterInvokerTest.class).anyTimes();
    EasyMock.replay(invoker1);
    
    EasyMock.reset(invoker2);
    EasyMock.expect(invoker2.invoke(invocation)).andReturn(result).anyTimes();
    EasyMock.expect(invoker2.isAvailable()).andReturn(true).anyTimes();
    EasyMock.expect(invoker2.getUrl()).andReturn(url).anyTimes();
    EasyMock.expect(invoker2.getInterface()).andReturn(FailoverClusterInvokerTest.class).anyTimes();
    EasyMock.replay(invoker2);
    
    FailoverClusterInvoker<FailoverClusterInvokerTest> invoker = new FailoverClusterInvoker<FailoverClusterInvokerTest>(dic);
    for(int i=0;i<100;i++){
        Result ret = invoker.invoke(invocation);
        assertSame(result, ret);
    }
}
 
Example #11
Source File: CompatibleFilterFilterTest.java    From dubbo-2.6.5 with Apache License 2.0 6 votes vote down vote up
@Test
public void testInvokerJsonPojoSerialization() {
    invocation = mock(Invocation.class);
    given(invocation.getMethodName()).willReturn("enumlength");
    given(invocation.getParameterTypes()).willReturn(new Class<?>[]{Type[].class});
    given(invocation.getArguments()).willReturn(new Object[]{"hello"});

    invoker = mock(Invoker.class);
    given(invoker.isAvailable()).willReturn(true);
    given(invoker.getInterface()).willReturn(DemoService.class);
    RpcResult result = new RpcResult();
    result.setValue("High");
    given(invoker.invoke(invocation)).willReturn(result);
    URL url = URL.valueOf("test://test:11/test?group=dubbo&version=1.1&serialization=json");
    given(invoker.getUrl()).willReturn(url);

    Result filterResult = compatibleFilter.invoke(invoker, invocation);
    assertEquals(Type.High, filterResult.getValue());
}
 
Example #12
Source File: AbstractClusterInvoker.java    From dubbox with Apache License 2.0 6 votes vote down vote up
public Result invoke(final Invocation invocation) throws RpcException {

        checkWheatherDestoried();

        LoadBalance loadbalance;
        
        List<Invoker<T>> invokers = list(invocation);
        if (invokers != null && invokers.size() > 0) {
            loadbalance = ExtensionLoader.getExtensionLoader(LoadBalance.class).getExtension(invokers.get(0).getUrl()
                    .getMethodParameter(invocation.getMethodName(),Constants.LOADBALANCE_KEY, Constants.DEFAULT_LOADBALANCE));
        } else {
            loadbalance = ExtensionLoader.getExtensionLoader(LoadBalance.class).getExtension(Constants.DEFAULT_LOADBALANCE);
        }
        RpcUtils.attachInvocationIdIfAsync(getUrl(), invocation);
        return doInvoke(invocation, invokers, loadbalance);
    }
 
Example #13
Source File: CompatibleFilterFilterTest.java    From dubbo3 with Apache License 2.0 6 votes vote down vote up
@Test
public void testInvokerNonJsonPojoSerialization() {
    invocation = EasyMock.createMock(Invocation.class);
    EasyMock.expect(invocation.getMethodName()).andReturn("echo").anyTimes();
    EasyMock.expect(invocation.getParameterTypes()).andReturn(new Class<?>[] { String.class }).anyTimes();
    EasyMock.expect(invocation.getArguments()).andReturn(new Object[] { "hello" }).anyTimes();
    EasyMock.replay(invocation);
    invoker = EasyMock.createMock(Invoker.class);
    EasyMock.expect(invoker.isAvailable()).andReturn(true).anyTimes();
    EasyMock.expect(invoker.getInterface()).andReturn(DemoService.class).anyTimes();
    RpcResult result = new RpcResult();
    result.setValue("hello");
    EasyMock.expect(invoker.invoke(invocation)).andReturn(result).anyTimes();
    URL url = URL.valueOf("test://test:11/test?group=dubbo&version=1.1");
    EasyMock.expect(invoker.getUrl()).andReturn(url).anyTimes();
    EasyMock.replay(invoker);
    Result filterResult = compatibleFilter.invoke(invoker, invocation);
    assertEquals("hello", filterResult.getValue());
}
 
Example #14
Source File: CompatibleFilterFilterTest.java    From dubbox with Apache License 2.0 6 votes vote down vote up
@Test
public void testResulthasException() {
    invocation = EasyMock.createMock(Invocation.class);
    EasyMock.expect(invocation.getMethodName()).andReturn("enumlength").anyTimes();
    EasyMock.expect(invocation.getParameterTypes()).andReturn(new Class<?>[] { Enum.class }).anyTimes();
    EasyMock.expect(invocation.getArguments()).andReturn(new Object[] { "hello" }).anyTimes();
    EasyMock.replay(invocation);
    invoker = EasyMock.createMock(Invoker.class);
    EasyMock.expect(invoker.isAvailable()).andReturn(true).anyTimes();
    EasyMock.expect(invoker.getInterface()).andReturn(DemoService.class).anyTimes();
    RpcResult result = new RpcResult();
    result.setException(new RuntimeException());
    result.setValue("High");
    EasyMock.expect(invoker.invoke(invocation)).andReturn(result).anyTimes();
    URL url = URL.valueOf("test://test:11/test?group=dubbo&version=1.1");
    EasyMock.expect(invoker.getUrl()).andReturn(url).anyTimes();
    EasyMock.replay(invoker);
    Result filterResult = compatibleFilter.invoke(invoker, invocation);
    assertEquals(filterResult, result);
}
 
Example #15
Source File: TpsLimitFilter.java    From dubbo-2.6.5 with Apache License 2.0 6 votes vote down vote up
@Override
    public Result invoke(Invoker<?> invoker, Invocation invocation) throws RpcException {

//        已经超过了tps不允许执行业务方法
        if (!tpsLimiter.isAllowable(invoker.getUrl(), invocation)) {
            throw new RpcException(
                    "Failed to invoke service " +
                            invoker.getInterface().getName() +
                            "." +
                            invocation.getMethodName() +
                            " because exceed max service tps.");
        }

//        业务方法执行
        return invoker.invoke(invocation);
    }
 
Example #16
Source File: AbstractClusterInvoker.java    From dubbo3 with Apache License 2.0 6 votes vote down vote up
public Result invoke(final Invocation invocation) throws RpcException {

        checkWheatherDestoried();

        LoadBalance loadbalance;
        
        List<Invoker<T>> invokers = list(invocation);
        if (invokers != null && invokers.size() > 0) {
            loadbalance = ExtensionLoader.getExtensionLoader(LoadBalance.class).getExtension(invokers.get(0).getUrl()
                    .getMethodParameter(invocation.getMethodName(),Constants.LOADBALANCE_KEY, Constants.DEFAULT_LOADBALANCE));
        } else {
            loadbalance = ExtensionLoader.getExtensionLoader(LoadBalance.class).getExtension(Constants.DEFAULT_LOADBALANCE);
        }
        RpcUtils.attachInvocationIdIfAsync(getUrl(), invocation);
        return doInvoke(invocation, invokers, loadbalance);
    }
 
Example #17
Source File: CompatibleFilterFilterTest.java    From dubbo3 with Apache License 2.0 6 votes vote down vote up
@Test
public void testInvokerNonJsonNonPojoSerialization() {
    invocation = EasyMock.createMock(Invocation.class);
    EasyMock.expect(invocation.getMethodName()).andReturn("echo").anyTimes();
    EasyMock.expect(invocation.getParameterTypes()).andReturn(new Class<?>[] {String.class }).anyTimes();
    EasyMock.expect(invocation.getArguments()).andReturn(new Object[] { "hello" }).anyTimes();
    EasyMock.replay(invocation);
    invoker = EasyMock.createMock(Invoker.class);
    EasyMock.expect(invoker.isAvailable()).andReturn(true).anyTimes();
    EasyMock.expect(invoker.getInterface()).andReturn(DemoService.class).anyTimes();
    RpcResult result = new RpcResult();
    result.setValue(new String[]{"High"});
    EasyMock.expect(invoker.invoke(invocation)).andReturn(result).anyTimes();
    URL url = URL.valueOf("test://test:11/test?group=dubbo&version=1.1");
    EasyMock.expect(invoker.getUrl()).andReturn(url).anyTimes();
    EasyMock.replay(invoker);
    Result filterResult = compatibleFilter.invoke(invoker, invocation);
    assertArrayEquals(new String[]{"High"}, (String[])filterResult.getValue());
}
 
Example #18
Source File: MockClusterInvoker.java    From dubbox with Apache License 2.0 5 votes vote down vote up
public Result invoke(Invocation invocation) throws RpcException {
	Result result = null;
       
       String value = directory.getUrl().getMethodParameter(invocation.getMethodName(), Constants.MOCK_KEY, Boolean.FALSE.toString()).trim(); 
       if (value.length() == 0 || value.equalsIgnoreCase("false")){
       	//no mock
       	result = this.invoker.invoke(invocation);
       } else if (value.startsWith("force")) {
       	if (logger.isWarnEnabled()) {
       		logger.info("force-mock: " + invocation.getMethodName() + " force-mock enabled , url : " +  directory.getUrl());
       	}
       	//force:direct mock
       	result = doMockInvoke(invocation, null);
       } else {
       	//fail-mock
       	try {
       		result = this.invoker.invoke(invocation);
       	}catch (RpcException e) {
			if (e.isBiz()) {
				throw e;
			} else {
				if (logger.isWarnEnabled()) {
	        		logger.info("fail-mock: " + invocation.getMethodName() + " fail-mock enabled , url : " +  directory.getUrl(), e);
	        	}
				result = doMockInvoke(invocation, e);
			}
		}
       }
       return result;
}
 
Example #19
Source File: StickyTest.java    From dubbox-hystrix with Apache License 2.0 5 votes vote down vote up
@Override
protected Result doInvoke(Invocation invocation, List<Invoker<T>> invokers,
                          LoadBalance loadbalance) throws RpcException {
    Invoker<T> invoker = select(loadbalance, invocation, invokers, null);
    selectedInvoker = invoker ;
    return null;
}
 
Example #20
Source File: MockClusterInvokerTest.java    From dubbox-hystrix with Apache License 2.0 5 votes vote down vote up
/**
 * 测试mock策略是否正常-fail-mock
 */
@Test
public void testMockInvokerFromOverride_Invoke_checkCompatible_ImplMock2(){
	URL url = URL.valueOf("remote://1.2.3.4/"+IHelloService.class.getName())
			.addParameter("mock","fail")
			.addParameter("invoke_return_error", "true" );
	Invoker<IHelloService> cluster = getClusterInvoker(url);        
	//方法配置了mock
       RpcInvocation invocation = new RpcInvocation();
	invocation.setMethodName("getSomething");
       Result ret = cluster.invoke(invocation);
       Assert.assertEquals("somethingmock", ret.getValue());
}
 
Example #21
Source File: MockClusterInvoker.java    From dubbox with Apache License 2.0 5 votes vote down vote up
public Result invoke(Invocation invocation) throws RpcException {
	Result result = null;
       
       String value = directory.getUrl().getMethodParameter(invocation.getMethodName(), Constants.MOCK_KEY, Boolean.FALSE.toString()).trim(); 
       if (value.length() == 0 || value.equalsIgnoreCase("false")){
       	//no mock
       	result = this.invoker.invoke(invocation);
       } else if (value.startsWith("force")) {
       	if (logger.isWarnEnabled()) {
       		logger.info("force-mock: " + invocation.getMethodName() + " force-mock enabled , url : " +  directory.getUrl());
       	}
       	//force:direct mock
       	result = doMockInvoke(invocation, null);
       } else {
       	//fail-mock
       	try {
       		result = this.invoker.invoke(invocation);
       	}catch (RpcException e) {
			if (e.isBiz()) {
				throw e;
			} else {
				if (logger.isWarnEnabled()) {
	        		logger.info("fail-mock: " + invocation.getMethodName() + " fail-mock enabled , url : " +  directory.getUrl(), e);
	        	}
				result = doMockInvoke(invocation, e);
			}
		}
       }
       return result;
}
 
Example #22
Source File: InjvmInvoker.java    From dubbox with Apache License 2.0 5 votes vote down vote up
public Result doInvoke(Invocation invocation) throws Throwable {
    Exporter<?> exporter = InjvmProtocol.getExporter(exporterMap, getUrl());
    if (exporter == null)  {
        throw new RpcException("Service [" + key + "] not found.");
    }
    RpcContext.getContext().setRemoteAddress(NetUtils.LOCALHOST, 0);
    return exporter.getInvoker().invoke(invocation);
}
 
Example #23
Source File: ForkingClusterInvokerTest.java    From dubbox with Apache License 2.0 5 votes vote down vote up
@Test()
public void testInvokeNoExceptoin() {

    resetInvokerToNoException();

    ForkingClusterInvoker<ForkingClusterInvokerTest> invoker = new ForkingClusterInvoker<ForkingClusterInvokerTest>(
                                                                                                                    dic);
    Result ret = invoker.invoke(invocation);
    Assert.assertSame(result, ret);
}
 
Example #24
Source File: FailbackClusterInvokerTest.java    From dubbox with Apache License 2.0 5 votes vote down vote up
@Test()
public void testInvokeNoExceptoin() {

    resetInvokerToNoException();

    FailbackClusterInvoker<FailbackClusterInvokerTest> invoker = new FailbackClusterInvoker<FailbackClusterInvokerTest>(
                                                                                                                        dic);
    Result ret = invoker.invoke(invocation);
    Assert.assertSame(result, ret);
}
 
Example #25
Source File: MockClusterInvokerTest.java    From dubbo3 with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("unchecked")
@Test
public void testMockInvokerFromOverride_Invoke_check_ListString_empty(){
	URL url = URL.valueOf("remote://1.2.3.4/"+IHelloService.class.getName())
			.addParameter("getListString.mock","force:return empty")
			.addParameter("invoke_return_error", "true" );
	Invoker<IHelloService> cluster = getClusterInvoker(url);        
	//方法配置了mock
       RpcInvocation invocation = new RpcInvocation();
	invocation.setMethodName("getListString");
       Result ret = cluster.invoke(invocation);
       Assert.assertEquals(0, ((List<String>)ret.getValue()).size());
}
 
Example #26
Source File: MockClusterInvokerTest.java    From dubbox with Apache License 2.0 5 votes vote down vote up
/**
 * 测试mock策略是否正常-fail-mock
 */
@Test
public void testMockInvokerFromOverride_Invoke_checkCompatible_ImplMock(){
	URL url = URL.valueOf("remote://1.2.3.4/"+IHelloService.class.getName())
			.addParameter("mock","true")
			.addParameter("invoke_return_error", "true" );
	Invoker<IHelloService> cluster = getClusterInvoker(url);        
	//方法配置了mock
       RpcInvocation invocation = new RpcInvocation();
	invocation.setMethodName("getSomething");
       Result ret = cluster.invoke(invocation);
       Assert.assertEquals("somethingmock", ret.getValue());
}
 
Example #27
Source File: DeprecatedFilter.java    From dubbo-2.6.5 with Apache License 2.0 5 votes vote down vote up
@Override
public Result invoke(Invoker<?> invoker, Invocation invocation) throws RpcException {
    String key = invoker.getInterface().getName() + "." + invocation.getMethodName();
    if (!logged.contains(key)) {
        logged.add(key);
        if (invoker.getUrl().getMethodParameter(invocation.getMethodName(), Constants.DEPRECATED_KEY, false)) {
            LOGGER.error("The service method " + invoker.getInterface().getName() + "." + getMethodSignature(invocation) + " is DEPRECATED! Declare from " + invoker.getUrl());
        }
    }
    return invoker.invoke(invocation);
}
 
Example #28
Source File: FinishSpanTest.java    From brave with Apache License 2.0 5 votes vote down vote up
@Test public void create_null_result_value_and_error_DubboClientRequest() {
  Span span = tracing.tracer().nextSpan().kind(Span.Kind.CLIENT).start();

  FinishSpan.create(filter, clientRequest, mock(Result.class), span)
      .accept(null, null);

  testSpanHandler.takeRemoteSpan(Kind.CLIENT);
}
 
Example #29
Source File: FutureFilter.java    From dubbox with Apache License 2.0 5 votes vote down vote up
public Result invoke(final Invoker<?> invoker, final Invocation invocation) throws RpcException {
	final boolean isAsync = RpcUtils.isAsync(invoker.getUrl(), invocation);
    
	fireInvokeCallback(invoker, invocation);
    //需要在调用前配置好是否有返回值,已供invoker判断是否需要返回future.
    Result result = invoker.invoke(invocation);
    if (isAsync) {
        asyncCallback(invoker, invocation);
    } else {
        syncCallback(invoker, invocation, result);
    }
    return result;
}
 
Example #30
Source File: TokenFilter.java    From dubbox with Apache License 2.0 5 votes vote down vote up
public Result invoke(Invoker<?> invoker, Invocation inv)
		throws RpcException {
    String token = invoker.getUrl().getParameter(Constants.TOKEN_KEY);
    if (ConfigUtils.isNotEmpty(token)) {
        Class<?> serviceType = invoker.getInterface();
        Map<String, String> attachments = inv.getAttachments();
   		String remoteToken = attachments == null ? null : attachments.get(Constants.TOKEN_KEY);
   		if (! token.equals(remoteToken)) {
   			throw new RpcException("Invalid token! Forbid invoke remote service " + serviceType + " method " + inv.getMethodName() + "() from consumer " + RpcContext.getContext().getRemoteHost() + " to provider "  + RpcContext.getContext().getLocalHost());
   		}
    }
	return invoker.invoke(inv);
}