当前位置: 首页>>代码示例>>Java>>正文


Java NetUtils类代码示例

本文整理汇总了Java中com.alibaba.dubbo.common.utils.NetUtils的典型用法代码示例。如果您正苦于以下问题:Java NetUtils类的具体用法?Java NetUtils怎么用?Java NetUtils使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。


NetUtils类属于com.alibaba.dubbo.common.utils包,在下文中一共展示了NetUtils类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。

示例1: isProviderSide

import com.alibaba.dubbo.common.utils.NetUtils; //导入依赖的package包/类
/**
 * is provider side.
 * 
 * @return provider side.
 */
public boolean isProviderSide() {
    URL url = getUrl();
    if (url == null) {
        return false;
    }
    InetSocketAddress address = getRemoteAddress();
    if (address == null) {
        return false;
    }
    String host;
    if (address.getAddress() == null) {
        host = address.getHostName();
    } else {
        host = address.getAddress().getHostAddress();
    }
    return url.getPort() != address.getPort() || 
            ! NetUtils.filterLocalHost(url.getIp()).equals(NetUtils.filterLocalHost(host));
}
 
开发者ID:zhuxiaolei,项目名称:dubbo2,代码行数:24,代码来源:RpcContext.java

示例2: isClientSide

import com.alibaba.dubbo.common.utils.NetUtils; //导入依赖的package包/类
protected boolean isClientSide(Channel channel) {
    String side = (String) channel.getAttribute(Constants.SIDE_KEY);
    if ("client".equals(side)) {
        return true;
    } else if ("server".equals(side)) {
        return false;
    } else {
        InetSocketAddress address = channel.getRemoteAddress();
        URL url = channel.getUrl();
        boolean client = url.getPort() == address.getPort()
            && NetUtils.filterLocalHost(url.getIp()).equals(
            NetUtils.filterLocalHost(address.getAddress()
                                         .getHostAddress()));
        channel.setAttribute(Constants.SIDE_KEY, client ? "client"
            : "server");
        return client;
    }
}
 
开发者ID:yunhaibin,项目名称:dubbox-hystrix,代码行数:19,代码来源:DeprecatedTelnetCodec.java

示例3: handle

import com.alibaba.dubbo.common.utils.NetUtils; //导入依赖的package包/类
public Page handle(URL url) {
    List<List<String>> rows = new ArrayList<List<String>>();
    Collection<ExchangeServer> servers = DubboProtocol.getDubboProtocol().getServers();
    int clientCount = 0;
    if (servers != null && servers.size() > 0) {
        for (ExchangeServer s : servers) {
            List<String> row = new ArrayList<String>();
            String address = s.getUrl().getAddress();
            row.add(NetUtils.getHostName(address) + "/" + address);
            int clientSize = s.getExchangeChannels().size();
            clientCount += clientSize;
            row.add("<a href=\"clients.html?port=" + s.getUrl().getPort() + "\">Clients(" + clientSize + ")</a>");
            rows.add(row);
        }
    }
    return new Page("Servers", "Servers (" + rows.size() + ")", new String[]{"Server Address:", "Clients(" + clientCount + ")"}, rows);
}
 
开发者ID:yunhaibin,项目名称:dubbox-hystrix,代码行数:18,代码来源:ServersPageHandler.java

示例4: createRegistry

import com.alibaba.dubbo.common.utils.NetUtils; //导入依赖的package包/类
public Registry createRegistry(URL url) {
    url = getRegistryURL(url);
    List<URL> urls = new ArrayList<URL>();
    urls.add(url.removeParameter(Constants.BACKUP_KEY));
    String backup = url.getParameter(Constants.BACKUP_KEY);
    if (backup != null && backup.length() > 0) {
        String[] addresses = Constants.COMMA_SPLIT_PATTERN.split(backup);
        for (String address : addresses) {
            urls.add(url.setAddress(address));
        }
    }
    RegistryDirectory<RegistryService> directory = new RegistryDirectory<RegistryService>(RegistryService.class, url.addParameter(Constants.INTERFACE_KEY, RegistryService.class.getName()).addParameterAndEncoded(Constants.REFER_KEY, url.toParameterString()));
    Invoker<RegistryService> registryInvoker = cluster.join(directory);
    RegistryService registryService = proxyFactory.getProxy(registryInvoker);
    DubboRegistry registry = new DubboRegistry(registryInvoker, registryService);
    directory.setRegistry(registry);
    directory.setProtocol(protocol);
    directory.notify(urls);
    directory.subscribe(new URL(Constants.CONSUMER_PROTOCOL, NetUtils.getLocalHost(), 0, RegistryService.class.getName(), url.getParameters()));
    return registry;
}
 
开发者ID:l1325169021,项目名称:github-test,代码行数:22,代码来源:DubboRegistryFactory.java

示例5: testReconnectWarnLog

import com.alibaba.dubbo.common.utils.NetUtils; //导入依赖的package包/类
/**
 * 重连日志的校验,时间不够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();
}
 
开发者ID:flychao88,项目名称:dubbocloud,代码行数:22,代码来源:ClientReconnectTest.java

示例6: testDelayFixedTime

import com.alibaba.dubbo.common.utils.NetUtils; //导入依赖的package包/类
@Test
public void testDelayFixedTime() throws Exception {
    SimpleRegistryService registryService = new SimpleRegistryService();
    Exporter<RegistryService> exporter = SimpleRegistryExporter.export(4548, registryService);
    ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext(ConfigTest.class.getPackage().getName().replace('.', '/') + "/delay-fixed-time.xml");
    ctx.start();
    try {
        List<URL> urls = registryService.getRegistered().get("com.alibaba.dubbo.config.spring.api.DemoService");
        assertNull(urls);
        int i = 0;
        while ((i ++) < 60 && urls == null) {
            urls = registryService.getRegistered().get("com.alibaba.dubbo.config.spring.api.DemoService");
            Thread.sleep(10);
        }
        assertNotNull(urls);
        assertEquals(1, urls.size());
        assertEquals("dubbo://" + NetUtils.getLocalHost() + ":20883/com.alibaba.dubbo.config.spring.api.DemoService", urls.get(0).toIdentityString());
    } finally {
        ctx.stop();
        ctx.close();
        exporter.unexport();
    }
}
 
开发者ID:yunhaibin,项目名称:dubbox-hystrix,代码行数:24,代码来源:ConfigTest.java

示例7: leave

import com.alibaba.dubbo.common.utils.NetUtils; //导入依赖的package包/类
@Override
public void leave(URL url) throws RemotingException {
    super.leave(url);
    try {
        String full = url.toFullString();
        String[] lines = IOUtils.readLines(file);
        List<String> saves = new ArrayList<String>();
        for (String line : lines) {
            if (full.equals(line)) {
                return;
            }
            saves.add(line);
        }
        IOUtils.appendLines(file, saves.toArray(new String[0]));
    } catch (IOException e) {
        throw new RemotingException(new InetSocketAddress(NetUtils.getLocalHost(), 0), getUrl().toInetSocketAddress(), e.getMessage(), e);
    }
}
 
开发者ID:zhuxiaolei,项目名称:dubbo2,代码行数:19,代码来源:FileGroup.java

示例8: testNormalEnum

import com.alibaba.dubbo.common.utils.NetUtils; //导入依赖的package包/类
@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();
}
 
开发者ID:yunhaibin,项目名称:dubbox-hystrix,代码行数:20,代码来源:EnumBak.java

示例9: joinExchange

import com.alibaba.dubbo.common.utils.NetUtils; //导入依赖的package包/类
public ExchangePeer joinExchange(URL url, ExchangeHandler handler) throws RemotingException {
    ExchangePeer peer = super.join(url, handler);
    try {
        String full = url.toFullString();
        String[] lines = IOUtils.readLines(file);
        for (String line : lines) {
            if (full.equals(line)) {
                return peer;
            }
        }
        IOUtils.appendLines(file, new String[] {full});
    } catch (IOException e) {
        throw new RemotingException(new InetSocketAddress(NetUtils.getLocalHost(), 0), getUrl().toInetSocketAddress(), e.getMessage(), e);
    }
    return peer;
}
 
开发者ID:dachengxi,项目名称:EatDubbo,代码行数:17,代码来源:FileExchangeGroup.java

示例10: isClientSide

import com.alibaba.dubbo.common.utils.NetUtils; //导入依赖的package包/类
protected boolean isClientSide(Channel channel) {
	String side = (String) channel.getAttribute(Constants.SIDE_KEY);
	if ("client".equals(side)) {
		return true;
	} else if ("server".equals(side)) {
		return false;
	} else {
		InetSocketAddress address = channel.getRemoteAddress();
		URL url = channel.getUrl();
		boolean client = url.getPort() == address.getPort()
				&& NetUtils.filterLocalHost(url.getIp()).equals(
						NetUtils.filterLocalHost(address.getAddress()
								.getHostAddress()));
		channel.setAttribute(Constants.SIDE_KEY, client ? "client"
				: "server");
		return client;
	}
}
 
开发者ID:dachengxi,项目名称:EatDubbo,代码行数:19,代码来源:AbstractCodec.java

示例11: AbstractServer

import com.alibaba.dubbo.common.utils.NetUtils; //导入依赖的package包/类
public AbstractServer(URL url, ChannelHandler handler) throws RemotingException {
    super(url, handler);
    localAddress = getUrl().toInetSocketAddress();
    String host = url.getParameter(Constants.ANYHOST_KEY, false) 
                    || NetUtils.isInvalidLocalHost(getUrl().getHost()) 
                    ? NetUtils.ANYHOST : getUrl().getHost();
    bindAddress = new InetSocketAddress(host, getUrl().getPort());
    this.accepts = url.getParameter(Constants.ACCEPTS_KEY, Constants.DEFAULT_ACCEPTS);
    this.idleTimeout = url.getParameter(Constants.IDLE_TIMEOUT_KEY, Constants.DEFAULT_IDLE_TIMEOUT);
    try {
        doOpen();
        if (logger.isInfoEnabled()) {
            logger.info("Start " + getClass().getSimpleName() + " bind " + getBindAddress() + ", export " + getLocalAddress());
        }
    } catch (Throwable t) {
        throw new RemotingException(url.toInetSocketAddress(), null, "Failed to bind " + getClass().getSimpleName() 
                                    + " on " + getLocalAddress() + ", cause: " + t.getMessage(), t);
    }
    if (handler instanceof WrappedChannelHandler ){
        executor = ((WrappedChannelHandler)handler).getExecutor();
    }
}
 
开发者ID:dachengxi,项目名称:EatDubbo,代码行数:23,代码来源:AbstractServer.java

示例12: index

import com.alibaba.dubbo.common.utils.NetUtils; //导入依赖的package包/类
public void index(Map<String, Object> context) throws Exception {
    Map<String, String> properties = new TreeMap<String, String>();
    StringBuilder msg = new StringBuilder();
    msg.append("Version: ");
    msg.append(Version.getVersion(Envs.class, "2.2.0"));
    properties.put("Registry", msg.toString());
    String address = NetUtils.getLocalHost();
    properties.put("Host", NetUtils.getHostName(address) + "/" + address);
    properties.put("Java", System.getProperty("java.runtime.name") + " " + System.getProperty("java.runtime.version"));
    properties.put("OS", System.getProperty("os.name") + " "
            + System.getProperty("os.version"));
    properties.put("CPU", System.getProperty("os.arch", "") + ", "
            + String.valueOf(Runtime.getRuntime().availableProcessors()) + " cores");
    properties.put("Locale", Locale.getDefault().toString() + "/"
            + System.getProperty("file.encoding"));
    properties.put("Uptime", formatUptime(ManagementFactory.getRuntimeMXBean().getUptime()) 
            + " From " + new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS Z").format(new Date(ManagementFactory.getRuntimeMXBean().getStartTime())) 
            + " To " + new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS Z").format(new Date()));
    context.put("properties", properties);
}
 
开发者ID:zhuxiaolei,项目名称:dubbo2,代码行数:21,代码来源:Envs.java

示例13: testMultiRegistry

import com.alibaba.dubbo.common.utils.NetUtils; //导入依赖的package包/类
@Test
public void testMultiRegistry() {
    SimpleRegistryService registryService1 = new SimpleRegistryService();
    Exporter<RegistryService> exporter1 = SimpleRegistryExporter.export(4545, registryService1);
    SimpleRegistryService registryService2 = new SimpleRegistryService();
    Exporter<RegistryService> exporter2 = SimpleRegistryExporter.export(4546, registryService2);
    ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext(ConfigTest.class.getPackage().getName().replace('.', '/') + "/multi-registry.xml");
    ctx.start();
    try {
        List<URL> urls1 = registryService1.getRegistered().get("com.alibaba.dubbo.config.spring.api.DemoService");
        assertNull(urls1);
        List<URL> urls2 = registryService2.getRegistered().get("com.alibaba.dubbo.config.spring.api.DemoService");
        assertNotNull(urls2);
        assertEquals(1, urls2.size());
        assertEquals("dubbo://" + NetUtils.getLocalHost() + ":20880/com.alibaba.dubbo.config.spring.api.DemoService", urls2.get(0).toIdentityString());
    } finally {
        ctx.stop();
        ctx.close();
        exporter1.unexport();
        exporter2.unexport();
    }
}
 
开发者ID:flychao88,项目名称:dubbocloud,代码行数:23,代码来源:ConfigTest.java

示例14: testGenericEnum

import com.alibaba.dubbo.common.utils.NetUtils; //导入依赖的package包/类
@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();
}
 
开发者ID:dachengxi,项目名称:EatDubbo,代码行数:21,代码来源:EnumBak.java

示例15: testInvokeAutoFindMethod

import com.alibaba.dubbo.common.utils.NetUtils; //导入依赖的package包/类
@SuppressWarnings("unchecked")
@Test
public void testInvokeAutoFindMethod() throws RemotingException {
    mockInvoker = EasyMock.createMock(Invoker.class);
    EasyMock.expect(mockInvoker.getInterface()).andReturn(DemoService.class).anyTimes();
    EasyMock.expect(mockInvoker.getUrl()).andReturn(URL.valueOf("dubbo://127.0.0.1:20883/demo")).anyTimes();
    EasyMock.expect(mockInvoker.invoke((Invocation) EasyMock.anyObject())).andReturn(new RpcResult("ok")).anyTimes();
    mockChannel = EasyMock.createMock(Channel.class);
    EasyMock.expect(mockChannel.getAttribute("telnet.service")).andReturn(null).anyTimes();
    EasyMock.expect(mockChannel.getLocalAddress()).andReturn(NetUtils.toAddress("127.0.0.1:5555")).anyTimes();
    EasyMock.expect(mockChannel.getRemoteAddress()).andReturn(NetUtils.toAddress("127.0.0.1:20883")).anyTimes();
    EasyMock.replay(mockChannel, mockInvoker);
    DubboProtocol.getDubboProtocol().export(mockInvoker);
    String result = invoke.telnet(mockChannel, "echo(\"ok\")");
    assertTrue(result.contains("ok"));
    EasyMock.reset(mockChannel, mockInvoker);
}
 
开发者ID:dachengxi,项目名称:EatDubbo,代码行数:18,代码来源:InvokerTelnetHandlerTest.java


注:本文中的com.alibaba.dubbo.common.utils.NetUtils类示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。