Search in sources :

Example 91 with RpcException

use of org.apache.dubbo.rpc.RpcException in project dubbo by alibaba.

the class EtcdRegistry method doRegister.

@Override
public void doRegister(URL url) {
    try {
        String path = toUrlPath(url);
        if (url.getParameter(DYNAMIC_KEY, true)) {
            etcdClient.createEphemeral(path);
            return;
        }
        etcdClient.create(path);
    } catch (Throwable e) {
        throw new RpcException("Failed to register " + url + " to etcd " + getUrl() + ", cause: " + (OptionUtil.isProtocolError(e) ? "etcd3 registry may not be supported yet or etcd3 registry is not available." : e.getMessage()), e);
    }
}
Also used : RpcException(org.apache.dubbo.rpc.RpcException)

Example 92 with RpcException

use of org.apache.dubbo.rpc.RpcException in project dubbo by alibaba.

the class InjvmInvoker method doInvoke.

@Override
public Result doInvoke(Invocation invocation) throws Throwable {
    Exporter<?> exporter = InjvmProtocol.getExporter(delegateExporterMap, getUrl());
    if (exporter == null) {
        throw new RpcException("Service [" + key + "] not found.");
    }
    RpcContext.getContext().setRemoteAddress(LOCALHOST_VALUE, 0);
    // Solve local exposure, the server opens the token, and the client call fails.
    URL serverURL = exporter.getInvoker().getUrl();
    boolean serverHasToken = serverURL.hasParameter(Constants.TOKEN_KEY);
    if (serverHasToken) {
        invocation.setAttachment(Constants.TOKEN_KEY, serverURL.getParameter(Constants.TOKEN_KEY));
    }
    if (isAsync(exporter.getInvoker().getUrl(), getUrl())) {
        ((RpcInvocation) invocation).setInvokeMode(InvokeMode.ASYNC);
        // use consumer executor
        ExecutorService executor = executorRepository.createExecutorIfAbsent(getUrl());
        CompletableFuture<AppResponse> appResponseFuture = CompletableFuture.supplyAsync(() -> {
            Result result = exporter.getInvoker().invoke(invocation);
            if (result.hasException()) {
                return new AppResponse(result.getException());
            } else {
                return new AppResponse(result.getValue());
            }
        }, executor);
        // save for 2.6.x compatibility, for example, TraceFilter in Zipkin uses com.alibaba.xxx.FutureAdapter
        FutureContext.getContext().setCompatibleFuture(appResponseFuture);
        AsyncRpcResult result = new AsyncRpcResult(appResponseFuture, invocation);
        result.setExecutor(executor);
        return result;
    } else {
        return exporter.getInvoker().invoke(invocation);
    }
}
Also used : RpcInvocation(org.apache.dubbo.rpc.RpcInvocation) AppResponse(org.apache.dubbo.rpc.AppResponse) RpcException(org.apache.dubbo.rpc.RpcException) ExecutorService(java.util.concurrent.ExecutorService) URL(org.apache.dubbo.common.URL) AsyncRpcResult(org.apache.dubbo.rpc.AsyncRpcResult) Result(org.apache.dubbo.rpc.Result) AsyncRpcResult(org.apache.dubbo.rpc.AsyncRpcResult)

Example 93 with RpcException

use of org.apache.dubbo.rpc.RpcException in project dubbo by alibaba.

the class DubboProtocolTest method testNonSerializedParameter.

@Test
public void testNonSerializedParameter() throws Exception {
    DemoService service = new DemoServiceImpl();
    int port = NetUtils.getAvailablePort();
    protocol.export(proxy.getInvoker(service, DemoService.class, URL.valueOf("dubbo://127.0.0.1:" + port + "/" + DemoService.class.getName() + "?codec=exchange")));
    service = proxy.getProxy(protocol.refer(DemoService.class, URL.valueOf("dubbo://127.0.0.1:" + port + "/" + DemoService.class.getName() + "?codec=exchange").addParameter("timeout", 3000L)));
    try {
        service.nonSerializedParameter(new NonSerialized());
        Assertions.fail();
    } catch (RpcException e) {
        Assertions.assertTrue(e.getMessage().contains("org.apache.dubbo.rpc.protocol.dubbo.support.NonSerialized must implement java.io.Serializable"));
    }
}
Also used : NonSerialized(org.apache.dubbo.rpc.protocol.dubbo.support.NonSerialized) RpcException(org.apache.dubbo.rpc.RpcException) DemoService(org.apache.dubbo.rpc.protocol.dubbo.support.DemoService) DemoServiceImpl(org.apache.dubbo.rpc.protocol.dubbo.support.DemoServiceImpl) Test(org.junit.jupiter.api.Test)

Example 94 with RpcException

use of org.apache.dubbo.rpc.RpcException in project dubbo by alibaba.

the class DubboProtocolTest method testReturnNonSerialized.

@Test
public void testReturnNonSerialized() throws Exception {
    DemoService service = new DemoServiceImpl();
    int port = NetUtils.getAvailablePort();
    protocol.export(proxy.getInvoker(service, DemoService.class, URL.valueOf("dubbo://127.0.0.1:" + port + "/" + DemoService.class.getName() + "?codec=exchange")));
    service = proxy.getProxy(protocol.refer(DemoService.class, URL.valueOf("dubbo://127.0.0.1:" + port + "/" + DemoService.class.getName() + "?codec=exchange").addParameter("timeout", 3000L)));
    try {
        service.returnNonSerialized();
        Assertions.fail();
    } catch (RpcException e) {
        e.printStackTrace();
        Assertions.assertTrue(e.getMessage().contains("org.apache.dubbo.rpc.protocol.dubbo.support.NonSerialized must implement java.io.Serializable"));
    }
}
Also used : RpcException(org.apache.dubbo.rpc.RpcException) DemoService(org.apache.dubbo.rpc.protocol.dubbo.support.DemoService) DemoServiceImpl(org.apache.dubbo.rpc.protocol.dubbo.support.DemoServiceImpl) Test(org.junit.jupiter.api.Test)

Example 95 with RpcException

use of org.apache.dubbo.rpc.RpcException in project dubbo by alibaba.

the class DubboInvoker method doInvoke.

@Override
protected Result doInvoke(final Invocation invocation) throws Throwable {
    RpcInvocation inv = (RpcInvocation) invocation;
    final String methodName = RpcUtils.getMethodName(invocation);
    inv.setAttachment(PATH_KEY, getUrl().getPath());
    inv.setAttachment(VERSION_KEY, version);
    ExchangeClient currentClient;
    if (clients.length == 1) {
        currentClient = clients[0];
    } else {
        currentClient = clients[index.getAndIncrement() % clients.length];
    }
    try {
        boolean isOneway = RpcUtils.isOneway(getUrl(), invocation);
        int timeout = calculateTimeout(invocation, methodName);
        invocation.put(TIMEOUT_KEY, timeout);
        if (isOneway) {
            boolean isSent = getUrl().getMethodParameter(methodName, Constants.SENT_KEY, false);
            currentClient.send(inv, isSent);
            return AsyncRpcResult.newDefaultAsyncResult(invocation);
        } else {
            ExecutorService executor = getCallbackExecutor(getUrl(), inv);
            CompletableFuture<AppResponse> appResponseFuture = currentClient.request(inv, timeout, executor).thenApply(obj -> (AppResponse) obj);
            // save for 2.6.x compatibility, for example, TraceFilter in Zipkin uses com.alibaba.xxx.FutureAdapter
            FutureContext.getContext().setCompatibleFuture(appResponseFuture);
            AsyncRpcResult result = new AsyncRpcResult(appResponseFuture, inv);
            result.setExecutor(executor);
            return result;
        }
    } catch (TimeoutException e) {
        throw new RpcException(RpcException.TIMEOUT_EXCEPTION, "Invoke remote method timeout. method: " + invocation.getMethodName() + ", provider: " + getUrl() + ", cause: " + e.getMessage(), e);
    } catch (RemotingException e) {
        throw new RpcException(RpcException.NETWORK_EXCEPTION, "Failed to invoke remote method: " + invocation.getMethodName() + ", provider: " + getUrl() + ", cause: " + e.getMessage(), e);
    }
}
Also used : RpcInvocation(org.apache.dubbo.rpc.RpcInvocation) ExchangeClient(org.apache.dubbo.remoting.exchange.ExchangeClient) AppResponse(org.apache.dubbo.rpc.AppResponse) RpcException(org.apache.dubbo.rpc.RpcException) RemotingException(org.apache.dubbo.remoting.RemotingException) ExecutorService(java.util.concurrent.ExecutorService) AsyncRpcResult(org.apache.dubbo.rpc.AsyncRpcResult) TimeoutException(org.apache.dubbo.remoting.TimeoutException)

Aggregations

RpcException (org.apache.dubbo.rpc.RpcException)102 URL (org.apache.dubbo.common.URL)37 Test (org.junit.jupiter.api.Test)29 RpcInvocation (org.apache.dubbo.rpc.RpcInvocation)28 Result (org.apache.dubbo.rpc.Result)21 Invocation (org.apache.dubbo.rpc.Invocation)17 ArrayList (java.util.ArrayList)15 AppResponse (org.apache.dubbo.rpc.AppResponse)13 AsyncRpcResult (org.apache.dubbo.rpc.AsyncRpcResult)13 Invoker (org.apache.dubbo.rpc.Invoker)13 IOException (java.io.IOException)9 List (java.util.List)9 Method (java.lang.reflect.Method)8 Gson (com.google.gson.Gson)6 InvocationTargetException (java.lang.reflect.InvocationTargetException)6 HashMap (java.util.HashMap)5 RemotingException (org.apache.dubbo.remoting.RemotingException)5 TException (org.apache.thrift.TException)5 SocketTimeoutException (java.net.SocketTimeoutException)4 CountDownLatch (java.util.concurrent.CountDownLatch)4