Java Code Examples for com.alibaba.dubbo.remoting.exchange.Exchangers

The following examples show how to use com.alibaba.dubbo.remoting.exchange.Exchangers. These examples are extracted from open source projects. 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 Project: dubbo-2.6.5   Source File: ClientReconnectTest.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * Reconnect log check, when the time is not enough for shutdown time, there is no error log, but there must be a warn log
 */
@Test
public void testReconnectWarnLog() throws RemotingException, InterruptedException {
    int port = NetUtils.getAvailablePort();
    DubboAppender.doStart();
    String url = "exchange://127.0.0.2:" + port + "/client.reconnect.test?check=false&"
            + Constants.RECONNECT_KEY + "=" + 1; //1ms reconnect, ensure that there is enough frequency to reconnect
    try {
        Exchangers.connect(url);
    } catch (Exception e) {

        //do nothing
    }
    Thread.sleep(1500);
    //Time is not long enough to produce a error log
    Assert.assertEquals("no error message ", 0, LogUtil.findMessage(Level.ERROR, "client reconnect to "));
    //The first reconnection failed to have a warn log
    Assert.assertEquals("must have one warn message ", 1, LogUtil.findMessage(Level.WARN, "client reconnect to "));
    DubboAppender.doStop();
}
 
Example 2
Source Project: dubbox-hystrix   Source File: ClientReconnectTest.java    License: Apache License 2.0 6 votes vote down vote up
/**
   * 测试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+"="+10 //1ms reconnect,保证有足够频率的重连
      +"&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();
  }
 
Example 3
Source Project: dubbo-2.6.5   Source File: Main.java    License: Apache License 2.0 6 votes vote down vote up
private static void test(int port) throws Exception {
    ExchangeChannel client = Exchangers.connect(URL.valueOf("dubbo://localhost:" + port));
    MockResult result = (MockResult) client.request(new RpcMessage(DemoService.class.getName(), "plus", new Class<?>[]{int.class, int.class}, new Object[]{55, 25})).get();
    System.out.println("55+25=" + result.getResult());

    for (int i = 0; i < 100; i++)
        client.request(new RpcMessage(DemoService.class.getName(), "sayHello", new Class<?>[]{String.class}, new Object[]{"qianlei" + i}));

    for (int i = 0; i < 100; i++)
        client.request(new Main.Data());

    System.out.println("=====test invoke=====");
    for (int i = 0; i < 100; i++) {
        ResponseFuture future = client.request(new Main.Data());
        System.out.println("invoke and get");
        System.out.println("invoke result:" + future.get());
    }
    System.out.println("=====the end=====");
}
 
Example 4
Source Project: dubbo-2.6.5   Source File: ThriftProtocol.java    License: Apache License 2.0 6 votes vote down vote up
private ExchangeServer getServer(URL url) {
    // enable sending readonly event when server closes by default
    url = url.addParameterIfAbsent(Constants.CHANNEL_READONLYEVENT_SENT_KEY, Boolean.TRUE.toString());
    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);

    ExchangeServer server;
    try {
        server = Exchangers.bind(url, handler);
    } 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;
}
 
Example 5
Source Project: dubbox   Source File: ClientReconnectTest.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * 重连日志的校验
 */
public void testReconnectWaringLog() throws RemotingException, InterruptedException{
    int port = NetUtils.getAvailablePort();
    DubboAppender.doStart();
    String url = "exchange://127.0.0.4:"+port + "/client.reconnect.test?transporter=netty4&check=false&"
    +Constants.RECONNECT_KEY+"="+100
    +"&"+Constants.SHUTDOWN_TIMEOUT_KEY+ "=1"//shutdown时间足够短,确保error日志输出
    +"&reconnect.waring.period=500";//每隔多少warning记录一次
    try{
        Exchangers.connect(url);
    }catch (Exception e) {
        //do nothing
    }
    int count =  0;
    for (int i=0;i<10;i++){
        count =  LogUtil.findMessage(Level.WARN, "client reconnect to ") ; 
        if (count >=1){
            break;
        }
        Thread.sleep(50);//重连线程的运行
    }
    Assert.assertTrue("warning message count must >= 1, real :"+count, count>= 1);
    DubboAppender.doStop();
}
 
Example 6
Source Project: dubbox-hystrix   Source File: ClientReconnectTest.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * 重连日志的校验
 */
@Test
public void testReconnectWaringLog() throws RemotingException, InterruptedException{
    int port = NetUtils.getAvailablePort();
    DubboAppender.doStart();
    String url = "exchange://127.0.0.4:"+port + "/client.reconnect.test?check=false&"
    +Constants.RECONNECT_KEY+"="+1 //1ms reconnect,保证有足够频率的重连
    +"&"+Constants.SHUTDOWN_TIMEOUT_KEY+ "=1"//shutdown时间足够短,确保error日志输出
    +"&reconnect.waring.period=100";//每隔多少warning记录一次
    try{
        Exchangers.connect(url);
    }catch (Exception e) {
        //do nothing
    }
    int count =  0;
    for (int i=0;i<100;i++){
        count =  LogUtil.findMessage(Level.WARN, "client reconnect to ") ; 
        if (count >=1){
            break;
        }
        Thread.sleep(50);//重连线程的运行
    }
    Assert.assertTrue("warning message count must >= 1, real :"+count, count>= 1);
    DubboAppender.doStop();
}
 
Example 7
Source Project: dubbox   Source File: ClientReconnectTest.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * 重连日志的校验,时间不够shutdown time时,不能有error日志,但必须有一条warn日志
 */
@Test
public void testReconnectWarnLog() throws RemotingException, InterruptedException{
    int port = NetUtils.getAvailablePort();
    DubboAppender.doStart();
    String url = "exchange://127.0.0.2:"+port + "/client.reconnect.test?check=false&"
    +Constants.RECONNECT_KEY+"="+1 ; //1ms reconnect,保证有足够频率的重连
    try{
        Exchangers.connect(url);
    }catch (Exception e) {
        //do nothing
    }
    Thread.sleep(1500);//重连线程的运行
    //时间不够长,不会产生error日志
    Assert.assertEquals("no error message ", 0 , LogUtil.findMessage(Level.ERROR, "client reconnect to "));
    //第一次重连失败就会有warn日志
    Assert.assertEquals("must have one warn message ", 1 , LogUtil.findMessage(Level.WARN, "client reconnect to "));
    DubboAppender.doStop();
}
 
Example 8
Source Project: dubbox   Source File: ClientReconnectTest.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * 重连日志的校验,不能一直抛出error日志.
 */
@Test
public void testReconnectErrorLog() throws RemotingException, InterruptedException{
    int port = NetUtils.getAvailablePort();
    DubboAppender.doStart();
    String url = "exchange://127.0.0.3:"+port + "/client.reconnect.test?check=false&"
    +Constants.RECONNECT_KEY+"="+1 + //1ms reconnect,保证有足够频率的重连
    "&"+Constants.SHUTDOWN_TIMEOUT_KEY+ "=1";//shutdown时间足够短,确保error日志输出
    try{
        Exchangers.connect(url);
    }catch (Exception e) {
        //do nothing
    }
    Thread.sleep(1500);//重连线程的运行
    Assert.assertEquals("only one error message ", 1 , LogUtil.findMessage(Level.ERROR, "client reconnect to "));
    DubboAppender.doStop();
}
 
Example 9
Source Project: dubbox   Source File: ClientReconnectTest.java    License: Apache License 2.0 6 votes vote down vote up
/**
   * 测试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+"="+10 //1ms reconnect,保证有足够频率的重连
      +"&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();
  }
 
Example 10
Source Project: dubbox   Source File: ClientReconnectTest.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * 重连日志的校验
 */
@Test
public void testReconnectWaringLog() throws RemotingException, InterruptedException{
    int port = NetUtils.getAvailablePort();
    DubboAppender.doStart();
    String url = "exchange://127.0.0.4:"+port + "/client.reconnect.test?check=false&"
    +Constants.RECONNECT_KEY+"="+1 //1ms reconnect,保证有足够频率的重连
    +"&"+Constants.SHUTDOWN_TIMEOUT_KEY+ "=1"//shutdown时间足够短,确保error日志输出
    +"&reconnect.waring.period=100";//每隔多少warning记录一次
    try{
        Exchangers.connect(url);
    }catch (Exception e) {
        //do nothing
    }
    int count =  0;
    for (int i=0;i<100;i++){
        count =  LogUtil.findMessage(Level.WARN, "client reconnect to ") ; 
        if (count >=1){
            break;
        }
        Thread.sleep(50);//重连线程的运行
    }
    Assert.assertTrue("warning message count must >= 1, real :"+count, count>= 1);
    DubboAppender.doStop();
}
 
Example 11
Source Project: dubbox-hystrix   Source File: ClientReconnectTest.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * 重连日志的校验,时间不够shutdown time时,不能有error日志,但必须有一条warn日志
 */
@Test
public void testReconnectWarnLog() throws RemotingException, InterruptedException{
    int port = NetUtils.getAvailablePort();
    DubboAppender.doStart();
    String url = "exchange://127.0.0.2:"+port + "/client.reconnect.test?check=false&"
    +Constants.RECONNECT_KEY+"="+1 ; //1ms reconnect,保证有足够频率的重连
    try{
        Exchangers.connect(url);
    }catch (Exception e) {
        //do nothing
    }
    Thread.sleep(1500);//重连线程的运行
    //时间不够长,不会产生error日志
    Assert.assertEquals("no error message ", 0 , LogUtil.findMessage(Level.ERROR, "client reconnect to "));
    //第一次重连失败就会有warn日志
    Assert.assertEquals("must have one warn message ", 1 , LogUtil.findMessage(Level.WARN, "client reconnect to "));
    DubboAppender.doStop();
}
 
Example 12
Source Project: dubbox-hystrix   Source File: Main.java    License: Apache License 2.0 6 votes vote down vote up
private static void test(int port) throws Exception
{
    ExchangeChannel client = Exchangers.connect(URL.valueOf("dubbo://localhost:" + port));
	MockResult result = (MockResult)client.request(new RpcMessage(DemoService.class.getName(),"plus",new Class<?>[]{int.class, int.class},new Object[]{55,25})).get();
	System.out.println("55+25="+result.getResult());

	for(int i=0;i<100;i++)
		client.request(new RpcMessage(DemoService.class.getName(),"sayHello", new Class<?>[]{String.class},new Object[]{"qianlei"+i}));

	for(int i=0;i<100;i++)
		client.request(new Main.Data());

	System.out.println("=====test invoke=====");
	for(int i=0;i<100;i++){
		ResponseFuture future = client.request(new Main.Data());
		System.out.println("invoke and get");
		System.out.println("invoke result:" + future.get());
	}
	System.out.println("=====the end=====");
}
 
Example 13
Source Project: dubbox   Source File: Main.java    License: Apache License 2.0 6 votes vote down vote up
private static void test(int port) throws Exception
{
    ExchangeChannel client = Exchangers.connect(URL.valueOf("dubbo://localhost:" + port));
	MockResult result = (MockResult)client.request(new RpcMessage(DemoService.class.getName(),"plus",new Class<?>[]{int.class, int.class},new Object[]{55,25})).get();
	System.out.println("55+25="+result.getResult());

	for(int i=0;i<100;i++)
		client.request(new RpcMessage(DemoService.class.getName(),"sayHello", new Class<?>[]{String.class},new Object[]{"qianlei"+i}));

	for(int i=0;i<100;i++)
		client.request(new Main.Data());

	System.out.println("=====test invoke=====");
	for(int i=0;i<100;i++){
		ResponseFuture future = client.request(new Main.Data());
		System.out.println("invoke and get");
		System.out.println("invoke result:" + future.get());
	}
	System.out.println("=====the end=====");
}
 
Example 14
Source Project: dubbox   Source File: DubboProtocol.java    License: Apache License 2.0 6 votes vote down vote up
private ExchangeServer createServer(URL url) {
    //默认开启server关闭时发送readonly事件
    url = url.addParameterIfAbsent(Constants.CHANNEL_READONLYEVENT_SENT_KEY, Boolean.TRUE.toString());
    //默认开启heartbeat
    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, Version.isCompatibleVersion() ? COMPATIBLE_CODEC_NAME : 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;
}
 
Example 15
Source Project: dubbox   Source File: ThriftProtocol.java    License: Apache License 2.0 6 votes vote down vote up
private ExchangeServer getServer(URL url) {
    //默认开启server关闭时发送readonly事件
    url = url.addParameterIfAbsent(Constants.CHANNEL_READONLYEVENT_SENT_KEY, Boolean.TRUE.toString());
    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);

    ExchangeServer server;
    try {
        server = Exchangers.bind(url, handler);
    } 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;
}
 
Example 16
Source Project: dubbox   Source File: ThriftProtocol.java    License: Apache License 2.0 6 votes vote down vote up
private ExchangeServer getServer(URL url) {
    //默认开启server关闭时发送readonly事件
    url = url.addParameterIfAbsent(Constants.CHANNEL_READONLYEVENT_SENT_KEY, Boolean.TRUE.toString());
    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);

    ExchangeServer server;
    try {
        server = Exchangers.bind(url, handler);
    } 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;
}
 
Example 17
Source Project: dubbox-hystrix   Source File: DubboProtocol.java    License: Apache License 2.0 6 votes vote down vote up
private ExchangeServer createServer(URL url) {
    //默认开启server关闭时发送readonly事件
    url = url.addParameterIfAbsent(Constants.CHANNEL_READONLYEVENT_SENT_KEY, Boolean.TRUE.toString());
    //默认开启heartbeat
    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, Version.isCompatibleVersion() ? COMPATIBLE_CODEC_NAME : 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;
}
 
Example 18
Source Project: dubbo-2.6.5   Source File: HeartbeatHandlerTest.java    License: Apache License 2.0 5 votes vote down vote up
@Test
public void testServerHeartbeat() throws Exception {
    URL serverURL = URL.valueOf("header://localhost:55555");
    serverURL = serverURL.addParameter(Constants.HEARTBEAT_KEY, 1000);
    TestHeartbeatHandler handler = new TestHeartbeatHandler();
    server = Exchangers.bind(serverURL, handler);
    System.out.println("Server bind successfully");

    FakeChannelHandlers.setTestingChannelHandlers();
    serverURL = serverURL.removeParameter(Constants.HEARTBEAT_KEY);
    client = Exchangers.connect(serverURL);
    Thread.sleep(10000);
    Assert.assertTrue(handler.disconnectCount > 0);
    System.out.println("disconnect count " + handler.disconnectCount);
}
 
Example 19
Source Project: dubbo-2.6.5   Source File: HeartbeatHandlerTest.java    License: Apache License 2.0 5 votes vote down vote up
@Test
public void testHeartbeat() throws Exception {
    URL serverURL = URL.valueOf("header://localhost:55555");
    serverURL = serverURL.addParameter(Constants.HEARTBEAT_KEY, 1000);
    TestHeartbeatHandler handler = new TestHeartbeatHandler();
    server = Exchangers.bind(serverURL, handler);
    System.out.println("Server bind successfully");

    client = Exchangers.connect(serverURL);
    Thread.sleep(10000);
    System.err.println("++++++++++++++ disconnect count " + handler.disconnectCount);
    System.err.println("++++++++++++++ connect count " + handler.connectCount);
    Assert.assertTrue(handler.disconnectCount == 0);
    Assert.assertTrue(handler.connectCount == 1);
}
 
Example 20
Source Project: dubbox-hystrix   Source File: DubboProtocol.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * 创建新连接.
 */
private ExchangeClient initClient(URL url) {
    
    // client type setting.
    String str = url.getParameter(Constants.CLIENT_KEY, url.getParameter(Constants.SERVER_KEY, Constants.DEFAULT_REMOTING_CLIENT));

    String version = url.getParameter(Constants.DUBBO_VERSION_KEY);
    boolean compatible = (version != null && version.startsWith("1.0."));
    url = url.addParameter(Constants.CODEC_KEY, Version.isCompatibleVersion() && compatible ? COMPATIBLE_CODEC_NAME : DubboCodec.NAME);
    //默认开启heartbeat
    url = url.addParameterIfAbsent(Constants.HEARTBEAT_KEY, String.valueOf(Constants.DEFAULT_HEARTBEAT));
    
    // BIO存在严重性能问题,暂时不允许使用
    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 {
        //设置连接应该是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;
}
 
Example 21
Source Project: dubbox-hystrix   Source File: NettyStringTest.java    License: Apache License 2.0 5 votes vote down vote up
@BeforeClass
public static void setUp() throws Exception {
    //int port = (int) (1000 * Math.random() + 10000);
    int port = 10001;
    System.out.println(port);
    server = Exchangers.bind(URL.valueOf("telnet://0.0.0.0:" + port + "?server=netty"), new TelnetServerHandler());
    client = Exchangers.connect(URL.valueOf("telnet://127.0.0.1:" + port + "?client=netty"), new TelnetClientHandler());
}
 
Example 22
Source Project: dubbo-2.6.5   Source File: NettyStringTest.java    License: Apache License 2.0 5 votes vote down vote up
@BeforeClass
public static void setUp() throws Exception {
    //int port = (int) (1000 * Math.random() + 10000);
    int port = 10001;
    System.out.println(port);
    server = Exchangers.bind(URL.valueOf("telnet://0.0.0.0:" + port + "?server=netty"), new TelnetServerHandler());
    client = Exchangers.connect(URL.valueOf("telnet://127.0.0.1:" + port + "?client=netty"), new TelnetClientHandler());
}
 
Example 23
Source Project: dubbo-2.6.5   Source File: AbstractExchangeGroup.java    License: Apache License 2.0 5 votes vote down vote up
protected Client connect(URL url) throws RemotingException {
    if (servers.containsKey(url)) {
        return null;
    }
    ExchangeClient client = clients.get(url);
    if (client == null) { // TODO exist concurrent gap
        client = Exchangers.connect(url, dispatcher);
        clients.put(url, client);
    }
    return client;
}
 
Example 24
Source Project: dubbo-2.6.5   Source File: ChanelHandlerTest.java    License: Apache License 2.0 5 votes vote down vote up
public static ExchangeClient initClient(String url) {
    // Create client and build connection
    ExchangeClient exchangeClient = null;
    PeformanceTestHandler handler = new PeformanceTestHandler(url);
    boolean run = true;
    while (run) {
        try {
            exchangeClient = Exchangers.connect(url, handler);
        } catch (Throwable t) {

            if (t != null && t.getCause() != null && t.getCause().getClass() != null && (t.getCause().getClass() == java.net.ConnectException.class
                    || t.getCause().getClass() == java.net.ConnectException.class)) {

            } else {
                t.printStackTrace();
            }

            try {
                Thread.sleep(50);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        if (exchangeClient != null) {
            run = false;
        }
    }
    return exchangeClient;
}
 
Example 25
Source Project: dubbo-2.6.5   Source File: Main.java    License: Apache License 2.0 5 votes vote down vote up
private static void startServer(int port) throws Exception {
    ReplierDispatcher dispatcher = new ReplierDispatcher();
    dispatcher.addReplier(RpcMessage.class, new RpcMessageHandler());
    dispatcher.addReplier(Object.class, new Replier<Object>() {
        public Object reply(ExchangeChannel channel, Object msg) {
            for (int i = 0; i < 10000; i++)
                System.currentTimeMillis();
            System.out.println("handle:" + msg + ";thread:" + Thread.currentThread().getName());
            return new StringMessage("hello world");
        }
    });
    Exchangers.bind(URL.valueOf("dubbo://localhost:" + port), dispatcher);
}
 
Example 26
Source Project: dubbox-hystrix   Source File: HeartbeatHandlerTest.java    License: Apache License 2.0 5 votes vote down vote up
@Test
public void testClientHeartbeat() throws Exception {
    FakeChannelHandlers.setTestingChannelHandlers();
    URL serverURL = URL.valueOf("header://localhost:55555");
    TestHeartbeatHandler handler = new TestHeartbeatHandler();
    server = Exchangers.bind(serverURL, handler);
    System.out.println("Server bind successfully");

    FakeChannelHandlers.resetChannelHandlers();
    serverURL = serverURL.addParameter(Constants.HEARTBEAT_KEY, 1000);
    client = Exchangers.connect(serverURL);
    Thread.sleep(10000);
    Assert.assertTrue(handler.connectCount > 0);
    System.out.println("connect count " + handler.connectCount);
}
 
Example 27
Source Project: dubbo-2.6.5   Source File: LazyConnectExchangeClient.java    License: Apache License 2.0 5 votes vote down vote up
private void initClient() throws RemotingException {
    if (client != null)
        return;
    if (logger.isInfoEnabled()) {
        logger.info("Lazy connect to " + url);
    }
    connectLock.lock();
    try {
        if (client != null)
            return;
        this.client = Exchangers.connect(url, requestHandler);
    } finally {
        connectLock.unlock();
    }
}
 
Example 28
Source Project: dubbo-2.6.5   Source File: DubboProtocol.java    License: Apache License 2.0 5 votes vote down vote up
private ExchangeServer createServer(URL url) {
        // send readonly event when server closes, it's enabled by default channel.readonly.sent 服务器关闭时发送只读事件,默认情况下启用
        url = url.addParameterIfAbsent(Constants.CHANNEL_READONLYEVENT_SENT_KEY, Boolean.TRUE.toString());
        // enable heartbeat by default
//        默认心跳检测时间heartbeat 6s
        url = url.addParameterIfAbsent(Constants.HEARTBEAT_KEY, String.valueOf(Constants.DEFAULT_HEARTBEAT));
//        server值默认是netty
        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);

//        codec默认dubbo
        url = url.addParameter(Constants.CODEC_KEY, DubboCodec.NAME);
        ExchangeServer server;
        try {
//          创建netty server
            server = Exchangers.bind(url, requestHandler);
        } catch (RemotingException e) {
            throw new RpcException("Fail to start server(url: " + url + ") " + e.getMessage(), e);
        }
//        解析client参数
        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;
    }
 
Example 29
Source Project: dubbo-2.6.5   Source File: DubboProtocol.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * 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;
}
 
Example 30
Source Project: dubbox-hystrix   Source File: ExchangeClientFactory.java    License: Apache License 2.0 5 votes vote down vote up
protected ExchangeClient createClient(String targetIP, int targetPort, int connectTimeout) throws Exception {
    StringBuilder url = new StringBuilder();
    url.append("exchange://");
    url.append(targetIP);
    url.append(":");
    url.append(targetPort);
    url.append("?");
    url.append("timeout=");
    url.append(connectTimeout);
    return Exchangers.connect(url.toString());
}