本文整理汇总了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));
}
示例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;
}
}
示例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);
}
示例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;
}
示例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();
}
示例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();
}
}
示例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);
}
}
示例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();
}
示例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;
}
示例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;
}
}
示例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();
}
}
示例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);
}
示例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();
}
}
示例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();
}
示例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);
}