Search in sources :

Example 21 with RemotingException

use of com.alibaba.dubbo.remoting.RemotingException in project dubbo by alibaba.

the class ClientReconnectTest method testClientReconnectMethod.

/**
     * 测试client重连方法不会导致重连线程失效.
     */
@Test
public void testClientReconnectMethod() throws RemotingException, InterruptedException {
    int port = NetUtils.getAvailablePort();
    String url = "exchange://127.0.0.3:" + port + "/client.reconnect.test?check=false&" + Constants.RECONNECT_KEY + "=" + //1ms reconnect,保证有足够频率的重连
    10 + "&reconnect.waring.period=1";
    DubboAppender.doStart();
    Client client = Exchangers.connect(url);
    try {
        client.reconnect();
    } catch (Exception e) {
    //do nothing
    }
    //重连线程的运行
    Thread.sleep(1500);
    Assert.assertTrue("have more then one warn msgs . bug was :" + LogUtil.findMessage(Level.WARN, "client reconnect to "), LogUtil.findMessage(Level.WARN, "client reconnect to ") > 1);
    DubboAppender.doStop();
}
Also used : Client(com.alibaba.dubbo.remoting.Client) RemotingException(com.alibaba.dubbo.remoting.RemotingException) Test(org.junit.Test)

Example 22 with RemotingException

use of com.alibaba.dubbo.remoting.RemotingException 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(Constants.PATH_KEY, getUrl().getPath());
    inv.setAttachment(Constants.VERSION_KEY, version);
    ExchangeClient currentClient;
    if (clients.length == 1) {
        currentClient = clients[0];
    } else {
        currentClient = clients[index.getAndIncrement() % clients.length];
    }
    try {
        boolean isAsync = RpcUtils.isAsync(getUrl(), invocation);
        boolean isOneway = RpcUtils.isOneway(getUrl(), invocation);
        int timeout = getUrl().getMethodParameter(methodName, Constants.TIMEOUT_KEY, Constants.DEFAULT_TIMEOUT);
        if (isOneway) {
            boolean isSent = getUrl().getMethodParameter(methodName, Constants.SENT_KEY, false);
            currentClient.send(inv, isSent);
            RpcContext.getContext().setFuture(null);
            return new RpcResult();
        } else if (isAsync) {
            ResponseFuture future = currentClient.request(inv, timeout);
            RpcContext.getContext().setFuture(new FutureAdapter<Object>(future));
            return new RpcResult();
        } else {
            RpcContext.getContext().setFuture(null);
            return (Result) currentClient.request(inv, timeout).get();
        }
    } 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(com.alibaba.dubbo.rpc.RpcInvocation) ExchangeClient(com.alibaba.dubbo.remoting.exchange.ExchangeClient) RpcException(com.alibaba.dubbo.rpc.RpcException) RemotingException(com.alibaba.dubbo.remoting.RemotingException) RpcResult(com.alibaba.dubbo.rpc.RpcResult) ResponseFuture(com.alibaba.dubbo.remoting.exchange.ResponseFuture) TimeoutException(com.alibaba.dubbo.remoting.TimeoutException)

Example 23 with RemotingException

use of com.alibaba.dubbo.remoting.RemotingException in project dubbo by alibaba.

the class DubboProtocol method getInvoker.

Invoker<?> getInvoker(Channel channel, Invocation inv) throws RemotingException {
    boolean isCallBackServiceInvoke = false;
    boolean isStubServiceInvoke = false;
    int port = channel.getLocalAddress().getPort();
    String path = inv.getAttachments().get(Constants.PATH_KEY);
    // if it's callback service on client side
    isStubServiceInvoke = Boolean.TRUE.toString().equals(inv.getAttachments().get(Constants.STUB_EVENT_KEY));
    if (isStubServiceInvoke) {
        port = channel.getRemoteAddress().getPort();
    }
    // callback
    isCallBackServiceInvoke = isClientSide(channel) && !isStubServiceInvoke;
    if (isCallBackServiceInvoke) {
        path = inv.getAttachments().get(Constants.PATH_KEY) + "." + inv.getAttachments().get(Constants.CALLBACK_SERVICE_KEY);
        inv.getAttachments().put(IS_CALLBACK_SERVICE_INVOKE, Boolean.TRUE.toString());
    }
    String serviceKey = serviceKey(port, path, inv.getAttachments().get(Constants.VERSION_KEY), inv.getAttachments().get(Constants.GROUP_KEY));
    DubboExporter<?> exporter = (DubboExporter<?>) exporterMap.get(serviceKey);
    if (exporter == null)
        throw new RemotingException(channel, "Not found exported service: " + serviceKey + " in " + exporterMap.keySet() + ", may be version or group mismatch " + ", channel: consumer: " + channel.getRemoteAddress() + " --> provider: " + channel.getLocalAddress() + ", message:" + inv);
    return exporter.getInvoker();
}
Also used : RemotingException(com.alibaba.dubbo.remoting.RemotingException)

Example 24 with RemotingException

use of com.alibaba.dubbo.remoting.RemotingException in project dubbo by alibaba.

the class DubboProtocol method createServer.

private ExchangeServer createServer(URL url) {
    // send readonly event when server closes, it's enabled by default
    url = url.addParameterIfAbsent(Constants.CHANNEL_READONLYEVENT_SENT_KEY, Boolean.TRUE.toString());
    // enable heartbeat by default
    url = url.addParameterIfAbsent(Constants.HEARTBEAT_KEY, String.valueOf(Constants.DEFAULT_HEARTBEAT));
    String str = url.getParameter(Constants.SERVER_KEY, Constants.DEFAULT_REMOTING_SERVER);
    if (str != null && str.length() > 0 && !ExtensionLoader.getExtensionLoader(Transporter.class).hasExtension(str))
        throw new RpcException("Unsupported server type: " + str + ", url: " + url);
    url = url.addParameter(Constants.CODEC_KEY, DubboCodec.NAME);
    ExchangeServer server;
    try {
        server = Exchangers.bind(url, requestHandler);
    } catch (RemotingException e) {
        throw new RpcException("Fail to start server(url: " + url + ") " + e.getMessage(), e);
    }
    str = url.getParameter(Constants.CLIENT_KEY);
    if (str != null && str.length() > 0) {
        Set<String> supportedTypes = ExtensionLoader.getExtensionLoader(Transporter.class).getSupportedExtensions();
        if (!supportedTypes.contains(str)) {
            throw new RpcException("Unsupported client type: " + str);
        }
    }
    return server;
}
Also used : RpcException(com.alibaba.dubbo.rpc.RpcException) RemotingException(com.alibaba.dubbo.remoting.RemotingException) ExchangeServer(com.alibaba.dubbo.remoting.exchange.ExchangeServer) Transporter(com.alibaba.dubbo.remoting.Transporter)

Example 25 with RemotingException

use of com.alibaba.dubbo.remoting.RemotingException in project dubbo by alibaba.

the class DubboProtocol method initClient.

/**
 * Create new connection
 */
private ExchangeClient initClient(URL url) {
    // client type setting.
    String str = url.getParameter(Constants.CLIENT_KEY, url.getParameter(Constants.SERVER_KEY, Constants.DEFAULT_REMOTING_CLIENT));
    url = url.addParameter(Constants.CODEC_KEY, DubboCodec.NAME);
    // enable heartbeat by default
    url = url.addParameterIfAbsent(Constants.HEARTBEAT_KEY, String.valueOf(Constants.DEFAULT_HEARTBEAT));
    // BIO is not allowed since it has severe performance issue.
    if (str != null && str.length() > 0 && !ExtensionLoader.getExtensionLoader(Transporter.class).hasExtension(str)) {
        throw new RpcException("Unsupported client type: " + str + "," + " supported client type is " + StringUtils.join(ExtensionLoader.getExtensionLoader(Transporter.class).getSupportedExtensions(), " "));
    }
    ExchangeClient client;
    try {
        // connection should be lazy
        if (url.getParameter(Constants.LAZY_CONNECT_KEY, false)) {
            client = new LazyConnectExchangeClient(url, requestHandler);
        } else {
            client = Exchangers.connect(url, requestHandler);
        }
    } catch (RemotingException e) {
        throw new RpcException("Fail to create remoting client for service(" + url + "): " + e.getMessage(), e);
    }
    return client;
}
Also used : ExchangeClient(com.alibaba.dubbo.remoting.exchange.ExchangeClient) RpcException(com.alibaba.dubbo.rpc.RpcException) RemotingException(com.alibaba.dubbo.remoting.RemotingException) Transporter(com.alibaba.dubbo.remoting.Transporter)

Aggregations

RemotingException (com.alibaba.dubbo.remoting.RemotingException)40 IOException (java.io.IOException)13 Request (com.alibaba.dubbo.remoting.exchange.Request)7 RpcException (com.alibaba.dubbo.rpc.RpcException)7 ExchangeChannel (com.alibaba.dubbo.remoting.exchange.ExchangeChannel)5 Response (com.alibaba.dubbo.remoting.exchange.Response)5 Channel (com.alibaba.dubbo.remoting.Channel)4 ExchangeClient (com.alibaba.dubbo.remoting.exchange.ExchangeClient)4 InetSocketAddress (java.net.InetSocketAddress)4 TimeoutException (com.alibaba.dubbo.remoting.TimeoutException)3 Transporter (com.alibaba.dubbo.remoting.Transporter)3 RpcInvocation (com.alibaba.dubbo.rpc.RpcInvocation)3 Test (org.junit.Test)3 ObjectOutput (com.alibaba.dubbo.common.serialize.ObjectOutput)2 Serialization (com.alibaba.dubbo.common.serialize.Serialization)2 ExchangeServer (com.alibaba.dubbo.remoting.exchange.ExchangeServer)2 Result (com.alibaba.dubbo.rpc.Result)2 RpcResult (com.alibaba.dubbo.rpc.RpcResult)2 ChannelFuture (io.netty.channel.ChannelFuture)2 UnsupportedEncodingException (java.io.UnsupportedEncodingException)2