public static String getMyIp() {
        String ip = null;
        try {
            Enumeration<NetworkInterface> interfaces = NetworkInterface.getNetworkInterfaces();
            while (interfaces.hasMoreElements()) {
                NetworkInterface iface = interfaces.nextElement();
                // filters out 127.0.0.1 and inactive interfaces
                if (iface.isLoopback() || !iface.isUp()) {
                    continue;
                }

                Enumeration<InetAddress> addresses = iface.getInetAddresses();
                while (addresses.hasMoreElements()) {
                    InetAddress addr = addresses.nextElement();
                    if (addr instanceof Inet4Address) {
                        ip = addr.getHostAddress();
                        break;
                    }
                }
            }
            return ip;
        } catch (SocketException e) {
            return "localhost";
        }
    }
Test helper
@author boyan (boyan@alibaba-inc.com)

2018-Apr-11 10:16:07 AM
jraft-extension/rpc-grpc-impl/src/test/java/com/alipay/sofa/jraft/test/TestUtils.java