Search in sources :

Example 11 with ExchangeClient

use of com.alibaba.dubbo.remoting.exchange.ExchangeClient in project dubbo by alibaba.

the class DubboInvokerAvilableTest method getClients.

private ExchangeClient[] getClients(DubboInvoker<?> invoker) throws Exception {
    Field field = DubboInvoker.class.getDeclaredField("clients");
    field.setAccessible(true);
    ExchangeClient[] clients = (ExchangeClient[]) field.get(invoker);
    Assert.assertEquals(1, clients.length);
    return clients;
}
Also used : Field(java.lang.reflect.Field) ExchangeClient(com.alibaba.dubbo.remoting.exchange.ExchangeClient)

Example 12 with ExchangeClient

use of com.alibaba.dubbo.remoting.exchange.ExchangeClient in project dubbo by alibaba.

the class ReferenceCountExchangeClientTest method getClient.

private ExchangeClient getClient(Invoker<?> invoker) {
    if (invoker.getUrl().getParameter(Constants.CONNECTIONS_KEY, 1) == 1) {
        return getInvokerClient(invoker);
    } else {
        ReferenceCountExchangeClient client = getReferenceClient(invoker);
        try {
            Field clientField = ReferenceCountExchangeClient.class.getDeclaredField("client");
            clientField.setAccessible(true);
            return (ExchangeClient) clientField.get(client);
        } catch (Exception e) {
            e.printStackTrace();
            Assert.fail(e.getMessage());
            throw new RuntimeException(e);
        }
    }
}
Also used : Field(java.lang.reflect.Field) ExchangeClient(com.alibaba.dubbo.remoting.exchange.ExchangeClient)

Example 13 with ExchangeClient

use of com.alibaba.dubbo.remoting.exchange.ExchangeClient in project dubbo by alibaba.

the class PortTelnetHandlerTest method testListClient.

/**
 * In NAT network scenario, server's channel.getRemoteAddress() possibly get the address of network gateway, or
 * the address converted by NAT. In this case, check port only.
 */
@Test
public void testListClient() throws Exception {
    ExchangeClient client1 = Exchangers.connect("dubbo://127.0.0.1:20887/demo");
    ExchangeClient client2 = Exchangers.connect("dubbo://127.0.0.1:20887/demo");
    Thread.sleep(5000);
    String result = port.telnet(null, "-l 20887");
    String client1Addr = client1.getLocalAddress().toString();
    String client2Addr = client2.getLocalAddress().toString();
    System.out.printf("Result: %s %n", result);
    System.out.printf("Client 1 Address %s %n", client1Addr);
    System.out.printf("Client 2 Address %s %n", client2Addr);
    assertTrue(result.contains(String.valueOf(client1.getLocalAddress().getPort())));
    assertTrue(result.contains(String.valueOf(client2.getLocalAddress().getPort())));
}
Also used : ExchangeClient(com.alibaba.dubbo.remoting.exchange.ExchangeClient) Test(org.junit.Test)

Example 14 with ExchangeClient

use of com.alibaba.dubbo.remoting.exchange.ExchangeClient in project dubbo by alibaba.

the class ThriftInvoker method doInvoke.

@Override
protected Result doInvoke(Invocation invocation) throws Throwable {
    RpcInvocation inv = (RpcInvocation) invocation;
    final String methodName;
    methodName = invocation.getMethodName();
    inv.setAttachment(Constants.PATH_KEY, getUrl().getPath());
    // for thrift codec
    inv.setAttachment(ThriftCodec.PARAMETER_CLASS_NAME_GENERATOR, getUrl().getParameter(ThriftCodec.PARAMETER_CLASS_NAME_GENERATOR, DubboClassNameGenerator.NAME));
    ExchangeClient currentClient;
    if (clients.length == 1) {
        currentClient = clients[0];
    } else {
        currentClient = clients[index.getAndIncrement() % clients.length];
    }
    try {
        int timeout = getUrl().getMethodParameter(methodName, Constants.TIMEOUT_KEY, Constants.DEFAULT_TIMEOUT);
        RpcContext.getContext().setFuture(null);
        return (Result) currentClient.request(inv, timeout).get();
    } catch (TimeoutException e) {
        throw new RpcException(RpcException.TIMEOUT_EXCEPTION, e.getMessage(), e);
    } catch (RemotingException e) {
        throw new RpcException(RpcException.NETWORK_EXCEPTION, 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) Result(com.alibaba.dubbo.rpc.Result) TimeoutException(com.alibaba.dubbo.remoting.TimeoutException)

Example 15 with ExchangeClient

use of com.alibaba.dubbo.remoting.exchange.ExchangeClient in project dubbo by alibaba.

the class ExchangeServerPeer method getExchangeChannel.

@Override
public ExchangeChannel getExchangeChannel(InetSocketAddress remoteAddress) {
    String host = remoteAddress.getAddress() != null ? remoteAddress.getAddress().getHostAddress() : remoteAddress.getHostName();
    int port = remoteAddress.getPort();
    ExchangeChannel channel = super.getExchangeChannel(remoteAddress);
    if (channel == null) {
        for (Map.Entry<URL, ExchangeClient> entry : clients.entrySet()) {
            URL url = entry.getKey();
            if (url.getIp().equals(host) && url.getPort() == port) {
                return entry.getValue();
            }
        }
    }
    return channel;
}
Also used : ExchangeClient(com.alibaba.dubbo.remoting.exchange.ExchangeClient) ExchangeChannel(com.alibaba.dubbo.remoting.exchange.ExchangeChannel) Map(java.util.Map) URL(com.alibaba.dubbo.common.URL)

Aggregations

ExchangeClient (com.alibaba.dubbo.remoting.exchange.ExchangeClient)21 Test (org.junit.Test)6 RemotingException (com.alibaba.dubbo.remoting.RemotingException)4 RpcException (com.alibaba.dubbo.rpc.RpcException)4 URL (com.alibaba.dubbo.common.URL)3 Field (java.lang.reflect.Field)3 ArrayList (java.util.ArrayList)3 TimeoutException (com.alibaba.dubbo.remoting.TimeoutException)2 HeaderExchangeClient (com.alibaba.dubbo.remoting.exchange.support.header.HeaderExchangeClient)2 HeaderExchangeServer (com.alibaba.dubbo.remoting.exchange.support.header.HeaderExchangeServer)2 RpcInvocation (com.alibaba.dubbo.rpc.RpcInvocation)2 List (java.util.List)2 Random (java.util.Random)2 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)2 Transporter (com.alibaba.dubbo.remoting.Transporter)1 ExchangeChannel (com.alibaba.dubbo.remoting.exchange.ExchangeChannel)1 ExchangeServer (com.alibaba.dubbo.remoting.exchange.ExchangeServer)1 ResponseFuture (com.alibaba.dubbo.remoting.exchange.ResponseFuture)1 Result (com.alibaba.dubbo.rpc.Result)1 RpcResult (com.alibaba.dubbo.rpc.RpcResult)1