Java Code Examples for org.apache.servicecomb.foundation.common.net.NetUtils#getHostAddress()

The following examples show how to use org.apache.servicecomb.foundation.common.net.NetUtils#getHostAddress() . 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: CloudEyeFilePublisher.java    From servicecomb-samples with Apache License 2.0 6 votes vote down vote up
@Override
public void init(GlobalRegistry globalRegistry, EventBus eventBus, MetricsBootstrapConfig config) {
  eventBus.register(this);

  Microservice microservice = RegistryUtils.getMicroservice();
  filePrefix = microservice.getAppId() + "." + microservice.getServiceName();

  hostName = NetUtils.getHostName();
  if (StringUtils.isEmpty(hostName)) {
    hostName = NetUtils.getHostAddress();
  }

  System.setProperty("cloudEye.logDir",
      DynamicPropertyFactory
          .getInstance()
          .getStringProperty("cloudEye.logDir", "logs")
          .get());
}
 
Example 2
Source File: RegistrationManager.java    From servicecomb-java-chassis with Apache License 2.0 6 votes vote down vote up
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;
}
 
Example 3
Source File: RegistryHandler.java    From spring-cloud-huawei with Apache License 2.0 5 votes vote down vote up
private static MicroserviceInstance buildInstance(String serviceID,
    ServiceCombDiscoveryProperties serviceCombDiscoveryProperties,
    TagsProperties tagsProperties) {
  MicroserviceInstance microserviceInstance = new MicroserviceInstance();
  microserviceInstance.setServiceId(serviceID);
  microserviceInstance.setHostName(NetUtil.getLocalHost());
  if (null != serviceCombDiscoveryProperties.getDatacenter()) {
    microserviceInstance.setDataCenterInfo(serviceCombDiscoveryProperties.getDatacenter());
  }
  List<String> endPoints = new ArrayList<>();
  String address = NetUtils.getHostAddress();
  endPoints.add("rest://" + address + ":" + serviceCombDiscoveryProperties.getPort());
  microserviceInstance.setEndpoints(endPoints);
  HealthCheck healthCheck = new HealthCheck();
  healthCheck.setMode(HealthCheckMode.PLATFORM);
  healthCheck.setInterval(serviceCombDiscoveryProperties.getHealthCheckInterval());
  healthCheck.setTimes(3);
  microserviceInstance.setHealthCheck(healthCheck);
  String currTime = String.valueOf(System.currentTimeMillis());
  microserviceInstance.setTimestamp(currTime);
  microserviceInstance.setModTimestamp(currTime);
  microserviceInstance.setVersion(serviceCombDiscoveryProperties.getVersion());
  Map<String, String> properties = new HashMap<>();
  if (tagsProperties.getTag() != null) {
    properties.putAll(tagsProperties.getTag());
  }
  properties.putAll(genCasProperties());
  microserviceInstance.setProperties(properties);
  return microserviceInstance;
}
 
Example 4
Source File: RegistrationManager.java    From servicecomb-java-chassis with Apache License 2.0 5 votes vote down vote up
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);
}