Mac获取本地IP超慢

唐代de豆腐 发布于 2016/10/19 10:37
阅读 366
收藏 0

public static InetAddress getLocalHost() throws UnknownHostException {



        SecurityManager security = System.getSecurityManager();

        try {

            String local = impl.getLocalHostName();



            if (security != null) {

                security.checkConnect(local, -1);

            }



            if (local.equals("localhost")) {

                return impl.loopbackAddress();

            }



            InetAddress ret = null;

            synchronized (cacheLock) {

                long now = System.currentTimeMillis();

                if (cachedLocalHost != null) {

                    if ((now - cacheTime) < maxCacheTime) // Less than 5s old?

                        ret = cachedLocalHost;

                    else

                        cachedLocalHost = null;

                }



                // we are calling getAddressesFromNameService directly

                // to avoid getting localHost from cache

                if (ret == null) {

                    InetAddress[] localAddrs;

                    try {

                        localAddrs =

                            InetAddress.getAddressesFromNameService(local, null);

                    } catch (UnknownHostException uhe) {

                        // Rethrow with a more informative error message.

                        UnknownHostException uhe2 =

                            new UnknownHostException(local + ": " +

                                                     uhe.getMessage());

                        uhe2.initCause(uhe);

                        throw uhe2;

                    }

                    cachedLocalHost = localAddrs[0];

                    cacheTime = now;

                    ret = localAddrs[0];

                }

            }

            return ret;

        } catch (java.lang.SecurityException e) {

            return impl.loopbackAddress();

        }

    }



升级mac 系统后,电脑主机名本来是tangdu.local ,升级后这个名称ping不通了。无奈之下只能把主机名改成localhost.

PS:mac的主机名不需要在hosts中配置也可以ping .有熟悉网络朋友可以解释下嘛。

如果 我在hosts中配置了 127.0.0.1 tangdu.local .但在dubbo中执行这个方法要很长时间5S。


加载中
返回顶部
顶部