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); } } }
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); } } }