Java Code Examples for com.alibaba.dubbo.common.utils.NetUtils#getAvailablePort()

The following examples show how to use com.alibaba.dubbo.common.utils.NetUtils#getAvailablePort() . 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: EnumBak.java    From dubbo-2.6.5 with Apache License 2.0 6 votes vote down vote up
@Test
public void testNormalEnum() {
    int port = NetUtils.getAvailablePort();
    URL serviceurl = URL.valueOf("dubbo://127.0.0.1:" + port + "/test?timeout=" + Integer.MAX_VALUE
    );
    DemoService demo = new DemoServiceImpl();
    Invoker<DemoService> invoker = proxy.getInvoker(demo, DemoService.class, serviceurl);
    protocol.export(invoker);

    URL consumerurl = serviceurl;
    Invoker<DemoService> reference = protocol.refer(DemoService.class, consumerurl);
    DemoService demoProxy = (DemoService) proxy.getProxy(reference);
    Type type = demoProxy.enumlength(Type.High);
    System.out.println(type);
    Assert.assertEquals(Type.High, type);

    invoker.destroy();
    reference.destroy();
}
 
Example 2
Source File: ClientReconnectTest.java    From dubbo-remoting-netty4 with Apache License 2.0 6 votes vote down vote up
/**
   * 测试client重连方法不会导致重连线程失效.
   */
  public void testClientReconnectMethod() throws RemotingException, InterruptedException{
      int port = NetUtils.getAvailablePort();
      String url = "exchange://127.0.0.3:"+port + "/client.reconnect.test?transporter=netty4&check=false&"
      +Constants.RECONNECT_KEY+"="+10 //1ms reconnect,保证有足够频率的重连
      +"&reconnect.waring.period=100";
      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 File: ClientReconnectTest.java    From dubbo3 with 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 4
Source File: ClientReconnectTest.java    From dubbo3 with 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 5
Source File: EnumBak.java    From dubbox with Apache License 2.0 6 votes vote down vote up
@Test
public void testNormalEnum(){
    int port = NetUtils.getAvailablePort();
    URL serviceurl = URL.valueOf("dubbo://127.0.0.1:"+port+"/test?timeout="+Integer.MAX_VALUE
            );
    DemoService demo = new DemoServiceImpl();
    Invoker<DemoService> invoker = proxy.getInvoker(demo, DemoService.class, serviceurl);
    protocol.export(invoker);
    
    URL consumerurl = serviceurl;
    Invoker<DemoService> reference = protocol.refer(DemoService.class, consumerurl);
    DemoService demoProxy = (DemoService)proxy.getProxy(reference);
    Type type = demoProxy.enumlength(Type.High);
    System.out.println(type);
    Assert.assertEquals(Type.High, type);
    
    invoker.destroy();
    reference.destroy();
}
 
Example 6
Source File: EnumBak.java    From dubbo3 with Apache License 2.0 6 votes vote down vote up
@Test
public void testGenericEnum() throws InterruptedException{
    int port = NetUtils.getAvailablePort();
    URL serviceurl = URL.valueOf("dubbo://127.0.0.1:"+port+"/test?timeout="+Integer.MAX_VALUE
            );
    DemoService demo = new DemoServiceImpl();
    Invoker<DemoService> invoker = proxy.getInvoker(demo, DemoService.class, serviceurl);
    protocol.export(invoker);
    
    URL consumerurl = serviceurl;
    
    Invoker<GenericService> reference = protocol.refer(GenericService.class, consumerurl);
    
    GenericService demoProxy = (GenericService)proxy.getProxy(reference);
    Object obj = demoProxy.$invoke("enumlength", new String[]{Type[].class.getName()}, new Object[]{new Type[]{Type.High,Type.High}});
    System.out.println("obj---------->"+obj);
    
    invoker.destroy();
    reference.destroy();
}
 
Example 7
Source File: EnumBak.java    From dubbox with Apache License 2.0 6 votes vote down vote up
@Test
public void testNormalEnum(){
    int port = NetUtils.getAvailablePort();
    URL serviceurl = URL.valueOf("dubbo://127.0.0.1:"+port+"/test?timeout="+Integer.MAX_VALUE
            );
    DemoService demo = new DemoServiceImpl();
    Invoker<DemoService> invoker = proxy.getInvoker(demo, DemoService.class, serviceurl);
    protocol.export(invoker);
    
    URL consumerurl = serviceurl;
    Invoker<DemoService> reference = protocol.refer(DemoService.class, consumerurl);
    DemoService demoProxy = (DemoService)proxy.getProxy(reference);
    Type type = demoProxy.enumlength(Type.High);
    System.out.println(type);
    Assert.assertEquals(Type.High, type);
    
    invoker.destroy();
    reference.destroy();
}
 
Example 8
Source File: ReferenceCountExchangeClientTest.java    From dubbo3 with Apache License 2.0 6 votes vote down vote up
@SuppressWarnings("unchecked")
private void init(int connections){
    int port = NetUtils.getAvailablePort();
    URL demoUrl = URL.valueOf("dubbo://127.0.0.1:"+port+"/demo?"+Constants.CONNECTIONS_KEY+"="+connections);
    URL helloUrl = URL.valueOf("dubbo://127.0.0.1:"+port+"/hello?"+Constants.CONNECTIONS_KEY+"="+connections);
    
    demoExporter = export(new DemoServiceImpl(), IDemoService.class, demoUrl);
    helloExporter = export(new HelloServiceImpl(), IHelloService.class, helloUrl);
    
    demoServiceInvoker = (Invoker<IDemoService>) referInvoker(IDemoService.class, demoUrl);
    demoService = proxy.getProxy(demoServiceInvoker);
    Assert.assertEquals("demo", demoService.demo());
    
    helloServiceInvoker = (Invoker<IHelloService>) referInvoker(IHelloService.class, helloUrl);
    helloService = proxy.getProxy(helloServiceInvoker);
    Assert.assertEquals("hello", helloService.hello());
    
    demoClient = getClient(demoServiceInvoker);
    helloClient = getClient(helloServiceInvoker);
}
 
Example 9
Source File: ClientReconnectTest.java    From dubbox with 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 10
Source File: EnumBak.java    From dubbo-2.6.5 with Apache License 2.0 6 votes vote down vote up
@Ignore
    @Test
    public void testExportService() throws InterruptedException {
        int port = NetUtils.getAvailablePort();
        URL serviceurl = URL.valueOf("dubbo://127.0.0.1:" + port + "/test?proxy=jdk&timeout=" + Integer.MAX_VALUE
        );
        DemoService demo = new DemoServiceImpl();
        Invoker<DemoService> invoker = proxy.getInvoker(demo, DemoService.class, serviceurl);
        protocol.export(invoker);
        synchronized (EnumBak.class) {
            EnumBak.class.wait();
        }

//        URL consumerurl = serviceurl;
//        Invoker<DemoService> reference = protocol.refer(DemoService.class, consumerurl);
//        DemoService demoProxy = (DemoService)proxyFactory.createProxy(reference);
////        System.out.println(demoProxy.getThreadName());
//        System.out.println("byte:"+demoProxy.getbyte((byte)-128));
//
//        invoker.destroy();
//        reference.destroy();
    }
 
Example 11
Source File: ClientReconnectTest.java    From dubbox-hystrix with 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 12
Source File: EnumBak.java    From dubbox with Apache License 2.0 6 votes vote down vote up
@Test
    public void testNormal(){
        int port = NetUtils.getAvailablePort();
        URL serviceurl = URL.valueOf("dubbo://127.0.0.1:"+port+"/test?proxy=jdk" 
                + "&interface=" + DemoService.class.getName()
        		+ "&timeout=" + Integer.MAX_VALUE
                );
        DemoService demo = new DemoServiceImpl();
        Invoker<DemoService> invoker = proxy.getInvoker(demo, DemoService.class, serviceurl);
        protocol.export(invoker);
        
        URL consumerurl = serviceurl;
        Invoker<DemoService> reference = protocol.refer(DemoService.class, consumerurl);
        DemoService demoProxy = (DemoService)proxy.getProxy(reference);
//        System.out.println(demoProxy.getThreadName());
        Assert.assertEquals((byte)-128, demoProxy.getbyte((byte)-128));
        
//        invoker.destroy();
        reference.destroy();
    }
 
Example 13
Source File: EnumBak.java    From dubbox with Apache License 2.0 6 votes vote down vote up
@Test
    public void testNormal(){
        int port = NetUtils.getAvailablePort();
        URL serviceurl = URL.valueOf("dubbo://127.0.0.1:"+port+"/test?proxy=jdk" 
                + "&interface=" + DemoService.class.getName()
        		+ "&timeout=" + Integer.MAX_VALUE
                );
        DemoService demo = new DemoServiceImpl();
        Invoker<DemoService> invoker = proxy.getInvoker(demo, DemoService.class, serviceurl);
        protocol.export(invoker);
        
        URL consumerurl = serviceurl;
        Invoker<DemoService> reference = protocol.refer(DemoService.class, consumerurl);
        DemoService demoProxy = (DemoService)proxy.getProxy(reference);
//        System.out.println(demoProxy.getThreadName());
        Assert.assertEquals((byte)-128, demoProxy.getbyte((byte)-128));
        
//        invoker.destroy();
        reference.destroy();
    }
 
Example 14
Source File: DubboLazyConnectTest.java    From dubbox with Apache License 2.0 5 votes vote down vote up
@Test
public void testSticky4() {
    int port = NetUtils.getAvailablePort();
    URL url = URL.valueOf("dubbo://127.0.0.1:"+port+"/hi?"+Constants.LAZY_CONNECT_KEY+"=true");
    
    ProtocolUtils.export(new DemoServiceImpl(), IDemoService.class, url);
    
    IDemoService service = (IDemoService)ProtocolUtils.refer(IDemoService.class, url);
    Assert.assertEquals("ok", service.get());
}
 
Example 15
Source File: ExplicitCallbackTest.java    From dubbox with Apache License 2.0 5 votes vote down vote up
public void initOrResetUrl(int callbacks, int timeout) throws Exception {
        int port = NetUtils.getAvailablePort() ;
        consumerUrl = serviceURL =  URL.valueOf("dubbo://127.0.0.1:"+port+"/"+IDemoService.class.getName()+"?group=test" 
                +"&xxx.0.callback=true"
                +"&xxx2.0.callback=true"
                +"&unxxx2.0.callback=false"
                +"&timeout="+timeout
                +"&retries=0"
                +"&"+Constants.CALLBACK_INSTANCES_LIMIT_KEY+"="+callbacks
                );
        //      uncomment is unblock invoking
//        serviceURL = serviceURL.addParameter("yyy."+Constants.ASYNC_KEY,String.valueOf(true));
//        consumerUrl = consumerUrl.addParameter("yyy."+Constants.ASYNC_KEY,String.valueOf(true));
    }
 
Example 16
Source File: DubboLazyConnectTest.java    From dubbo-2.6.5 with Apache License 2.0 5 votes vote down vote up
@Test
public void testSticky4() {
    int port = NetUtils.getAvailablePort();
    URL url = URL.valueOf("dubbo://127.0.0.1:" + port + "/hi?" + Constants.LAZY_CONNECT_KEY + "=true");

    ProtocolUtils.export(new DemoServiceImpl(), IDemoService.class, url);

    IDemoService service = (IDemoService) ProtocolUtils.refer(IDemoService.class, url);
    Assert.assertEquals("ok", service.get());
}
 
Example 17
Source File: ExplicitCallbackTest.java    From dubbox with Apache License 2.0 5 votes vote down vote up
public void initOrResetUrl(int callbacks, int timeout) throws Exception {
        int port = NetUtils.getAvailablePort() ;
        consumerUrl = serviceURL =  URL.valueOf("dubbo://127.0.0.1:"+port+"/"+IDemoService.class.getName()+"?group=test" 
                +"&xxx.0.callback=true"
                +"&xxx2.0.callback=true"
                +"&unxxx2.0.callback=false"
                +"&timeout="+timeout
                +"&retries=0"
                +"&"+Constants.CALLBACK_INSTANCES_LIMIT_KEY+"="+callbacks
                );
        //      uncomment is unblock invoking
//        serviceURL = serviceURL.addParameter("yyy."+Constants.ASYNC_KEY,String.valueOf(true));
//        consumerUrl = consumerUrl.addParameter("yyy."+Constants.ASYNC_KEY,String.valueOf(true));
    }
 
Example 18
Source File: RedisRegistryTest.java    From dubbo-2.6.5 with Apache License 2.0 5 votes vote down vote up
@Before
public void setUp() throws Exception {
    int redisPort = NetUtils.getAvailablePort();
    this.redisServer = new RedisServer(redisPort);
    this.redisServer.start();
    this.registryUrl = URL.valueOf("redis://localhost:" + redisPort);

    redisRegistry = (RedisRegistry) new RedisRegistryFactory().createRegistry(registryUrl);
}
 
Example 19
Source File: ZookeeperRegistryTest.java    From dubbo-2.6.5 with Apache License 2.0 5 votes vote down vote up
@Before
public void setUp() throws Exception {
    int zkServerPort = NetUtils.getAvailablePort();
    this.zkServer = new TestingServer(zkServerPort, true);
    this.registryUrl = URL.valueOf("zookeeper://localhost:" + zkServerPort);

    zookeeperRegistryFactory = new ZookeeperRegistryFactory();
    zookeeperRegistryFactory.setZookeeperTransporter(new CuratorZookeeperTransporter());
    this.zookeeperRegistry = (ZookeeperRegistry) zookeeperRegistryFactory.createRegistry(registryUrl);
}
 
Example 20
Source File: EnumBak.java    From dubbox with Apache License 2.0 5 votes vote down vote up
public void testGenericExport() throws InterruptedException{
    int port = NetUtils.getAvailablePort();
    port = 20880;
    URL serviceurl = URL.valueOf("dubbo://127.0.0.1:"+port+"/test?timeout="+Integer.MAX_VALUE
            );
    DemoService demo = new DemoServiceImpl();
    Invoker<DemoService> invoker = proxy.getInvoker(demo, DemoService.class, serviceurl);
    protocol.export(invoker);
    
    
    //SERVER
    Thread.sleep(Integer.MAX_VALUE);
}