Spark Utils find localIpAddress

来源:互联网 发布:软件定义 编辑:程序博客网 时间:2024/06/07 23:10
/**   * Get the local host's IP address in dotted-quad format (e.g. 1.2.3.4).   * Note, this is typically not used from within core spark.   */  private lazy val localIpAddress: InetAddress = findLocalInetAddress()  private def findLocalInetAddress(): InetAddress = {    val defaultIpOverride = System.getenv("SPARK_LOCAL_IP")    if (defaultIpOverride != null) {      InetAddress.getByName(defaultIpOverride)    } else {      val address = InetAddress.getLocalHost      if (address.isLoopbackAddress) {        // Address resolves to something like 127.0.1.1, which happens on Debian; try to find        // a better address using the local network interfaces        // getNetworkInterfaces returns ifs in reverse order compared to ifconfig output order        // on unix-like system. On windows, it returns in index order.        // It's more proper to pick ip address following system output order.        val activeNetworkIFs = NetworkInterface.getNetworkInterfaces.asScala.toSeq        val reOrderedNetworkIFs = if (isWindows) activeNetworkIFs else activeNetworkIFs.reverse        for (ni <- reOrderedNetworkIFs) {          val addresses = ni.getInetAddresses.asScala            .filterNot(addr => addr.isLinkLocalAddress || addr.isLoopbackAddress).toSeq          if (addresses.nonEmpty) {            val addr = addresses.find(_.isInstanceOf[Inet4Address]).getOrElse(addresses.head)            // because of Inet6Address.toHostName may add interface at the end if it knows about it            val strippedAddress = InetAddress.getByAddress(addr.getAddress)            // We've found an address that looks reasonable!            logWarning("Your hostname, " + InetAddress.getLocalHost.getHostName + " resolves to" +              " a loopback address: " + address.getHostAddress + "; using " +              strippedAddress.getHostAddress + " instead (on interface " + ni.getName + ")")            logWarning("Set SPARK_LOCAL_IP if you need to bind to another address")            return strippedAddress          }        }        logWarning("Your hostname, " + InetAddress.getLocalHost.getHostName + " resolves to" +          " a loopback address: " + address.getHostAddress + ", but we couldn't find any" +          " external IP address!")        logWarning("Set SPARK_LOCAL_IP if you need to bind to another address")      }      address    }  }
0 0
原创粉丝点击