Tabnine Logo
NetUtils
Code IndexAdd Tabnine to your IDE (free)

How to use
NetUtils
in
org.apache.servicecomb.foundation.common.net

Best Java code snippets using org.apache.servicecomb.foundation.common.net.NetUtils (Showing top 20 results out of 315)

origin: apache/servicecomb-java-chassis

 @Override
 public boolean ping(MicroserviceInstance instance) {
  if (instance.getEndpoints() != null && instance.getEndpoints().size() > 0) {
   IpPort ipPort = NetUtils.parseIpPortFromURI(instance.getEndpoints().get(0));
   try (Socket s = new Socket()) {
    s.connect(new InetSocketAddress(ipPort.getHostOrIp(), ipPort.getPort()), 3000);
    return true;
   } catch (IOException e) {
    // ignore this error
   }
  }
  return false;
 }
}
origin: apache/servicecomb-java-chassis

public static boolean canPublishEndpoint(String listenAddress) {
 if (StringUtils.isEmpty(listenAddress)) {
  LOGGER.info("listenAddress is null, can not publish.");
  return false;
 }
 IpPort ipPort = NetUtils.parseIpPortFromURI("http://" + listenAddress);
 if (ipPort == null) {
  LOGGER.info("invalid listenAddress {}, can not publish, format should be ip:port.", listenAddress);
  return false;
 }
 if (NetUtils.canTcpListen(ipPort.getSocketAddress().getAddress(), ipPort.getPort())) {
  LOGGER.info("{} is not listened, can not publish.", ipPort.getSocketAddress());
  return false;
 }
 return true;
}
origin: apache/servicecomb-java-chassis

/**
 * Parse a {@link URI} into an {@link IpPort}.
 *
 * <p>
 *   A uri without port is allowed, in which case the port will be inferred from the scheme. {@code http} is 80, and
 *   {@code https} is 443.
 * </p>
 * <p>
 *   The host of the {@code uri} should not be null, or it will be treated as an illegal param,
 *   and an {@link IllegalArgumentException} will be thrown.
 * </p>
 */
public static IpPort parseIpPort(URI uri) {
 return parseIpPort(uri, false);
}
origin: apache/servicecomb-java-chassis

public static String getPublishHostName() {
 String publicAddressSetting =
   DynamicPropertyFactory.getInstance().getStringProperty(PUBLISH_ADDRESS, "").get();
 publicAddressSetting = publicAddressSetting.trim();
 if (publicAddressSetting.isEmpty()) {
  return NetUtils.getHostName();
 }
 if (publicAddressSetting.startsWith("{") && publicAddressSetting.endsWith("}")) {
  return NetUtils
    .ensureGetInterfaceAddress(publicAddressSetting.substring(1, publicAddressSetting.length() - 1))
    .getHostName();
 }
 return publicAddressSetting;
}
origin: apache/servicecomb-java-chassis

public static String getPublishAddress() {
 String publicAddressSetting =
   DynamicPropertyFactory.getInstance().getStringProperty(PUBLISH_ADDRESS, "").get();
 publicAddressSetting = publicAddressSetting.trim();
 if (publicAddressSetting.isEmpty()) {
  return NetUtils.getHostAddress();
 }
 // placeholder is network interface name
 if (publicAddressSetting.startsWith("{") && publicAddressSetting.endsWith("}")) {
  return NetUtils
    .ensureGetInterfaceAddress(publicAddressSetting.substring(1, publicAddressSetting.length() - 1))
    .getHostAddress();
 }
 return publicAddressSetting;
}
origin: apache/servicecomb-java-chassis

@Override
public boolean canInit() {
 setListenAddressWithoutSchema(TransportConfig.getAddress());
 URIEndpointObject ep = (URIEndpointObject) getEndpoint().getAddress();
 if (ep == null) {
  return true;
 }
 if (!NetUtils.canTcpListen(ep.getSocketAddress().getAddress(), ep.getPort())) {
  LOGGER.warn(
    "Can not start VertxRestTransport, the port:{} may have been occupied. You can ignore this message if you are using a web container like tomcat.",
    ep.getPort());
  return false;
 }
 return true;
}
origin: apache/servicecomb-java-chassis

public static String getHostAddress() {
 //If failed to get host address ,micro-service will registry failed
 //So I add retry mechanism
 if (hostAddress == null) {
  doGetHostNameAndHostAddress();
 }
 return hostAddress;
}
origin: apache/servicecomb-java-chassis

protected void setListenAddressWithoutSchema(String addressWithoutSchema,
  Map<String, String> pairs) {
 addressWithoutSchema = genAddressWithoutSchema(addressWithoutSchema, pairs);
 this.endpoint = new Endpoint(this, NetUtils.getRealListenAddress(getName(), addressWithoutSchema));
 if (this.endpoint.getEndpoint() != null) {
  this.publishEndpoint = new Endpoint(this, RegistryUtils.getPublishAddress(getName(),
    addressWithoutSchema));
 } else {
  this.publishEndpoint = null;
 }
}
origin: apache/servicecomb-java-chassis

private static void doGetHostNameAndHostAddress() {
 try {
  doGetIpv4AddressFromNetworkInterface();
  // getLocalHost will throw exception in some docker image and sometimes will do a hostname lookup and time consuming
  InetAddress localHost = InetAddress.getLocalHost();
  hostName = localHost.getHostName();
  if ((localHost.isAnyLocalAddress() || localHost.isLoopbackAddress() || localHost.isMulticastAddress())
    && !allInterfaceAddresses.isEmpty()) {
   InetAddress availabelAddress = allInterfaceAddresses.values().iterator().next();
   hostAddress = availabelAddress.getHostAddress();
   LOGGER.warn("cannot find a proper host address, choose {}, may not be correct.", hostAddress);
  } else {
   LOGGER.info("get localhost address: {}", localHost.getHostAddress());
   hostAddress = localHost.getHostAddress();
  }
  LOGGER.info(
    "add host name from localhost:" + hostName + ",host address:" + hostAddress);
 } catch (Exception e) {
  LOGGER.error("got exception when trying to get addresses:", e);
  if (allInterfaceAddresses.size() >= 1) {
   InetAddress entry = allInterfaceAddresses.entrySet().iterator().next().getValue();
   // get host name will do a reverse name lookup and is time consuming
   hostName = entry.getHostName();
   hostAddress = entry.getHostAddress();
   LOGGER.info(
     "add host name from interfaces:" + hostName + ",host address:" + hostAddress);
  }
 }
}
origin: apache/servicecomb-java-chassis

private static IpPort genPublishIpPort(String schema, IpPort ipPort) {
 String publicAddressSetting = DynamicPropertyFactory.getInstance()
   .getStringProperty(PUBLISH_ADDRESS, "")
   .get();
 publicAddressSetting = publicAddressSetting.trim();
 if (publicAddressSetting.isEmpty()) {
  InetSocketAddress socketAddress = ipPort.getSocketAddress();
  if (socketAddress.getAddress().isAnyLocalAddress()) {
   String host = NetUtils.getHostAddress();
   LOGGER.warn("address {}, auto select a host address to publish {}:{}, maybe not the correct one",
     socketAddress,
     host,
     socketAddress.getPort());
   return new IpPort(host, ipPort.getPort());
  }
  return ipPort;
 }
 if (publicAddressSetting.startsWith("{") && publicAddressSetting.endsWith("}")) {
  publicAddressSetting = NetUtils
    .ensureGetInterfaceAddress(
      publicAddressSetting.substring(1, publicAddressSetting.length() - 1))
    .getHostAddress();
 }
 String publishPortKey = PUBLISH_PORT.replace("{transport_name}", schema);
 int publishPortSetting = DynamicPropertyFactory.getInstance()
   .getIntProperty(publishPortKey, 0)
   .get();
 int publishPort = publishPortSetting == 0 ? ipPort.getPort() : publishPortSetting;
 return new IpPort(publicAddressSetting, publishPort);
}
origin: org.apache.servicecomb/service-registry

public static String getPublishHostName() {
 String publicAddressSetting =
   DynamicPropertyFactory.getInstance().getStringProperty(PUBLISH_ADDRESS, "").get();
 publicAddressSetting = publicAddressSetting.trim();
 if (publicAddressSetting.isEmpty()) {
  return NetUtils.getHostName();
 }
 if (publicAddressSetting.startsWith("{") && publicAddressSetting.endsWith("}")) {
  return NetUtils
    .ensureGetInterfaceAddress(publicAddressSetting.substring(1, publicAddressSetting.length() - 1))
    .getHostName();
 }
 return publicAddressSetting;
}
origin: org.apache.servicecomb/transport-rest-vertx

@Override
public boolean canInit() {
 setListenAddressWithoutSchema(TransportConfig.getAddress());
 URIEndpointObject ep = (URIEndpointObject) getEndpoint().getAddress();
 if (ep == null) {
  return true;
 }
 if (!NetUtils.canTcpListen(ep.getSocketAddress().getAddress(), ep.getPort())) {
  LOGGER.warn(
    "Can not start VertxRestTransport, the port:{} may have been occupied. You can ignore this message if you are using a web container like tomcat.",
    ep.getPort());
  return false;
 }
 return true;
}
origin: apache/servicecomb-java-chassis

public static String getHostName() {
 //If failed to get host name ,micro-service will registry failed
 //So I add retry mechanism
 if (hostName == null) {
  doGetHostNameAndHostAddress();
 }
 return hostName;
}
origin: org.apache.servicecomb/java-chassis-core

protected void setListenAddressWithoutSchema(String addressWithoutSchema,
  Map<String, String> pairs) {
 addressWithoutSchema = genAddressWithoutSchema(addressWithoutSchema, pairs);
 this.endpoint = new Endpoint(this, NetUtils.getRealListenAddress(getName(), addressWithoutSchema));
 if (this.endpoint.getEndpoint() != null) {
  this.publishEndpoint = new Endpoint(this, RegistryUtils.getPublishAddress(getName(),
    addressWithoutSchema));
 } else {
  this.publishEndpoint = null;
 }
}
origin: org.apache.servicecomb/foundation-common

private static void doGetHostNameAndHostAddress() {
 try {
  doGetIpv4AddressFromNetworkInterface();
  // getLocalHost will throw exception in some docker image and sometimes will do a hostname lookup and time consuming
  InetAddress localHost = InetAddress.getLocalHost();
  hostName = localHost.getHostName();
  if ((localHost.isAnyLocalAddress() || localHost.isLoopbackAddress() || localHost.isMulticastAddress())
    && !allInterfaceAddresses.isEmpty()) {
   InetAddress availabelAddress = allInterfaceAddresses.values().iterator().next();
   hostAddress = availabelAddress.getHostAddress();
   LOGGER.warn("cannot find a proper host address, choose {}, may not be correct.", hostAddress);
  } else {
   hostAddress = localHost.getHostAddress();
  }
  LOGGER.info(
    "add host name from localhost:" + hostName + ",host address:" + hostAddress);
 } catch (Exception e) {
  LOGGER.error("got exception when trying to get addresses:", e);
  if (allInterfaceAddresses.size() >= 1) {
   InetAddress entry = allInterfaceAddresses.entrySet().iterator().next().getValue();
   // get host name will do a reverse name lookup and is time consuming
   hostName = entry.getHostName();
   hostAddress = entry.getHostAddress();
   LOGGER.info(
     "add host name from interfaces:" + hostName + ",host address:" + hostAddress);
  }
 }
}
origin: apache/servicecomb-java-chassis

public void doWatch(String configCenter)
  throws UnsupportedEncodingException, InterruptedException {
 CountDownLatch waiter = new CountDownLatch(1);
 IpPort ipPort = NetUtils.parseIpPortFromURI(configCenter);
 String url = uriConst.REFRESH_ITEMS + "?dimensionsInfo="
   + StringUtils.deleteWhitespace(URLEncoder.encode(serviceName, "UTF-8"));
origin: apache/servicecomb-java-chassis

public static IpPort parseIpPort(String scheme, String authority) {
 if (authority == null) {
  return null;
 }
 return parseIpPort(URI.create(scheme + "://" + authority));
}
origin: org.apache.servicecomb/service-registry

public static String getPublishAddress() {
 String publicAddressSetting =
   DynamicPropertyFactory.getInstance().getStringProperty(PUBLISH_ADDRESS, "").get();
 publicAddressSetting = publicAddressSetting.trim();
 if (publicAddressSetting.isEmpty()) {
  return NetUtils.getHostAddress();
 }
 // placeholder is network interface name
 if (publicAddressSetting.startsWith("{") && publicAddressSetting.endsWith("}")) {
  return NetUtils
    .ensureGetInterfaceAddress(publicAddressSetting.substring(1, publicAddressSetting.length() - 1))
    .getHostAddress();
 }
 return publicAddressSetting;
}
origin: org.apache.servicecomb/foundation-common

public static String getHostAddress() {
 //If failed to get host address ,micro-service will registry failed
 //So I add retry mechanism
 if (hostAddress == null) {
  doGetHostNameAndHostAddress();
 }
 return hostAddress;
}
origin: apache/servicecomb-java-chassis

  + ParseConfigUtils.getInstance().getCurrentVersionInfo();
clientMgr.findThreadBindClientPool().runOnContext(client -> {
 IpPort ipPort = NetUtils.parseIpPortFromURI(configcenter);
 HttpClientRequest request = client.get(ipPort.getPort(), ipPort.getHostOrIp(), path, rsp -> {
  if (rsp.statusCode() == HttpResponseStatus.OK.code()) {
org.apache.servicecomb.foundation.common.netNetUtils

Most used methods

  • parseIpPortFromURI
  • canTcpListen
  • parseIpPort
    Parse a URI into an IpPort
  • doGetHostNameAndHostAddress
  • doGetIpv4AddressFromNetworkInterface
    docker环境中,有时无法通过InetAddress.getLocalHost()获取 ,会报unknown host Exception, system error 此时,通过遍历网卡接口的方式规
  • ensureGetInterfaceAddress
  • getHostAddress
  • getHostName
  • getRealListenAddress
    对于配置为0.0.0.0的地址,let it go schema, e.g. http adddress, e.g 0.0.0.0:8080 return 实际监听的地址
  • humanReadableBytes

Popular in Java

  • Running tasks concurrently on multiple threads
  • getExternalFilesDir (Context)
  • scheduleAtFixedRate (Timer)
  • compareTo (BigDecimal)
  • BorderLayout (java.awt)
    A border layout lays out a container, arranging and resizing its components to fit in five regions:
  • FlowLayout (java.awt)
    A flow layout arranges components in a left-to-right flow, much like lines of text in a paragraph. F
  • ConcurrentHashMap (java.util.concurrent)
    A plug-in replacement for JDK1.5 java.util.concurrent.ConcurrentHashMap. This version is based on or
  • Collectors (java.util.stream)
  • JOptionPane (javax.swing)
  • IsNull (org.hamcrest.core)
    Is the value null?
  • Top plugins for WebStorm
Tabnine Logo
  • Products

    Search for Java codeSearch for JavaScript code
  • IDE Plugins

    IntelliJ IDEAWebStormVisual StudioAndroid StudioEclipseVisual Studio CodePyCharmSublime TextPhpStormVimGoLandRubyMineEmacsJupyter NotebookJupyter LabRiderDataGripAppCode
  • Company

    About UsContact UsCareers
  • Resources

    FAQBlogTabnine AcademyTerms of usePrivacy policyJava Code IndexJavascript Code Index
Get Tabnine for your IDE now