private static String init()

in eventmesh-common/src/main/java/org/apache/eventmesh/common/utils/IPUtils.java [53:119]


    private static String init() {
        // if the progress works under docker environment
        // return the host ip about this docker located from environment value
        String dockerHostIp = System.getenv("docker_host_ip");
        if (dockerHostIp != null && !"".equals(dockerHostIp)) {
            return dockerHostIp;
        }

        // priority of networkInterface when generating client ip
        String priority = System.getProperty("networkInterface.priority", "eth0<eth1<bond1");
        List<String> list = Arrays.asList(priority.split("<"));
        ArrayList<String> preferList = new ArrayList<>(list);
        NetworkInterface preferNetworkInterface = null;
        boolean isInterfacePreferred = false;

        try {
            Enumeration<NetworkInterface> enumeration1 = NetworkInterface.getNetworkInterfaces();
            while (enumeration1.hasMoreElements()) {
                final NetworkInterface networkInterface = enumeration1.nextElement();
                String interfaceName = networkInterface.getName();
                if (!isInterfacePreferred && preferList.contains(interfaceName)) {
                    isInterfacePreferred = true;
                }
                if (preferNetworkInterface == null) {
                    preferNetworkInterface = networkInterface;
                } else if (preferList.indexOf(interfaceName) // get the networkInterface that has higher priority
                    > preferList.indexOf(preferNetworkInterface.getName())) {
                    preferNetworkInterface = networkInterface;
                }
            }

            // Traversal Network interface to get the first non-loopback and non-private address
            ArrayList<String> ipv4Result = new ArrayList<String>();
            ArrayList<String> ipv6Result = new ArrayList<String>();

            if (preferNetworkInterface != null && isInterfacePreferred) {
                final Enumeration<InetAddress> en = preferNetworkInterface.getInetAddresses();
                getIpResult(ipv4Result, ipv6Result, en);
            } else {
                Enumeration<NetworkInterface> enumeration = NetworkInterface.getNetworkInterfaces();
                while (enumeration.hasMoreElements()) {
                    final NetworkInterface networkInterface = enumeration.nextElement();
                    final Enumeration<InetAddress> en = networkInterface.getInetAddresses();
                    getIpResult(ipv4Result, ipv6Result, en);
                }
            }

            // prefer ipv4
            if (!ipv4Result.isEmpty()) {
                for (String ip : ipv4Result) {
                    if (isValidIPV4Address(ip) && !ip.startsWith("127.0") && !ip.startsWith("192.168")) {
                        return ip;
                    }
                }

                return ipv4Result.get(ipv4Result.size() - 1);
            } else if (!ipv6Result.isEmpty()) {
                return ipv6Result.get(0);
            }
            // If failed to find,fall back to localhost
            final InetAddress localHost = InetAddress.getLocalHost();
            return normalizeHostAddress(localHost);
        } catch (SocketException | UnknownHostException e) {
            log.error("socket or unknown host exception:", e);
        }
        return null;
    }